WPF:我关闭后无法重复使用窗口(自定义按摩盒)

时间:2017-03-27 07:06:38

标签: c# wpf

我需要创建一个自定义"消息框"警报空字段。如果字段为空(如客户名字,姓氏,地址,...),则会加载我的消息框。 但它只发生一次,其他时间我发出以下错误:

类型' System.InvalidOperationException'的例外情况发生在PresentationFramework.dll中但未在用户代码中处理

附加信息:窗口关闭后,无法设置可见性或调用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;
        }

1 个答案:

答案 0 :(得分:1)

欢迎来到SO。

错误本身就完全清楚了。当用户关闭某个表单或在其上调用Close()时,它会在后台进行处理(hWnd会被破坏,但这并不重要)所以你不能再使用它了。 / p>

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

MywinMessageAllert = new winMessageAlert();

然后是你的其余代码。

Movafagh Baashid:)