[英]How should I pass data between WPF Windows involving `MainWindow` (C#)?
I am currently starting a C# project in which I am modelling a simple ATM machine, and will therefore need several screens.我目前正在启动一个 C# 项目,在该项目中我正在为一台简单的 ATM 机建模,因此需要几个屏幕。 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.
在编写 StockTicker 棋盘游戏改编代码之前,我遇到过在屏幕之间传递数据的问题,但我认为我的解决方法不是很好。
A lot of my problems stem from the fact that I use MainWindow
as the primary window of the application.我的很多问题都源于我使用
MainWindow
作为应用程序的主要 window 。 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.我能够轻松地将数据从辅助 window 传递回主 window,因为我可以在
Window1
class 关闭后访问它的属性。 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.但是,要从
MainWindow
转移到Window2
,我必须向Window2
的构造函数添加一个参数,这将使我能够传递必要的值。
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
.虽然这确实适用于小型应用程序,但我认为这对于需要传递给
Window2
的更多内容来说会变得很长/很混乱。 In addition, I do not know at all how to access any of the methods that I declare on MainWindow
from Window2
.此外,我根本不知道如何从
Window2
访问我在MainWindow
上声明的任何方法。 Since MainWindow
is the primary screen, most of my coding is in its class.由于
MainWindow
是主屏幕,我的大部分编码都在它的 class 中。
MainWindow
from Window2
??Window2
访问我在MainWindow
中放入的方法? (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. (澄清)我花了一些时间环顾四周,找不到一个明确的例子,具体涉及
MainWindow
,需要访问其中找到的变量/方法。
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.具体来说,我确信有一种更好的方法,而不是为
Window2
class 编写具有不同参数的不同构造函数。
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; } } }
Second WPF Window Code:第二 WPF Window 代码:
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(); } } }
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?我最好创建另一个 WPF window,并在
MainWindow
加载后立即加载,然后隐藏MainWindow
并“假装”另一个 window 是主屏幕? Because then I could simply access the properties in the same manner that I do above (when transferring from Window2
to MainWindow
).因为那时我可以简单地以与上面相同的方式访问属性(从
Window2
传输到MainWindow
时)。
ie. IE。 When needing to access a variable/method from another window, simply declare a new temporary window and access the properties/methods of it:
当需要从另一个 window 访问变量/方法时,只需声明一个新的临时 window 并访问它的属性/方法:
FakeMainWindow wndPrimary = new FakeMainWindow(); wndPrimary.strProperty= "test"; wndPrimary.methodCall();
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.这种方式对我的主要吸引力在于我可以访问另一个 window 上的方法(如上图所示),而在使用
MainWindow
作为我的主要形式时我无法做到这一点。
Summary概括
MainWindow
from Window2
?Window2
放入MainWindow
的方法?MainWindow
?MainWindow
? If I'm missing anything, let me know.如果我遗漏了什么,请告诉我。 Thanks!
谢谢!
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. Application.Current.MainWindow
返回在 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.它返回一个
Window
(您的主窗口源自该窗口),因此您需要对其进行强制转换才能访问其成员。
For example, you could use this code in your second window:例如,您可以在第二个窗口中使用此代码:
((MainWindow)Application.Current.MainWindow).txtForm1TextBox.Text = "Some text";
Having said that, I kindly suggest that you do some research into WPF design patterns (specifically MVVM ).话虽如此,我建议您对 WPF 设计模式(特别是MVVM )进行一些研究。
Pass the whole window通过整个窗口
this will be MainWindow这将是主窗口
Window2 newWindow = new Window2(this);
You can set the First window as the Second window's DataContext :您可以将第一个窗口设置为第二个窗口的 DataContext :
namespace Banking_System
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSwitchForm_Click(object sender, RoutedEventArgs e)
{
Window2 newWindow = new Window2(strForm1Text);
newWindow.DataContext = this;
newWindow.ShowDialog();
}
}
public partial class MainWindow2 : Window
{
public MainWindow2()
{
var window1 = this.DataContext;
}
}
} }
Additionally i would recommend that your Window would have it's own ViewModel set as it's DataContext which you can send to the other window.此外,我建议您的 Window 将它自己的 ViewModel 设置为可以发送到另一个窗口的 DataContext。 In this way you can also bind properties directly to both windows and not worry about updating them manually .
通过这种方式,您还可以将属性直接绑定到两个窗口,而不必担心手动更新它们。
create a static class & put static methods & or static properties that are common to all windows. create a static class & put static methods & or static properties that are common to all windows.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.