【发布时间】:2017-08-19 18:39:14
【问题描述】:
我需要为警报空字段创建一个自定义“消息框”。当一个字段为空(如客户名字、姓氏、地址……)时,我的消息框被加载。 但它只发生一次,另一次我给出以下错误:
PresentationFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理
附加信息:在窗口关闭后无法设置可见性或调用 Show、ShowDialog 或 WindowInteropHelper.EnsureHandle。
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# 中的命名约定。我们以他们为荣!如果您发现,请投票并将答案标记为已接受。如果您需要更多帮助,请发表评论:)