Setting DialogResult is some cases raises exception

不想你离开。 提交于 2020-01-06 15:29:35

问题


In my WPF application is some cases setting DialogResult = true raises an Exception telling that DialogResult cannot be set on unvisible window. Window is launched:

Window.ShowDialog()

Neither Window.Close() nor DialogResult is not called before. How to ensure that

DialogResult = true;

can be executed without raising error?

The exact exception content is:

DialogResult can be set only after Window is created and shown as dialog.

Here is the code

  // FormConstructor
    public FrmAccept()
    {
        InitializeComponent();

        vm = new AcceptViewModel();
        vm.CmdSubmit = new RelayCommand(pars => DoSubmit(), pars => vm.SubmitCanExecute);

        this.DataContext = vm;
    }


  private void DoSubmit()
    {
        try
        {
            if (!vm.IsGuiEnabled)
            {
                this.Close();
            }
            else
            {
                vm.IsGuiEnabled = false;
                bool isOk = DBervice.SaveDB(vm.Data);

                if (isOk)
                {
                    PrintData();  // This can cause an Exception
                }

                if (iOk)
                {
                    DialogResult = true;  // << === Here Exception Raises in Some Cases 
                    this.Close();
                }
                else
                {
                    vm.IsGuiEnabled = true;
                }
            }
        }
        catch (Exception ex)
        {
            vm.IsGuiEnabled = true;
        }
    }

And part of XAML

            <Button x:Name="btnAccept" 
                    Style="{StaticResource FlatButtonLarge}"
                    IsEnabled="{Binding Path=IsGuiEnabled}"
                    Height="42" 
                    Content="Submit">
                <Button.InputBindings>
                    <MouseBinding Gesture="LeftClick" Command="{Binding Path=CmdSubmit}" />
                </Button.InputBindings>
            </Button>

来源:https://stackoverflow.com/questions/35791956/setting-dialogresult-is-some-cases-raises-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!