[英]How can I pass application control to another WPF window?
I'm setting up a simple WPF application, which looks at its command-line arguments to determine what kind of window should be shown next. 我正在设置一个简单的WPF应用程序,它查看其命令行参数以确定接下来应该显示哪种窗口。 When that's determined, I show the next window by calling
new ApplicationWindow()
, set the content, and call Show()
. 当确定后,我通过调用
new ApplicationWindow()
显示下一个窗口,设置内容,并调用Show()
。 The problem is that the MainWindow
instance seems to have "application control" - ie when it closes, so does everything else. 问题是
MainWindow
实例似乎有“应用程序控制” - 即当它关闭时,其他一切都是如此。
It goes like this: 它是这样的:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TopBar.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF1975DD"));
this.ContentRendered += MainWindow_ContentRendered;
this.OperationModeSet += MainWindow_OperationModeSet;
}
[STAThread]
private void MainWindow_ContentRendered(object sender, EventArgs e)
{
Thread worker = new Thread(new ThreadStart(this.ParseCommandLineArgs));
worker.SetApartmentState(ApartmentState.STA);
worker.Start();
}
[STAThread]
public void ParseCommandLineArgs()
{
Thread.Sleep(3000);
string[] args = Environment.GetCommandLineArgs();
if (args.Any(item => item == "--server" || item == "-s"))
{
SetOperationMode(OperationMode.Server);
Dispatcher.BeginInvoke(new Action(delegate()
{
this.CloseWindow();
}));
}
else
{
SetOperationMode(OperationMode.Client);
Dispatcher.BeginInvoke(new Action(delegate()
{
this.CloseWindow();
}));
}
}
[STAThread]
private void SetOperationMode(OperationMode mode)
{
OperatingMode = mode;
if (OperationModeSet != null)
{
OperationModeSet(this, new OperationModeSetEventArgs(mode));
}
}
[STAThread]
private void MainWindow_OperationModeSet(object sender, OperationModeSetEventArgs e)
{
AppWindow window = new AppWindow();
if (e.Mode == OperationMode.Client)
{
this.CloseWindow();
window.Content = new ClientPage();
}
else if (e.Mode == OperationMode.Server)
{
this.CloseWindow();
window.Content = new ServerPage();
}
window.Show();
}
}
These methods get called in the order I've put them here, through various events. 这些方法按我通过各种事件将它们放在这里的顺序调用。 I've omitted a few fields and properties.
我省略了一些字段和属性。
The problem is that when this MainWindow
closes, so does window
- the instantiated ApplicationWindow
. 问题是当
this MainWindow
关闭时, window
也是如此 - 实例化的ApplicationWindow
。 I assume this is because the MainWindow
created it. 我认为这是因为
MainWindow
创建了它。
However, I do want to be able to close the MainWindow
and continue with another window as the "main" window - so how can I decouple the instantiated ApplicationWindow
from its parent MainWindow
so it continues on? 但是,我确实希望能够关闭
MainWindow
并继续使用另一个窗口作为“主”窗口 - 那么如何将实例化的ApplicationWindow
与其主MainWindow
分离,以便继续?
I've seen setting Application.MainWindow in App.xaml changes the main window - but I have no reference to the instantiated window that I can put into a static XAML file. 我已经看到在App.xaml中设置Application.MainWindow会更改主窗口 - 但是我没有引用可以放入静态XAML文件的实例化窗口。
Why are you parsing the command line args in your MainWindow? 为什么要解析MainWindow中的命令行参数?
You could just remove the StartupUri in the App.xaml and override the OnStartup method. 您可以删除App.xaml中的StartupUri并覆盖OnStartup方法。 Then you can use StartUpArgs to decide which operating mode you want.
然后,您可以使用StartUpArgs来决定所需的操作模式。
In App.xaml.cs 在App.xaml.cs中
protected override void OnStartup(StartupEventArgs e)
{
// Decide which window to show here
// Add bounds checks etc.
if (e.Args[0] == "-s")
{
var window = new ServerPage();
window.Show();
}
else
{
var window = new ClientPage();
window.Show();
}
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
base.OnStartup(e);
}
What I think you could do (now there are better options I'm sure...) is instead of creating a new window in your main program, move your other code into a new project and in your main project, launch it as a new process with Process.Start(...)
. 我认为你能做什么(现在有更好的选择我敢肯定......)而不是在你的主程序中创建一个新窗口,将你的其他代码移动到一个新项目中,在你的主项目中,将其作为一个新项目启动
Process.Start(...)
新流程。 I've only ever seen code that used this though, never written it from scratch myself. 我只看过使用过它的代码,但我自己从未写过。 But I would take a look at this page from the MDSN and pages related to it.
但我会从MDSN和与之相关的页面看一下这个页面 。
Excuse the lack of example code to help you, this is just at the edge of my knowledge and I'd hate to give you incorrect code. 请原谅缺乏示例代码来帮助你,这只是我所知的边缘,我不想给你错误的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.