我正在为WPF表单关闭实现
this MVVM模式,这也是在
blog中解释的,我收到System.InvalidOperationException,并显示错误消息“DialogResult只能在Window创建后显示为对话框”。当我尝试设置对话框结果关闭按钮命令:
DialogResult = true;@H_404_3@这是我的viewmodel:
class MainWindowviewmodel:INotifyPropertyChanged { private bool? dialogResult; public bool? DialogResult { get { return dialogResult; } set { if (value != this.dialogResult) { this.dialogResult = value; OnPropertyChanged("DialogResult"); } } } public string Text { get { return "Hello!"; } } void CloseCommandExecute() { this.DialogResult = true; }@H_404_3@这是XAML视图:
<Window x:Class="WpfApplication.Mvvm.Windowclosing.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication.Mvvm.Windowclosing" local:DialogCloser.DialogResult="{Binding DialogResult}" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:MainWindowviewmodel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="100"/> <RowDefinition Height="40"/> </Grid.RowDefinitions> <TextBlock Text="{Binding Text}" Grid.Row="0"/> <Button Grid.Row="1" Command="{Binding CloseCommand}">Close Me</Button> </Grid> </Window>@H_404_3@我在这里做错了什么?
使用ShowDialog()打开表单时,设置对话框结果才有效。当您尝试在使用Show()打开的表单上设置对话框结果时,会收到此错误。