Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvvmCross: How to pass Android context down to MvxCommand?

Tags:

mvvmcross

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?

like image 792
user2395286 Avatar asked Dec 01 '22 19:12

user2395286


1 Answers

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.

like image 90
user2395286 Avatar answered Jan 10 '23 19:01

user2395286