【问题标题】:How to setup StartupUri in App.xaml, if the startup window is in a different project/library?如果启动窗口位于不同的项目/库中,如何在 App.xaml 中设置 StartupUri?
【发布时间】:2019-08-09 04:00:22
【问题描述】:

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

我想弄清楚如何在App.xaml 中定义StartupUri,即BrowserView.xaml 将是一个启动窗口。

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

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

我收到一个异常:

XamlObjectWriterException:指定的类名 'G_Hoover.Views.BrowserView' 与实际根实例类型不匹配 'System.Windows.Window'。删除 Class 指令或提供 通过 XamlObjectWriterSettings.RootObjectInstance 实例。

当我输入时:

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

我收到异常:

System.IO.IOException: '找不到资源 'g_hoover;g_hoover.views/browserview.xaml'。'

当我根据this approach 输入App.xaml.cs 时,我得到的结果相同:

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。我做错了什么?

【问题讨论】:

    标签: c# wpf xaml view startup


    【解决方案1】:

    您可以从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);
        }
    }
    

    【讨论】:

    • 实际上它起作用了,而且比我预期的要简单。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多