我一直在关注Prism 7.2.0.1367 release notes.我能够使用dialogService.ShowDialog(NavigationStrings.MyViewModel)
导航到对话框ViewModel。但是,当我关闭它时,我在RequestClose上得到一个空引用异常。
在我的Xamarin Forms项目中,我有一个像这样的ViewModel:
public class MyViewModel : BaseViewModel, IDialogAware, IAutoInitialize
{
private DelegateCommand _closeCommand;
public DelegateCommand CloseCommand => _closeCommand ?? (_closeCommand = new DelegateCommand(Close));
public event Action<IDialogParameters> RequestClose;
public MyViewModel(INavigationService navigationService) : base(navigationService) { }
public override void OnAppearing()
{
base.OnAppearing();
}
private void Close()
{
RequestClose(null);
}
public bool CanCloseDialog() => true;
public void OnDialogClosed()
{
Console.WriteLine("The Demo Dialog has been closed...");
}
public void OnDialogOpened(IDialogParameters parameters)
{
// No need to do anything as IAutoInitialize will take care of what we need here...
}
}
我应该将RequestClose设置为阻止它为null吗?没有文档说明必须设置此事件。
最佳答案
RequestClose
是null
,因为您从未订阅此事件。
您应该在调用RequestClose
之前测试RequestClose(null)
是否为null。
如果要使用自定义行为来处理关闭,可以订阅此事件。
通过检查 Action 是否为null进行编辑:
public class MyViewModel : BaseViewModel, IDialogAware, IAutoInitialize
{
private DelegateCommand _closeCommand;
public DelegateCommand CloseCommand => _closeCommand ?? (_closeCommand = new DelegateCommand(Close));
public event Action<IDialogParameters> RequestClose;
public MyViewModel(INavigationService navigationService) : base(navigationService) { }
public override void OnAppearing()
{
base.OnAppearing();
}
private void Close()
{
RequestClose?.Invoke(null);
}
public bool CanCloseDialog() => true;
public void OnDialogClosed()
{
Console.WriteLine("The Demo Dialog has been closed...");
}
public void OnDialogOpened(IDialogParameters parameters)
{
// No need to do anything as IAutoInitialize will take care of what we need here...
}
}
关于mvvm - 如何针对Xamarin Forms在Prism 7.2.0.1367的IDialogAware中使用RequestClose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578022/