问题
i recently started working with MVVM Light and just used the IDialogService for the first time.
My ViewModelLocator
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
// Register the dialog service provided by mvvm light
SimpleIoc.Default.Register<IDialogService, DialogService>();
My ViewModel
private IRestService _restService;
public OrderViewModel (IDialogService dialogService )
{
_dialogService = dialogService;
}
And finally using ShowMessage:
await _dialogService.ShowMessage("test", "test", "Ok", "Nop", (result) => {
if (result)
{
//...
}
else
{
//...
}
});
Which is causing this exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Context.getTheme()'
on a null object reference
Can someone tell me whats going on ?
回答1:
I think the problem is, that current activity has not been set. The problem is, that MvvmLight needs CurrentActivity
to be of type ActivityBase
. So i think the DialogService
not really compatible with Forms, because Forms requires the Activity to inherit from FormsAppCompatActivity
. That would make up a nice diamond shaped inheritance, which is not possible, because C# doesn't support multiple inheritance.
So you could use ACR User Daialogs: https://www.nuget.org/packages/Acr.UserDialogs/
See https://github.com/aritchie/userdialogs#android-initialization-in-your-main-activity for the Setup.
And you can register it
SimpleIoc.Default.Register<IUserDialogs>(UserDialogs.Instance);
来源:https://stackoverflow.com/questions/37051820/xamarin-forms-mvvm-light-idialogservice-showmessage-throwing-native-exception