XAML wpf材料设计中如何更新dialoghost的内容?

lvjbypge  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(244)

当我在打开dialoghost的过程中打开新的用户控件到dialoghost时发生此错误:

当我按提交按钮这是我的代码xaml:
主窗口:

<Grid>

    <Button Content="Show"
            Command="{Binding OpenRegisCommand}"
            VerticalAlignment="Bottom"
            Margin="0 0 0 40"
            Foreground="White"
            Width="100"
            Background="#23A9F2">

    </Button>

    <md:DialogHost Identifier="RootDialogHostId">

    </md:DialogHost>

</Grid>

我的用户控件和主窗口使用1个视图模型:

public MainViewModel()
        {
            OpenRegisCommand = new DelegateCommand(OpenFormRegis);
            Submit = new DelegateCommand(Check);
        }

        private async void Check()
        {
            if(Name.Equals("admin")&&Pass.Equals("123456"))
            {
                var view = new DialogOk();
                await DialogHost.Show(view, DialogHostId);
            }
            else
            {
                var view = new DialogNo();
                await DialogHost.Show(view, DialogHostId);
            }

        }

        private async void OpenFormRegis()
        {
            var view = new FormRegis();
            await DialogHost.Show(view, DialogHostId);
        }

按钮提交在我的用户控件中绑定DelegateCommand提交在我的视图模型中

k5hmc34c

k5hmc34c1#

每个对话框主机可用于一次显示一个视图。
如果要同时拍摄两个视图(不太可能),则需要两个对话框主体。
在您的情况下,如果您试图在对话框宿主中打开“OpenFormRegis”窗口,我建议您使用Windows。

huus2vyu

huus2vyu2#

在我的案例中,我做了以下操作:
1-使用DialogHost.GetDialogSession(“RootDialog”)获取活动的对话框。
2-如果它不是null,则使用UpdateContent(alertBox)方法设置内容,该方法更新对话框的内容。
3-如果为空,则建立常规流以显示对话框的内容。

AlertBoxView alertBox = new AlertBoxView();
  alertBox.DataContext = this;

  var dialog = DialogHost.GetDialogSession(IdentifierDialog);

  if (dialog != null)
  {
      dialog.UpdateContent(alertBox);
  }
  else
  {
      await DialogHost.Show(alertBox, IdentifierDialog);
  }
  • 警报框视图:是我的DialogHost自定义视图
  • 标识符对话框:是获取对话框标识符的变量

相关问题