首页 新闻 会员 周边 捐助

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

0
悬赏园豆:5 [已关闭问题] 关闭于 2017-06-09 07:52
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Helper
{
    /// <summary>
    /// Interaction logic for frmMessage.xaml
    /// </summary>
    public partial class frmMessage : ModernDialog
    {
        public string ReturnMsg = "";
        private string msgTitle = "";
        private string msg1 = "";
        private string msg2 = "";
        private string msgType = "";
        private string msgDetail = "";
        public frmMessage(string msgTitle, string msg1, string msg2, string msgType, string msgDetail)
        {
            InitializeComponent();
            this.msgTitle = msgTitle;
            this.msg1 = msg1;
            this.msg2 = msg2;
            this.msgType = msgType;
            this.msgDetail = msgDetail;
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            try
            {
                ReturnMsg = "YES";
                this.DialogResult = true;
                this.Close();
            }
            catch (Exception ex)
            {
                //this.Cursor = Cursors.Default;
                string msg = Utility.UtilityHelper.GetFinalInnerException(ex).Message;
                Utility.UtilityHelper.WriteLog("frmMessage/btnYes_Click " + msg, Utility.UtilityHelper.eLogCategory.UI, System.Diagnostics.TraceEventType.Error);
                CommonHelper.CallMsgPopUp("Notice", CommonManager.GetMessage("30013"), "", "1", msg);
            }
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            try
            {
                ReturnMsg = "NO";
                this.DialogResult = true;
                this.Close();
            }
            catch (Exception ex)
            {
                //this.Cursor = Cursors.Default;
                string msg = Utility.UtilityHelper.GetFinalInnerException(ex).Message;
                Utility.UtilityHelper.WriteLog("frmMessage/btnNo_Click " + msg, Utility.UtilityHelper.eLogCategory.UI, System.Diagnostics.TraceEventType.Error);
                CommonHelper.CallMsgPopUp("Notice", CommonManager.GetMessage("30013"), "", "1", msg);
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                ReturnMsg = "OK";
                this.DialogResult = true;
                this.Close();
            }
            catch (Exception ex)
            {
                //this.Cursor = Cursors.Default;
                string msg = Utility.UtilityHelper.GetFinalInnerException(ex).Message;
                Utility.UtilityHelper.WriteLog("frmMessage/btnConfirm_Click " + msg, Utility.UtilityHelper.eLogCategory.UI, System.Diagnostics.TraceEventType.Error);
                CommonHelper.CallMsgPopUp("Notice", CommonManager.GetMessage("30013"), "", "1", msg);
            }
        }

        private void frmMessage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // define the dialog buttons
                this.Buttons = new Button[] { this.YesButton, this.NoButton, this.OkButton, this.CancelButton };
                this.YesButton.Click += new System.Windows.RoutedEventHandler(this.btnYes_Click);
                this.NoButton.Click += new System.Windows.RoutedEventHandler(this.btnNo_Click);
                this.OkButton.Click += new System.Windows.RoutedEventHandler(this.btnConfirm_Click);
                this.CancelButton.Click += new System.Windows.RoutedEventHandler(this.btnConfirm_Click);

                this.Title = msgTitle;
                this.lblMessage1.Text = msg1;
                this.lblMessage2.Text = msg2;
                if (msgType == "1")
                {
                    this.YesButton.Visibility = Visibility.Hidden;
                    this.NoButton.Visibility = Visibility.Hidden;
                    this.OkButton.Visibility = Visibility.Visible;

                }
                else if (msgType == "2")
                {
                    this.YesButton.Visibility = Visibility.Visible;
                    this.NoButton.Visibility = Visibility.Visible;
                    this.OkButton.Visibility = Visibility.Hidden; ;
                }
                if (msgDetail != "")
                {
                    CancelButton.Visibility = Visibility.Visible;
                    CancelButton.Content = msgDetail;
                }
            }
            catch (Exception ex)
            {
                string msg = Utility.UtilityHelper.GetFinalInnerException(ex).Message;
                Utility.UtilityHelper.WriteLog("frmMessage " + msg, Utility.UtilityHelper.eLogCategory.UI, System.Diagnostics.TraceEventType.Error);
                CommonHelper.CallMsgPopUp("Notice", CommonManager.GetMessage("30006"), "", "1", msg);
            }
        }
    }
}

点击确认时发生异常
异常信息如下:

Exception
System.InvalidOperationException: DialogResult can be set only after Window is created and shown as dialog.
at System.Windows.Window.set_DialogResult(Nullable`1 value)
at FirstFloor.ModernUI.Windows.Controls.ModernDialog.<.ctor>b__9_0(Object o)
at FirstFloor.ModernUI.Presentation.RelayCommand.OnExecute(Object parameter)
at FirstFloor.ModernUI.Presentation.CommandBase.Execute(Object parameter)
at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs rou



 

WPF
愤青愤青的主页 愤青愤青 | 初学一级 | 园豆:4
提问于:2016-10-31 14:12
< >
分享
所有回答(2)
0

你应该贴出来使用这个窗口的的代码,这个窗口展现的时候有两种方式 一种是 .show() 一种是.showdialog() 你要是用了前者就会出这个问题。

Darren.Dai | 园豆:329 (菜鸟二级) | 2016-10-31 17:45

string msgData = "";
Helper.frmMessage frm = new Helper.frmMessage(msgTitle, msg1, msg2, msgType, msgDetail);
bool? DialogResult = frm.ShowDialog();
msgData = frm.ReturnMsg;
return msgData;

我不觉得这个有什么问题

支持(0) 反对(0) 愤青愤青 | 园豆:4 (初学一级) | 2016-11-01 07:51

@愤青愤青: 还是有问题的

bool? DialogResult = frm.ShowDialog();
msgData = frm.ReturnMsg; 

这两句话就有问题了,你可以尝试着把底下那句话先注释试试。

 

WPF 取值一般不这么取,先学些一下MVVM再进行WPF开发吧,这样方向就错了。

支持(0) 反对(0) Darren.Dai | 园豆:329 (菜鸟二级) | 2016-11-01 09:11

@Darren.Dai: 不行

支持(0) 反对(0) 愤青愤青 | 园豆:4 (初学一级) | 2016-11-01 09:21

@愤青愤青: 没有抨击的意思,大家都是做技术的,别想这么多,我是上班前,一行行帮你看代码解决问题的。

方便的话要不把源码发一份 我帮你调试一下吧。

支持(0) 反对(0) Darren.Dai | 园豆:329 (菜鸟二级) | 2016-11-01 09:22

@Darren.Dai:

WPF 自学的

以前一直做Winform

自学点MVVM 或者MVC 

确实不懂

支持(0) 反对(0) 愤青愤青 | 园豆:4 (初学一级) | 2016-11-02 07:55
0

我猜应该是你继承ModernDialog的问题

jello chen | 园豆:7336 (大侠五级) | 2016-11-01 20:32

对的,是继承了ModernDialog

问一下怎么解决呢?

支持(0) 反对(0) 愤青愤青 | 园豆:4 (初学一级) | 2016-11-02 07:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册