如果启动 window 位于不同的 project/library,如何在 App.xaml 中设置 StartupUri?

How to setup StartupUri in App.xaml, if the startup window is in a different project/library?

下面是我的解决方案树,在那里你可以看到,App.xamlG_Hoover.Init 项目中,BrowserView.xaml window 在 G_Hoover.Views 项目中。

我想弄清楚如何在 App.xaml 中定义 StartupUriBrowserView.xaml 将是一家初创公司 window。

我找到的唯一方法 is here,但是当我输入 App.xaml:

StartupUri="C:/Users/asus/Desktop/IT/Programowanie/Projekty/G_Hoover/G_Hoover.Views/BrowserView.xaml"

我收到异常:

XamlObjectWriterException: Specified class name 'G_Hoover.Views.BrowserView' doesn't match actual root instance type 'System.Windows.Window'. Remove the Class directive or provide an instance via XamlObjectWriterSettings.RootObjectInstance.

当我输入时:

StartupUri="pack://application:,,,/G_Hoover;G_Hoover.Views/BrowserView.xaml">

我收到异常:

System.IO.IOException: 'Cannot locate resource 'g_hoover;g_hoover.views/browserview.xaml'.'

和我在 App.xaml.cs 中输入时得到的结果相同,基于 this approach:

public partial class App : Application

{
    protected override void OnStartup(StartupEventArgs e)
    {
        StartupUri = new Uri("pack://application:,,,/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ";G_Hoover.Views/BrowserView.xaml", UriKind.RelativeOrAbsolute);
        //or this: StartupUri = new Uri("pack://application:,,,/G_Hoover;G_Hoover.Views/BrowserView.xaml", UriKind.RelativeOrAbsolute);
        //or this: StartupUri = new Uri("C:/Users/asus/Desktop/IT/Programowanie/Projekty/G_Hoover/G_Hoover.Views/BrowserView.xaml");
    }
}

请注意 G_Hoover.Init 参考了 G_Hoover.Views。我做错了什么?

您可以从 App.xaml 中删除 StartupUri 并在 App.xaml.cs 中管理应用启动。

例如:

public partial class App : Application
{

    protected override void OnStartup(StartupEventArgs e)
    {
        var startupView = new G_Hoover.Views.BrowserView.xaml();
        startupView.ShowDialog();

        base.OnStartup(e);
    }
}