我最近开始使用MVVM Light,并且第一次使用IDialogService。

我的ViewModelLocator

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

// Register the dialog service provided by mvvm light
SimpleIoc.Default.Register<IDialogService, DialogService>();

我的ViewModel
private IRestService _restService;

public OrderViewModel (IDialogService dialogService )
{
   _dialogService = dialogService;
}

最后使用ShowMessage:
await _dialogService.ShowMessage("test", "test", "Ok", "Nop", (result) => {
   if (result)
   {
      //...
   }
   else
   {
      //...
   }
});

导致此异常的原因:
Java.Lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Context.getTheme()'
on a null object reference

有人可以告诉我怎么回事吗?

最佳答案

我认为问题是尚未确定当前 Activity 。问题是,MvvmLight需要CurrentActivityActivityBase类型。所以我认为DialogService与Forms并不真正兼容,因为Forms要求Activity从FormsAppCompatActivity继承。那将构成一个很好的菱形继承,这是不可能的,因为C#不支持多重继承。

因此,您可以使用ACR用户Daialog:https://www.nuget.org/packages/Acr.UserDialogs/

有关设置,请参见https://github.com/aritchie/userdialogs#android-initialization-in-your-main-activity

您可以注册

SimpleIoc.Default.Register<IUserDialogs>(UserDialogs.Instance);

08-25 08:31