以编程方式显示对话框,然后在按下对话框按钮时处理它



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

我也在使用材料设计来实现这一点,但从这里的文档中还不太清楚实现: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));
}
}

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

StartupViewModel:

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);
}
}

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

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

我找到了一个在不阻塞调用UI线程的情况下等待对话框的解决方案。这远不是最好的解决方案,但它对我有效。如果有人能增强它…

您可以尝试此解决方案:是否有同步调用ShowDialogHost的方法?

它由调用以下方法的代码组成

methodMessageDialog(string message, System.Windows.Forms.MessageBoxButtons messageBoxButtons = System.Windows.Forms.MessageBoxButtons.OK)).

这个方法是异步的,等待用户按钮的应答,它将在不阻塞UI的情况下阻塞当前方法。

最新更新