我应该如何在涉及“ MainWindow”(C#)的WPF Windows之间传递数据? [英] How should I pass data between WPF Windows involving `MainWindow` (C#)?

查看:414
本文介绍了我应该如何在涉及“ MainWindow”(C#)的WPF Windows之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开始一个C#项目,在该项目中,我正在为一个简单的ATM机建模,因此将需要几个屏幕。在编写StockTicker棋盘游戏改编版本之前,我曾遇到过在屏幕之间传递数据的问题,但是我认为我的解决方法不是很好。

I am currently starting a C# project in which I am modelling a simple ATM machine, and will therefore need several screens. I have run into the problem of passing data between screens before when I was coding a StockTicker board game adaption, but I don't think my method of solving it was very good.

我的很多问题都源于我使用 MainWindow 作为应用程序的主窗口的事实。我可以轻松地将数据从辅助窗口传递回主窗口,因为在关闭 Window1 类之后,我可以访问它的属性。但是,要从 MainWindow 转移到 Window2 ,我必须向 Window2 使我能够传递必要的值。

A lot of my problems stem from the fact that I use MainWindow as the primary window of the application. I am able to easily pass data from the secondary window back to the primary window, as I am able to access the properties of the Window1 class after it has been closed. However, to tranfer from MainWindow to Window2, I have to add a parameter to the constructor of Window2 that will enable me to pass in the necessary values.

虽然这对于小型应用程序确实有效,但我认为这需要传递给 Window2 的更多内容会变得很漫长/混乱。另外,我根本不知道如何从 Window2 访问在 MainWindow 上声明的任何方法。由于 MainWindow 是主屏幕,因此我的大部分编码都在其类中。

While this does work for a small application, it is my opinion that this would get very long/messy for the more things that need to be passed to Window2. In addition, I do not know at all how to access any of the methods that I declare on MainWindow from Window2. Since MainWindow is the primary screen, most of my coding is in its class.

  • 如何访问我从放入 MainWindow 的方法Window2 ??
  • How can I access a method that I have put in MainWindow from Window2 ??

(为澄清起见,我花了一些时间环顾四周,却找不到明确的示例,具体涉及 MainWindow ,需要访问其中的变量/方法。



这是我的C#代码(我尝试使用清晰的变量名)。该程序的要旨是您可以在Form 1上存储一些数据,然后在单击按钮时将其传递到Form 2。在这里,可以按用户的意愿对其进行修改,然后在关闭表单2时可以从表单1再次对其进行访问。

(For clarification) I have spent some time looking around, and cannot find a clear example that specifically involves MainWindow, and needing to access variables/methods found in it.


Here is my C# code (I have tried to have clear variable names). The gist of the program is that you can have some data stored on Form 1, which then is passed to Form 2 when the button is clicked. From here, it can be modified however the user wishes, and then it can be accessed again from Form 1 when Form 2 is closed.

在此示例中,我只是表明我已经找到了一种从表格到表格之间传输数据的方法,但是我相信有更好的方法。具体来说,我确信有一种更好的方法,不必为 Window2 类编写具有不同参数的不同构造函数。



第一个WPF窗口代码:

In this example, I am merely showing that I have found a way to transfer data from form to form, but I am sure that there is a better way. Specifically, I am sure that there is a nicer way than having to possibly code different constructors with varying paramaters for the Window2 class.


First WPF Window Code:

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

        private void btnSwitchForm_Click(object sender, RoutedEventArgs e)
        {
            string strForm1Text = txtForm1TextBox.Text;

            Window2 newWindow = new Window2(strForm1Text);
            newWindow.ShowDialog();

            txtForm2TextBox.Text = newWindow.strForm2Text;
        }
    }
}

第二个WPF窗口代码:

Second WPF Window Code:

namespace Banking_System
{
    public partial class Window2 : Window
    {
        public string strForm2Text { get; set; }

        public Window2(string form1)
        {
            InitializeComponent();

            txtForm1.Text = form1;
        }

        private void btnSwitchBack_Click(object sender, RoutedEventArgs e)
        {
            strForm2Text = txtForm2TextBox.Text;
            this.Close();
        }
    }
}






我最好还是创建另一个WPF窗口,并在 MainWindow 加载后立即加载它,然后隐藏 MainWindow 并假装另一个窗口是主屏幕?因为那样的话,我可以像上面一样操作(当从 Window2 转移到 MainWindow 时)来访问属性。 。


Would I be better off to create another WPF window, and have it load as soon as the MainWindow loaded, and then hide MainWindow and "pretend" that the other window was the primary screen? Because then I could simply access the properties in the same manner that I do above (when transferring from Window2 to MainWindow).

即。当需要从另一个窗口访问变量/方法时,只需声明一个新的临时窗口并访问它的属性/方法即可:

ie. When needing to access a variable/method from another window, simply declare a new temporary window and access the properties/methods of it:

FakeMainWindow wndPrimary = new FakeMainWindow();
wndPrimary.strProperty= "test";
wndPrimary.methodCall();

这种方式对我的主要吸引力在于,我可以在另一个窗口中访问方法(如上图所示) ),而使用 MainWindow 作为我的主要表单时,则无法执行。

The primary attraction of this way for me is that I can access methods on another window (as illustrated above), which I cannot do while using MainWindow as my primary form.

摘要


  • 如何访问从 MainWindow 中放入的方法c $ c> Window2 ?

  • 创建另一个WPF窗口更好吗,并假装这是我的主屏幕,以便我可以轻松访问它属性?

  • 如果没有,是否有更好的方法使用 MainWindow
  • How can I access a method that I have put in MainWindow from Window2?
  • Is it better to create another WPF window, and pretend that it is my "primary screen" so that I can easily access its properties?
  • If not, is there a better way of working with MainWindow?

如果我缺少任何物品,请告诉我。谢谢!

If I'm missing anything, let me know. Thanks!

推荐答案

您实际上可以访问应用程序的主窗口,而无需在窗口之间传递任何引用。 Application.Current.MainWindow 返回在app.xaml中声明的主窗口的实例。它会返回一个 Window (您的主窗口是从该窗口派生的),因此您需要对其进行强制转换才能访问其成员。

You can actually access your application's main window without passing any references between windows. Application.Current.MainWindow returns the instance of the main window that is declared in app.xaml. It returns a Window (which your main window derives from) so you will need to cast it in order to access its members.

例如,您可以在第二个窗口中使用以下代码:

For example, you could use this code in your second window:

((MainWindow)Application.Current.MainWindow).txtForm1TextBox.Text = "Some text";






话虽如此,我建议您这样做对WPF设计模式的一些研究(特别是 MVVM )。

这篇关于我应该如何在涉及“ MainWindow”(C#)的WPF Windows之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆