首页 > 解决方案 > 以编程方式显示对话框,然后在按下对话框按钮时将其处理

问题描述

我目前有一个 UserControl View 和 ViewModel ,它们基本上可以获取有关用户的一些数据。如果用户没有访问系统的权限,应用程序应该显示一个弹出框告诉用户这个信息——弹出框有一个“确定”按钮,然后处理对话框并返回到以前的用户控件。

我也在使用 Material Design 来做到这一点,但是从这里的文档中实现不是很清楚:https ://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Dialogs#dialoghostshow

我的实现如下:

对话框视图:

<UserControl x:Class="GiveUpRegister.Dialogs.MessageBoxView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d"
             Height="auto" Width="auto">

    <materialDesign:Card HorizontalAlignment="Center" materialDesign:ShadowAssist.ShadowDepth="Depth1">
        <DockPanel  Height="auto" Width="250">
            <TextBlock Text="{Binding TxtBlockTitle.Value}" FontWeight="Bold" DockPanel.Dock="Top" Margin="20,20,9,20">Example error title</TextBlock>
            <TextBlock Text="{Binding TxtBlockMessage.Value}" DockPanel.Dock="Top" Margin="20,5,20,10">Some error message.</TextBlock>
            <Button Margin="9,10,9,10" Style="{StaticResource MaterialDesignFlatButton}" DockPanel.Dock="Bottom">
                <Button.CommandParameter>
                    <system:Boolean>True</system:Boolean>
                </Button.CommandParameter>
                OK
            </Button>
        </DockPanel>
    </materialDesign:Card>
</UserControl>

对话框视图模型:

public class MessageBoxViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;


    private bool _isMessageBoxOpen;
    public bool IsMessageBoxOpen
    {
        get => _isMessageBoxOpen;
        set
        {
            if (_isMessageBoxOpen == value) return;
            _isMessageBoxOpen = value;
            OnPropertyChanged(nameof(IsMessageBoxOpen));
        }
    }

    private object _messageContent;
    public object MessageContent
    {
        get => _messageContent;
        set
        {
            if (_messageContent == value) return;
            _messageContent = value;
            OnPropertyChanged(nameof(MessageContent));
        }
    }


    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

如何调整此代码以便能够从以下代码初始化它:

启动视图模型:

private async Task LoadUserDetails()
{
    try
    {
        await Task.Factory.StartNew(() => SystemUserService = SystemUserService.Instance);
        bool hasAccess = SystemUserService.CheckAccessPermission();
        if (hasAccess)
        {
             // Show the dialog box
        }
    }
    catch (Exception ex)
    {
        Logger.SendError("Could not retrieve user details", ex);
    }
}

标签: c#wpfmvvmmaterial-design

解决方案


这看起来很有希望。我还没有尝试过,但这似乎会打开对话框,然后等待用户单击按钮。我在关闭策略下从他们的网站上获取了这个。

var result = await DialogHost.Show(myControl, delegate(object sender, 
DialogOpenedEventArgs args)
{
    args.Session.Close(false);
});

推荐阅读