mvvm - 如何针对Xamarin Forms在Prism 7.2.0.1367的IDialogAware中使用RequestClose

标签 mvvm xamarin.forms prism

我一直在关注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吗?没有文档说明必须设置此事件。

最佳答案

RequestClosenull,因为您从未订阅此事件。
您应该在调用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/

相关文章:

Unity 2.0 中的 XML 配置更改

javascript - 将项目添加到 knockout View 模型中,而不是更新 View

xamarin - 如何在 Xamarin.Forms 中创建登录页面?

wpf - WPF与PRISM 4.0和Unity的工作示例

c# - 发现 LAN 上的所有设备

android - 使用自定义渲染器强制重绘 Xamarin.Forms View

c# - 当 Prism 中的另一个属性发生变化时更新一个属性

wpf - 在 Silverlight 中连接事件处理程序的 MVVM 方法

c# - WPF : Custom Datagrid inside combobox

c# - ComboBox SelectionChanged 在 MVVM 中