简体   繁体   English

如何将应用程序控制传递到另一个WPF窗口?

[英]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.

相关问题 如何从另一个类或窗口访问 WPF 中的控件 - How can I access a control in WPF from another class or window 在 WPF 应用程序中关闭登录 Window 后,如何将已登录到另一个 window 的“用户 ID”传递? - How can I pass “User Id” which has logged in to another window after closing Login Window in WPF Application? 如何将WPF用户控件创建为dll,然后在另一个WPF应用程序中看到它? - How can I create WPF User Control as dll and then see it in another WPF application? 我可以将窗口的内容流式传输到WPF中的另一个控件吗? - Can I stream the contents of a window to another control in WPF? 如何在WPF应用程序的用户控件中放置Win32浮动窗口? - How can I position a Win32 floating window inside a user control in my WPF application? 如何在不打开另一个窗口的情况下将PowerPoint演示文稿嵌入到WPF应用程序中? - How can I embed a PowerPoint presentation into a WPF application without opening another window? 如何与WPF用户控件中托管的Delphi窗口通信? - How can I communicate with a Delphi window hosted in a WPF User Control? WPF:如何将线程中的文本框文本传递到主窗口? - WPF: How can I pass the text of textbox in a thread to the main window? 如何将WPF控件动态地更改为另一个? - How I can dynamically change a WPF control into another? 在WPF中如何控制是否单击了另一个按钮 - In WPF how can I control whether another button clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM