【问题标题】:WPF + Windows Forms, open a second Form as child of MainWindow from the first FormWPF + Windows 窗体,从第一个窗体打开第二个窗体作为 MainWindow 的子窗体
【发布时间】:2016-08-25 15:28:53
【问题描述】:

可能我不会在标题中说清楚,如有必要,请更改它。现在我开始解释:

我在 WPF 中完成了一个程序(导致图形)。 MainWindow 用一个按钮打开第一个 Form,Form1 做一些事情,改变 MainWindow 的 UI,完成后应该打开一个 Form2,它还应该做一些改变 MainWindow 的 UI 的事情,但我不能通过Form1打开Form2。

这里有一些代码:

MainWindow.xaml.cs

internal static MainWindow main; //It allows me to put the Form1 in the same context in order to change the UI
private void start_button_Click(object sender, RoutedEventArgs e)
{
     Form1 frm = new Form1(this);
     frm.Show();
}

Form1.cs

//function that allow me to communicate with the MainWindow
private MainWindow main1 = null;

public Form1(MainWindow callingForm)
{
     main1 = callingForm as MainWindow;
     InitializeComponent();
}

//Load function
private void Form1_Load(object sender, EventArgs e)
{
     //do some stuff changing MainWindow UI
     Form2 frm = new Form2(main1); //no compilation error
     frm.Show();
}

现在,Form2 没有打开。我还尝试使用 Form1 调用 MainWindow 中的 void 但什么也没有。我必须做什么?注意:还需要使用 Form2 更改主窗口的 UI,我需要仅在 Form1 计算结束时打开 Form2。

【问题讨论】:

  • Form 是什么命名空间?看起来那是 Winforms 的?
  • 命名空间“Application__WPF”@lll
  • 您可以尝试将您对Form2 的函数调用包装在Dispatcher 中吗?如Dispatcher.BeginInvoke
  • 使用 Dispatcher 不起作用@lll

标签: c# wpf forms


【解决方案1】:
    public Form1(MainWindow callingForm)
    {
        main1 = callingForm as MainWindow;
        InitializeComponent();
        this.Load += Form1_Load;
    }

【讨论】:

  • 这会使整个 Form1 崩溃,它会自动加载
【解决方案2】:

如果您尝试从 MainWindow 打开一个窗口,我建议您使用events 来实例化该窗口。

从结构的角度来看,子视图对他的父母说:“嘿,我希望你打开这个表单”有点奇怪 - 但是创建并显示它的是 Form1。

我会这样做:

主窗口:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void start_button_Click(object sender, RoutedEventArgs e)
    {
        Form1 frm = new Form1();
        frm.MyEvent += Form1EventMethod;
        frm.Show();
    }

    private void Form1EventMethod(object sender, EventArgs e)
    {
        // You can add here what you need to change in the MainWindow.

        Form2 frm = new Form2();
        frm.Show();
    }
}

Form1:

public partial class Form1 : Window
{
    public event EventHandler MyEvent;

    public Form1()
    {
        InitializeComponent();
    }

    private void show_form_Click(object sender, EventArgs e)
    {
        if(MyEvent != null)
            MyEvent(this, new EventArgs());
    }
}

同样,如果您想在引发此事件时执行某些操作,您可以订阅的不仅仅是 MainWindow。此外,您可以将不同的参数传递给您的订阅者。您的观点将是分开的,不会相互依赖。

【讨论】:

  • 不错的尝试,很好的解释,但不起作用:( Form2 不显示
【解决方案3】:

我知道你的意思,你想从 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 对象。

抱歉,这篇文章很长,但希望对您有所帮助!如果您需要屏幕截图来向您展示它的工作原理,并且如果它有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多