【问题标题】:WPF: I cannot reuse a window (custom massagebox) after it has been closedWPF:关闭后我无法重用窗口(自定义按摩框)
【发布时间】:2017-08-19 18:39:14
【问题描述】:

我需要为警报空字段创建一个自定义“消息框”。当一个字段为空(如客户名字、姓氏、地址……)时,我的消息框被加载。 但它只发生一次,另一次我给出以下错误:

PresentationFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

附加信息:在窗口关闭后无法设置可见性或调用 Show、ShowDialog 或 WindowInteropHelper.EnsureHandle。

enter image description here

enter image description here


winMessageAlert.xaml.cs

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.Shapes;
using DataModelLayer;
using SaleAndStorageSystems.Module;
using System.Text.RegularExpressions;
using System.Media;



namespace SaleAndStorageSystems.Windows
{
    /// <summary>
    /// Interaction logic for winUsers.xaml
    /// </summary>
    public partial class winMessageAlert : Window
    {
        public winMessageAlert()
        {
            InitializeComponent();
        }

        public string varTitle = "";
        public string varMessage = "";

        private void winMessageAlert1_Loaded(object sender, RoutedEventArgs e)
        {
            SystemSounds.Asterisk.Play();

            lblTitle.Content = varTitle;
            txtMessage.Text = varMessage;
        }
        private void recHeader_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}

winAddEditCustomer.xaml.cs

 winMessageInformation MywinMessageInformation = new winMessageInformation();
        winMessageAlert MywinMessageAlert = new winMessageAlert();

        SaleAndStorageSystemsEntities MyDatabase = new SaleAndStorageSystemsEntities();

        private bool CheckNullable()
        {
            if (string.IsNullOrEmpty(txtFirstName.Text.Trim()))
            {
                MywinMessageAlert.varTitle = "بدون مقدار";
                MywinMessageAlert.varMessage = "نام مشتری خالی می باشد";
                MywinMessageAlert.UpdateLayout();
                MywinMessageAlert.ShowDialog();

                txtFirstName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtLastName.Text.Trim()))
            {
                MywinMessageAlert.varTitle = "بدون مقدار";
                MywinMessageAlert.varMessage = "نام خانوادگی مشتری خالی می باشد";
                MywinMessageAlert.ShowDialog();

                txtLastName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtCellPhone.Text.Trim()))
            {
                MywinMessageAlert.varTitle = "بدون مقدار";
                MywinMessageAlert.varMessage = "تلفن مشتری خالی می باشد";
                MywinMessageAlert.ShowDialog();

                txtCellPhone.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtAddress.Text.Trim()))
            {
                MywinMessageAlert.varTitle = "بدون مقدار";
                MywinMessageAlert.varMessage = "آدرس مشتری خالی می باشد";
                MywinMessageAlert.ShowDialog();

                txtAddress.Focus();
                return false;
            }

            return true;
        }

【问题讨论】:

  • 代码由文本组成。将您的代码作为文本而不是图像链接发布。
  • 这类问题在 SO 中没有得到很好的回应。您必须在发布之前进行搜索,确切地知道您需要什么并发布您迄今为止尝试过的代码。不过,这不是一个坏问题。您还需要学习 C# 中的命名约定。我们以他们为荣!如果您发现,请投票并将答案标记为已接受。如果您需要更多帮助,请发表评论:)

标签: c# wpf


【解决方案1】:

欢迎来到 SO。

错误本身是完全清楚的。当用户关闭表单或通过调用Close() 关闭表单时,它会在后台处理(准确地说,hWnd 被破坏,但没关系),所以你不能再使用它了。

要多次使用一个页面,您需要在每次显示时都创建一个实例:

MywinMessageAllert = new winMessageAlert();

然后是你的其余代码。

Movafagh Baashid :)

【讨论】:

  • 非常感谢“亲爱的 EMAD” Damet Garm Dadash :-*
猜你喜欢
  • 2012-06-28
  • 2011-04-03
  • 2022-11-11
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多