代码之家  ›  专栏  ›  技术社区  ›  quinnapi

无法在WPF中设置对话结果

  •  28
  • quinnapi  · 技术社区  · 17 年前

    我在调用窗口中使用showDialog()显示一个WPF窗口。窗口将打开,并且如预期的那样是模态的。但是,在对话框窗口中的“确定”和“取消”按钮的单击事件中,我分别设置this.dialogresult=true(或false),但未设置该值。窗口按预期关闭,但DialogResult仍然为空。

    这是wpf中的错误吗?或者是因为无法设置DialogResult属性而没有引发异常?窗口不在浏览器中。

    调用窗口中的代码:

    Window2 win = new Window2();
    bool? result = win.ShowDialog();
    if (result.HasValue && result.Value) {
       //never gets here because result is always null
    }
    

    对话框窗口中的代码:

    this.DialogResult = true;
    
    9 回复  |  直到 7 年前
        1
  •  18
  •   CJBS    9 年前

    DialogResult 是一个可空布尔值。然而,你不必为了得到它的价值而去投它。

    bool? result = myWindow.ShowDialog();
    if (result ?? false)
    {
      // snip
    }
    

    这个??设置结果为空时返回的默认值。更多信息: Using Nullable Types (C# Programming Guide)

    至于原来的问题,我唯一看到和跟踪这个问题的时候是在设置对话结果和关闭窗口之间设置窗口的时候。不幸的是,我能提供的唯一建议是让你步进你的代码并检查操作的顺序。我相信我通过设置 对话结果 然后显式地关闭窗口。

        2
  •  11
  •   Carlo    17 年前

    首先要考虑它返回一个可为空的bool(bool?),因此为了比较它或将它设置为另一个变量,必须将它强制转换为一个常规bool

    bool result = (bool)myWindow.DialogResult;
    

    至于它是空的…我不明白为什么会这样,除非它在被设置为真或假之后以某种方式被设置为空。你能显示你的代码吗?

    编辑:

    你的代码对我来说很好,这是我在第二个窗口中看到的:

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }
    
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }
    

    在窗口1中:

    private void window1_Loaded(object sender, RoutedEventArgs e)
    {
        Window2 win = new Window2();
    
        bool? result = win.ShowDialog();
    
        if (result.HasValue && result.Value)
        {
            //it DID get here
        }
    }
    

    有什么大的区别吗?

        3
  •  7
  •   dodgy_coder    14 年前

    我刚刚 确切地 同样的问题,它似乎是由我重写onClosing()方法引起的。我需要重写onclosing()来停止用户通过close(x)按钮关闭模式窗口。

    当我注释掉onClosing()方法时,问题就消失了,而dialogResult返回的值是预期的true或false。

    这里的兴趣是我的按钮点击处理程序和关闭方法:

    private void AlternateButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
        buttonHasBeenClicked = true;
        this.Close();
    }
    
    private void DefaultButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
        buttonHasBeenClicked = true;
        this.Close();
    }
    
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        base.OnClosing(e);
        if (!buttonHasBeenClicked)
        {
            // Prevent the user closing the window without pressing one of the buttons.
            e.Cancel = true;
        }
    }
    
        4
  •  3
  •   Ehsan Zargar Ershadi    14 年前

    我也遇到过这个问题,唯一能解决这个问题的方法就是在我的类中使用这个代码:

    public new bool? DialogResult { get; set; }
    

    在设置好我的DialogResult之后,我就可以了!!(很奇怪的问题)。 这就是我使用的代码:

    cmdCancel = new RelayCommand(() => { DataContact.Reload(); this.DialogResult = false; this.Close(); });
    

    打开我的对话框:

    public static MessageBoxResult ShowQuestionYesNo(string message)
            {
                POLMessageBox w = new POLMessageBox("سوال", MessageBoxType.QuestionYesNo, message);
                w.ShowDialog();
                var b = w.DialogResult;
                if (b == true) return MessageBoxResult.Yes;
                if (b == false) return MessageBoxResult.No;
                return MessageBoxResult.No;
            }
    
        5
  •  2
  •   batzen    17 年前

    在设置DialogResult之前是否关闭窗口? 您应该发布按钮事件处理程序的全部内容。

        6
  •  0
  •   dracore    12 年前

    我也遇到了这个问题。原来我在if语句的大括号内设置了dialogresult,因此(看起来很奇怪)导致了错误。一旦这条线被移除,问题就解决了。

    private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(startBlockPosBox.Text))
            {
              .. do stuff ..
            }
            else
            {
              .. do stuff ..
              DialogResult = true; // this line caused the problem
            }
    
            DialogResult = true;
        }
    
        7
  •  0
  •   Kishore    10 年前

    我在对话框窗口页中有以下内容。(dialogwindow.xaml.cs)

       private void dlgWindowYesButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }
    
        private void dlgWindowNoButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    

    在调用页中,我使用了如下对话框窗口:

    dialogwindow dWinObj = new dialogwindow();
    if(dWinObj.ShowDialog().Value == true)
    {
      //perform the operation when the user clicks "Yes"
    }
    
        8
  •  0
  •   jlo-gmail    7 年前

    问题是由于表单的生命周期:

    对话事件 私有void\u loginviewmodel\u loginevent(对象发送者,loginviewmodel.logineventargs e) { dialogResult=true; 这个。关闭(); }

    作品:

    var login = new Login();
    var result = login.ShowDialog();
    

    不起作用:

    var result = new Login().ShowDialog();
    
        9
  •  -1
  •   Christopher Jordan    9 年前

    我有一个类似的问题,但我的问题来自我的结束语中的代码。我试图在窗口关闭之前释放一个列表,然后将列表属性设置为空…当我试图将set属性的值设置为null时,它被set属性阻塞了,因此我在set属性方法中提出了以下笨拙的解决方法,之后一切都正常:

        List<SettingItem> settingItems;
        public IEnumerable<SettingItem> Settings
        {
            get
            {
                return settingItems.OrderBy(t => t.Name).AsEnumerable();
            }
            set
            {
                if (value == null)
                {
                    settingItems.Clear();
                }
                else
                {
                    settingItems = value.ToList();
                }
            }
        }
    
    推荐文章