简体   繁体   English

如何从另一个应用程序打开WPF窗口

[英]How to Open a WPF Window from Another Application

How can I call a WPF Window from another application? 如何从另一个应用程序调用WPF窗口? I would like to have a UI.Resources project with all windows, view models, and what I call "sequences" (standard class to control flow of windows). 我想要一个UI.Resources项目,其中包含所有窗口,视图模型以及所谓的“序列”(控制窗口流的标准类)。 Ultimately, I would call the proper Sequence for the UI process that I want to invoke ( Login , GetLocale , etc). 最终,我将为要调用的UI进程调用适当的Sequence( LoginGetLocale等)。 The "sequence" class would then create all resources, and handle showing and hiding the correct Windows to accomplish the task. 然后,“序列”类将创建所有资源,并处理显示和隐藏正确的Windows以完成任务。 Unfortunately, in the example below, the desired Window never shows. 不幸的是,在下面的示例中,所需的窗口从不显示。 The app just hangs on the ShowDialog() call: 该应用程序仅挂在ShowDialog()调用上:

public static bool Process(ClientLibrary client,
                   out Country country, out State state, out City city,
                   out string errorMessage)
{
    country = null;
    state = null;
    city = null;
    errorMessage = null;

    try
    {
        if (client == null) { errorMessage = "Internal Error: Client not supplied"; }

        var model = new LocaleSelectHeirarchyViewModel(client);
        var window = new LocaleSelectHeirarchyWindow(model);

        var result = window.ShowDialog();
        window.Close();
        window = null;

        if (result == null || !result.Value || model.SelectedCity == null)
        {
            return false;
        }

        country = model.SelectedCountry;
        state = model.SelectedState;
        city = model.SelectedCity;

        return true;
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);

        errorMessage = "Internal Error: Client threw an exception";

        return false;
    }
}

I just did something very similar. 我只是做了非常相似的事情。 I would like to share my approach that worked. 我想分享我的方法。 Here is the approach. 这是方法。 We will use System.Windows.Control.DockPanel in the XAML. 我们将在XAML中使用System.Windows.Control.DockPanel。 The dock panel can load different 'user controls' dynamically. 扩展面板可以动态加载不同的“用户控件”。 User control is very similar to a form, on which we can create UI elements. 用户控件与表单非常相似,我们可以在该表单上创建UI元素。 In your case, the contents of the dialogue that you want to show go in here. 在您的情况下,您要显示的对话的内容将进入此处。

Step 1 第1步

Place this DockPanel inside a grid. 将此DockPanel放置在网格内。

<DockPanel x:Name="mainDockPanel">          
</DockPanel>

Step 2 第2步

The next step is to create the different user controls. 下一步是创建不同的用户控件。 Right click on your WPF project in visual studio -> Add new User control 右键单击Visual Studio中的WPF项目->添加新的用户控件

Make sure that the xaml.cs code of the newly added user control has this 确保新添加的用户控件的xaml.cs代码具有此功能

public partial class myUserControlUC : UserControl

Essentially it should derive from System.Windows.Controls.UserControl Using the XAML of the user control we can create any fancy UI that we need. 从本质上讲,它应该派生自System.Windows.Controls.UserControl使用用户控件的XAML,我们可以创建所需的任何精美的UI。

Step 3 第三步

Now everything is ready, we just need to call 现在一切准备就绪,我们只需要致电

mainDockPanel.Children.Add(new myUserControlUC());

This can be put inside a switch to show different stuff upon different conditions For example 可以将其放在开关中,以在不同条件下显示不同的内容,例如

mainDockPanel.Children.Clear();
            switch (i)
            {
                case 1:                    
                    mainDockPanel.Children.Add(new ToolKitUC1());
                    break;
                case 2:                    
                    mainDockPanel.Children.Add(new ToolKitUC2());
                    break;
                case 3:                    
                    mainDockPanel.Children.Add(new ToolKitUC3());
                    break;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM