MvvmCross: How to pass Android context down to Mvx

2019-03-21 19:47发布

问题:

I have created a service interface:

public interface IMessageDialogService
{
    void ShowDialog(object context,string title,string message,string buttonTitle);
}

I have implemented that interface on both Android and iOS. The context is only used on Android where an Android Context is needed to display a message dialog. I pass this interface into my ViewModel to be injected by IoC. My problem is in my platform independent ViewModel which calls a WebService and then handles the return value. It checks the return value for an error condition and needs to display a message dialog. iOS does not need any context to display a UIAlertView, but on Android how do I get a hold of an Android context to pass in as the first argument?

Is there an easier way to display a simple informational dialog from a ViewModel?

回答1:

After inspecting the source for the WebBrowserTask, it looks like I can always grab the current Activity by:

var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity> ().Activity;

so I don't have to pass it down, but have my implementation of IMessageDialogService on Android grab it and use it to display the message dialog.



标签: mvvmcross