我知道你的意思,你想从 WPF 应用程序主窗口调用窗口窗体。我准备了这个测试程序来帮助您了解如何以编程方式实现这一目标。
考虑以下简单程序:
1) 将 2 个数字作为用户输入(使用 TextBoxes)。
2) 以自定义窗口形式显示结果,作为对计算按钮的响应而弹出。
当用户按下“计算”按钮(未在此答案中显示)时,程序会添加 2 数字,然后以自定义窗口形式(类似于对话框)显示结果。
假设这是您要查找的内容,这里是响应用户点击的 caculateclick 方法:
在 MainWindow.xaml.cs 中:
private void calculateClick(object sender, RoutedEventArgs e)
{
try
{
int leftHandSide = System.Int32.Parse(lhsOperand.Text); //user input
int rightHandSide = System.Int32.Parse(rhsOperand.Text); // lhsOperand and rhsOperand are just references to TextBoxes that takes user input
string result_txt=addValues(leftHandSide,rightHandSide).ToString(); // add two numbers that returns the result
//addValues method is not defined in my answer, it's just a method I use in this example to add 2 values together
CustomDialog.show(result_txt); // diplays result in a custom window form
}
catch (FormatException exception )
{
exception.ToString();
}
您需要做的就是为用户定义的表单创建一个类,然后在这个类中定义主窗口将从上面看到的 caculateClick() 方法调用的 show 静态方法 (我将向您展示代码,请继续阅读)。注意调用静态方法'CustomDialog.show(result_txt);'
CustomDialog 是一个分部类(就像您创建的 Form1 类一样)以用户定义的形式显示添加的结果,该表单将像一个对话框一样工作。 (我假设您知道使用 Visual Studio 中的设计视图设计表单的用户界面)。
这是自定义对话框的代码:
public partial class CustomDialog : Form // CustomDialog
{
static CustomDialog MsgBox; //used in show method as a reference
static DialogResult result; //retuned by show method
public CustomDialog()
{
InitializeComponent();
result = DialogResult.OK; //initialise the result according to the function of the button, here I only use the OK button to close the dialog }
public static DialogResult show (string calculationResult) // show static method called by the Main Window
{
MsgBox = new CustomDialog();
MsgBox.result_text.Text = calculationResult; // result_text is a textbox implemented in the form using design view
MsgBox.StartPosition = FormStartPosition.CenterParent; // center dialog in the middle of the parent (WPF application main window)
MsgBox.ShowDialog(); // displays the dialog
return result; // returns the result so the button event is handled
}
我们在CustomDialog类中定义了两个字段,
static CustomDialog MsgBox; static DialogResult result; MsgBox 对象用于名为“显示(字符串计算)”的静态方法中,以显示对话框并返回“结果”变量,即 DialogResult 对象。 (返回 DialogResult 以便我们可以在 TextBox 中看到结果并处理事件点击)。
重要提示:
为避免任何错误,使用以下方法维护表单至关重要,这些方法执行一些后台工作并处理对话框上的按钮单击和标签单击(即使您不打算使用它们,也可以将它们留空)。 还要确保 WPF 应用程序的“CustomDialog”和“MainWindow”在 SAME 命名空间中实现
考虑将以下方法添加到您的 CustomDialog 类中,以防遇到一些错误:
private void Form1_Load(object sender, EventArgs e) // the name of this method depends on what you name it when you first create the form
{
}
private void button1_Click(object sender, EventArgs e)
{
result = DialogResult.OK; // handle result returned by show() method, here I only used an OK button
MsgBox.Close(); //closes the dialogbox
}
private void label1_Click(object sender, EventArgs e)
{
}
设计说明:在设计视图中创建表单时要小心,必须在 Visual Studio 的“属性”选项卡的“杂项”部分中将拖入其中的按钮指定为“接受”按钮。另外请记住,我将 MessageBox、Dialogbox 和 Form 称为同一个实体。实际上,如果您仔细检查我的代码,'MsgBox' 静态变量是一个 CustomDialog 对象。
抱歉,这篇文章很长,但希望对您有所帮助!如果您需要屏幕截图来向您展示它的工作原理,并且如果它有帮助,请告诉我。