Xamarin.Forms MVVM Light IDialogService ShowMessage throwing native exception

↘锁芯ラ 提交于 2020-02-04 11:01:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!