How to get a parameter from MainActivity via DependencyService?

Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

Hi!
I'm working with DependencyServie. Some method must implement in MainActivity and return a List result
How to get that List result from MainActivity via DependencyService?
Thank you!

Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @Liêm_Nguyễn said:
    Hi!
    I'm working with DependencyServie. Some method must implement in MainActivity and return a List result
    How to get that List result from MainActivity via DependencyService?
    Thank you!

    What have you tried?

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    > @JohnHardman said:
    > @Liêm_Nguyễn said:
    > Hi!
    > I'm working with DependencyServie. Some method must implement in MainActivity and return a List result
    > How to get that List result from MainActivity via DependencyService?
    > Thank you!
    >
    >
    >
    >
    >
    > What have you tried?

    Im trying to implement BSImagePicker in Dependency Service
    https://github.com/siralam/BSImagePicker
  • JohnHardmanJohnHardman GBUniversity mod

    What have you tried in order to implement "How to get that List result from MainActivity via DependencyService?"

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    > @JohnHardman said:
    > What have you tried in order to implement "How to get that List result from MainActivity via DependencyService?"

    I have try MessagingCenter. Pleae suggest me the best solution!
    Thank you!
  • JarvanJarvan Member, Xamarin Team Xamurai

    Some method must implement in MainActivity ... How to get that List result from MainActivity via DependencyService?

    Add a parameter in App's constructor method(). Pass the data from MainActivity to page in PCL.
    Activity.cs

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public string a;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ...
            a = "parameter from MainActivity";
            LoadApplication(new App(a));
        }
    }
    

    App.xaml.cs

    public App(string s)
    {
        InitializeComponent();
    
        MainPage = new MainPage(s);
    }
    

    page.xaml.cs

    public partial class MainPage : ContentPage
    {
        string a;
        public MainPage(string s)
        {
            InitializeComponent();
    
            a = s;// get the data
        }
    }
    
  • JohnHardmanJohnHardman GBUniversity mod

    @Liêm_Nguyễn said:
    > @JohnHardman said:
    > What have you tried in order to implement "How to get that List result from MainActivity via DependencyService?"

    I have try MessagingCenter. Pleae suggest me the best solution!
    Thank you!

    I am assuming that the list is generated after your app's first page has been displayed. Is that the case?

    If so, MessagingCenter is one approach (there are others). Show your code using MessagingCenter and explain what problem you encountered. That way, somebody can help identify the cause of the problem.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @JohnHardman said:

    @Liêm_Nguyễn said:
    > @JohnHardman said:
    > What have you tried in order to implement "How to get that List result from MainActivity via DependencyService?"

    I have try MessagingCenter. Pleae suggest me the best solution!
    Thank you!

    I am assuming that the list is generated after your app's first page has been displayed. Is that the case?

    If so, MessagingCenter is one approach (there are others). Show your code using MessagingCenter and explain what problem you encountered. That way, somebody can help identify the cause of the problem.

    But MessaginCenter not fire immediately..only fire when I run the code on next time then MC return previous results.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:

    Some method must implement in MainActivity ... How to get that List result from MainActivity via DependencyService?

    Add a parameter in App's constructor method(). Pass the data from MainActivity to page in PCL.
    Activity.cs

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public string a;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ...
            a = "parameter from MainActivity";
            LoadApplication(new App(a));
        }
    }
    

    App.xaml.cs

    public App(string s)
    {
        InitializeComponent();
    
        MainPage = new MainPage(s);
    }
    

    page.xaml.cs

    public partial class MainPage : ContentPage
    {
        string a;
        public MainPage(string s)
        {
            InitializeComponent();
            
            a = s;// get the data
        }
    }
    

    Great, I'll try your solution. Thank!

  • JohnHardmanJohnHardman GBUniversity mod

    @Liêm_Nguyễn

    For people to be able to help answer your question, you need to answer their requests for clarification.

    @JohnHardman said:
    I am assuming that the list is generated after your app's first page has been displayed. Is that the case?

    @JohnHardman said:
    If so, MessagingCenter is one approach (there are others). Show your code using MessagingCenter and explain what problem you encountered. That way, somebody can help identify the cause of the problem.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @JohnHardman said:
    @Liêm_Nguyễn

    For people to be able to help answer your question, you need to answer their requests for clarification.

    @JohnHardman said:
    I am assuming that the list is generated after your app's first page has been displayed. Is that the case?

    @JohnHardman said:
    If so, MessagingCenter is one approach (there are others). Show your code using MessagingCenter and explain what problem you encountered. That way, somebody can help identify the cause of the problem.

    Sorry my bad Eng!
    I have a Button to show Gallery Image..User select multi image then click OK..After selected i need to show selected Image on UI!

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @JohnHardman said:

    @Liêm_Nguyễn said:
    > @JohnHardman said:
    > What have you tried in order to implement "How to get that List result from MainActivity via DependencyService?"

    I have try MessagingCenter. Pleae suggest me the best solution!
    Thank you!

    I am assuming that the list is generated after your app's first page has been displayed. Is that the case?

    If so, MessagingCenter is one approach (there are others). Show your code using MessagingCenter and explain what problem you encountered. That way, somebody can help identify the cause of the problem.

    I'm trying to use BSImagePicker in Xamarin Forms via DependencyService.

    The plug in return IList in MainActivity. I need to get that List in DependencyService!

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:

    Some method must implement in MainActivity ... How to get that List result from MainActivity via DependencyService?

    Add a parameter in App's constructor method(). Pass the data from MainActivity to page in PCL.
    Activity.cs

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public string a;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ...
            a = "parameter from MainActivity";
            LoadApplication(new App(a));
        }
    }
    

    App.xaml.cs

    public App(string s)
    {
        InitializeComponent();
    
        MainPage = new MainPage(s);
    }
    

    page.xaml.cs

    public partial class MainPage : ContentPage
    {
        string a;
        public MainPage(string s)
        {
            InitializeComponent();
            
            a = s;// get the data
        }
    }
    

    I'm trying to use BSImagePicker in Xamarin Forms via DependencyService.

    The plug in return IList in MainActivity. I need to get that List in DependencyService!

  • JohnHardmanJohnHardman GBUniversity mod

    @Liêm_Nguyễn said:
    I'm trying to use BSImagePicker in Xamarin Forms via DependencyService.

    The plug in return IList in MainActivity. I need to get that List in DependencyService!

    Post your current Android DependencyService code, and the code where you try to make use of the DependencyService.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    edited July 16

    @JohnHardman said:

    @Liêm_Nguyễn said:
    I'm trying to use BSImagePicker in Xamarin Forms via DependencyService.

    The plug in return IList in MainActivity. I need to get that List in DependencyService!

    Post your current Android DependencyService code, and the code where you try to make use of the DependencyService.

    interface
    public interface IPickMultiImageService
    {
    Task<List> PickPhotosAsync();
    }

    Android implement
    [assembly:Dependency(typeof(PickMultiImageImplement))]
    namespace MyProject
    {
    public class PickMultiImageImplement : IPickMultiImageService
    {
    private List ImagePath = new List();

            public async Task<List<string>> PickPhotosAsync()
            {
                FormsAppCompatActivity activity = CrossCurrentActivity.Current.Activity as FormsAppCompatActivity;
                BSImagePicker bsImagePicker = new BSImagePicker.Builder("com.trangdangtin.mobileapp.fileprovider")
                    .SetMaximumDisplayingImages(24) //Default: Integer.MAX_VALUE. Don't worry about performance :)
                    .SetSpanCount(3) //Default: 3. This is the number of columns
                    .SetGridSpacing(Utils.Dp2px(2)) //Default: 2dp. Remember to pass in a value in pixel.
                    .SetPeekHeight(Utils.Dp2px(360)) //Default: 360dp. This is the initial height of the dialog.
                    .HideCameraTile() //Default: show. Set this if you don't want user to take photo.
                    .HideGalleryTile() //Default: show. Set this if you don't want to further let user select from a gallery app. In such case, I suggest you to set maximum displaying images to Integer.MAX_VALUE.
                    .SetTag("A request ID") //Default: null. Set this if you need to identify which picker is calling back your fragment / activity.  
                    .IsMultiSelect()
                    .SetMaximumMultiSelectCount(6)
                    .Build();
                bsImagePicker.Show(activity.SupportFragmentManager, "picker");                      
                return ImagePath;
            }
        }
    }
    

    MainActivity
    public void OnMultiImageSelected(IList<Android.Net.Uri> uriList, string tag)
    {
    if(uriList!=null)
    {
    List listImage = new List();
    foreach (var item in uriList)
    {
    listImage.Add(item.Path);
    }
    }
    }

  • JohnHardmanJohnHardman GBUniversity mod

    @""Liêm_Nguyễn"

    I don't see any code attempting to use the dependency service. You need to call DependencyService.Get<IPickMultiImageService>() to get the implementation of the service, and then call PickPhotosAsync() through the interface.

    Your implementation of PickPhotosAsync() is returning ImagePath, but other than creating a new List, nothing more has been done, so the List will always be empty when returned.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    edited July 16
    > @JohnHardman said:
    > @""Liêm_Nguyễn"
    >
    > I don't see any code attempting to use the dependency service. You need to call DependencyService.Get() to get the implementation of the service, and then call PickPhotosAsync() through the interface.
    >
    > Your implementation of PickPhotosAsync() is returning ImagePath, but other than creating a new List, nothing more has been done, so the List will always be empty when returned.

    Yes I have call DS.Get() in Button Clicked. But The UI pick image only show after row return ImagePath; ran. The ImagePath always null before the UI pick image show up.

    After pick image the OnMultiImageSelected in MainActivity will run and return a IList Uri in MainActivity. I need to get this IList Uri
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited July 17

    @Liêm_Nguyễn
    You can pass the list data to PickMultiImageImplement class instead of getting data from MainActivity in PickMultiImageImplement class. Create a static parameter in PickMultiImageImplement class. Pass the data to list in MainActivity.

    See as the code:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            //pass the value to list
            PickMultiImageImplement.list = get_list(); 
    
        }
    }
    
    public class PickMultiImageImplement : IPickMultiImageService
    {
        public static List<string> list;
    
        public void test()
        {
            //operate the list
        }
    }
    
  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:
    @Liêm_Nguyễn
    You can pass the list data to PickMultiImageImplement class instead of getting data from MainActivity in PickMultiImageImplement class. Create a static parameter in PickMultiImageImplement class. Pass the data to list in MainActivity.

    See as the code:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            //pass the value to list
            PickMultiImageImplement.list = get_list(); 
    
        }
    }
    
    public class PickMultiImageImplement : IPickMultiImageService
    {
        public static List<string> list;
    
        public void test()
        {
            //operate the list
        }
    }
    

    How to return a List
    List path = DependencyService.Get().....

  • JarvanJarvan Member, Xamarin Team Xamurai

    @Liêm_Nguyễn

    Some method must implement in MainActivity and return a List result

    Just pass the return data to the static list.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:
    @Liêm_Nguyễn

    Some method must implement in MainActivity and return a List result

    Just pass the return data to the static list.

    I need to use that list in PCL, the PickMultiImageImplement must 'return list'. But 'return list' always run before UI picker show up!

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited July 17

    @Liêm_Nguyễn
    You have already created a method to return a list.

    public interface IPickMultiImageService
    {
        Task<List> PickPhotosAsync();
    }
    
    public class PickMultiImageImplement : IPickMultiImageService
    {
        public static List<string> list;
    
        Task<List> PickPhotosAsync();
        {
            return list;
        }
    }
    
  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:
    @Liêm_Nguyễn
    You have already created a method to return a list.

    public interface IPickMultiImageService
    {
      Task<List> PickPhotosAsync();
    }
    
    public class PickMultiImageImplement : IPickMultiImageService
    {
        public static List<string> list;
    
        Task<List> PickPhotosAsync();
        {
            return list;
        }
    }
    

    I have try your solution, list alway null. Only get the list at the second run time.
    I just a new guy so I can not understand Android.
    How do you think about OnActivityResult(int requestCode, Result resultCode, Intent data)???

  • JarvanJarvan Member, Xamarin Team Xamurai

    @Liêm_Nguyễn
    I could get the value from MainActivity.cs. Here is my demo, you can check it.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:
    @Liêm_Nguyễn
    I could get the value from MainActivity.cs. Here is my demo, you can check it.

    Your sample working good. But I'm using a plugin a litle bit different when get result in MainActivity
    Could you please try to implement BSImagePicker in Xamarin.Forms
    Thank!

  • JarvanJarvan Member, Xamarin Team Xamurai

    Ok, I'll have a try.

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @yelinzh said:
    Ok, I'll have a try.

    Thank you for spent time to help me!
    I waiting you!

Sign In or Register to comment.