【问题标题】:WPF: How to start from a window in a different assemblyWPF:如何从不同程序集中的窗口开始
【发布时间】:2010-11-24 15:27:53
【问题描述】:

我用谷歌搜索了这个,但仍然无法正常工作

我有一个 WPF 应用程序并希望从位于不同程序集中的 Main.xaml 开始。两个程序集位于同一位置。

我该怎么做?我从 XAML 中取出 StartupUri 并尝试了这些和一些细微的变化:

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        StartupUri = new Uri("/CompanyName.VisualStudio.UI;CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml", UriKind.Relative);
        //StartupUri = new Uri(@"pack://application:,,,/ CompanyName.VisualStudio.UI;CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml");

    }

程序集的名称是“CompanyName.VisualStudio.UI”,命名空间是“CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml”

有什么想法吗?

【问题讨论】:

  • 我把它读作“WTF: ...” ;-)

标签: c# .net wpf vb.net xaml


【解决方案1】:

This article 提供了一个干净的 XAML-only 解决方案。

StartupUri="pack://application:,,,/assembly_name;component/path/file_name.xaml"

地点:

  • assembly_name 是引用程序集的名称,无扩展名
  • path 是组件所在的子文件夹;如果组件位于项目根目录,则省略该元素
  • file_name 是组件的文件名

例子:

pack://application:,,,/UI;component/CalculatorView.xaml
assembly - UI.dll
path - none (file at project root)
file_name - CalculatorView

pack://application:,,,/MyApp.UI;component/Views/CalculatorView.xaml
assembly - MyApp.UI.dll
path - Views
file_name - CalculatorView

pack://application:,,,/UI;component/Views/External/CalculatorView.xaml assembly - UI.dll
path - Views/External
file_name - CalculatorView 

【讨论】:

  • 第一次确实有效。另外,还有智能感知。干得好。
【解决方案2】:

我会检查你的包 URI。下面是我要尝试的uri。将“组件”视为项目中的根文件夹,在我放置“文件夹名称”的位置放置适当的文件夹名称,如果 Main.xaml 位于项目的根目录中,则将其删除。

StartupUri = new Uri(@"pack://application:,,,/CompanyName.VisualStudio.UI;component/FolderName/Main.xaml", UriKind.Absolute);

【讨论】:

    【解决方案3】:

    老问题,但this 也有帮助:

    void App_Startup(object sender, StartupEventArgs e)
            {
                MainWindow = new YourWindow(some, arguments);
                MainWindow.Show();
    }
    

    和我的 app.xaml:

    <Application
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="SDKSample.App"
      Startup="App_Startup" />
    

    并记住关于 ShutdownMode :如果你记得在关闭最后一个窗口之前打开新窗口,你应该很好

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      相关资源
      最近更新 更多