iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
Hello, support team,
I created an AndroidMobileComponentServiceBase and custom service class in Android project to recieved the ViewModel message, but how can I raise event or execute public method in Android Fragment from this service class? (This Android fragment already created)
thanks'
Please try to use following snippet code to execute method in Android Fragment.
MyFragment fragment = AndroidUtility.GetViewContext(this.Owner as IViewModel) as MyFragment; if (fragment != null) fragment.DoSomethingInFragment();
Hope this helps.
Hi William,
I'm sure that you're already aware that each ViewModel represents a View, in this case, the Fragment is the View that consumes that your ViewModel.
So basically, in the ViewModel, you may call your custom service in the following fashion:
ICustomService customService = ServiceProvider.GetService<ICustomService>();if (customService != null){ customService.SetOwner(this); customService.ExecuteCustomMethod(); }
So, in your custom Android service, you can obtain the Context in the following fashion:
Context context = this.DispatchContext(this.Owner);
This is the whole key to the process. In Android, the Context object can be of three types: Activity, Fragment, or Global Application Context. Since you've associated the ViewModel to the Fragment, you've actually obtained the Fragment in the Context object, so you can easily safe-cast it to execute your public method in the Fragment.
MyFragment fragment = context as MyFragment; if (fragment != null) { fragment.ExecuteMyPublicMethod(); }
Hello,
Sorry I didn't mention the Fragment class is inherited "Intersoft.Crosslight.Android.Fragment<customViewModel>". Error message "Cannot convert type 'customViewModel' to 'Intersoft.Crosslight.Android.Fragment<Prototype.Ems.ViewModels.customViewModel>' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion" occurred when I use safe-cast statment on design time.
It's ok when I code below:
customViewModel context = Owner as customViewModel;
It seemed error when I code below on runtime:
Fragment<customViewModel> customFragment = context.Context as Fragment<customViewModel>;
I still can't grab the Fragment<customViewModel> to start my Android AnimationDrawable object, thank you very much.
Glad to hear that the snippet code helps.
Should you need further assistance or run into any problems regarding our controls, feel free to post it into our forum. We would be happy to assist you again.
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname