WPF:如何从不同程序集中的窗口开始

时间:2021-09-17 13:58:33

I googled this and still can't get it working

我用谷歌搜索了一下,还是无法让它正常工作

I have a WPF app and want to start from Main.xaml which is located in a different assembly. Both assemblies are in the same location.

我有一个WPF的应用,想从Main开始。xaml位于不同的程序集中。两个程序集都在同一个位置。

How can I do this? I took out the StartupUri from the XAML and tried with these and some slight variations:

我该怎么做呢?我从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");

    }

The name of the assembly is "CompanyName.VisualStudio.UI" and the namespace is "CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml"

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

Any ideas?

什么好主意吗?

3 个解决方案

#1


37  

This article gives a clean XAML-only solution.

本文给出了一个纯xam的解决方案。

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

Where:

地点:

  • assembly_name is the name of the referenced assembly, sans extension
  • 程序集名称是引用程序集的名称,sans扩展名
  • path is the subfolder in which the component resides; if the component is at the project root, this element is omitted
  • path是组件所在的子文件夹;如果组件位于项目根目录,则省略此元素
  • file_name is the file name of the component
  • file_name是组件的文件名

Examples:

例子:

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


3  

I'd check your pack URI. Below is the uri I'd try. Think of 'component' as the root folder in your project and where I've put 'FolderName' put the appropriate folder name or remove it if Main.xaml is in the root of the project.

我会检查你的背包URI。下面是我要尝试的uri。可以将“component”视为项目中的根文件夹,并将“FolderName”放入相应的文件夹名,如果是Main,则将其删除。xaml是项目的根。

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

StartupUri =新的Uri(@”包:/ /应用程序:、、/ CompanyName.VisualStudio.UI;组件/ FolderName /主要。xaml”,UriKind.Absolute);

#3


2  

Old question, but this is also helpful:

老问题,但这也很有帮助:

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

and i app.xaml:

我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" />

and rememeber about ShutdownMode : if you remember to open new window before closing last one you should be good

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

#1


37  

This article gives a clean XAML-only solution.

本文给出了一个纯xam的解决方案。

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

Where:

地点:

  • assembly_name is the name of the referenced assembly, sans extension
  • 程序集名称是引用程序集的名称,sans扩展名
  • path is the subfolder in which the component resides; if the component is at the project root, this element is omitted
  • path是组件所在的子文件夹;如果组件位于项目根目录,则省略此元素
  • file_name is the file name of the component
  • file_name是组件的文件名

Examples:

例子:

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


3  

I'd check your pack URI. Below is the uri I'd try. Think of 'component' as the root folder in your project and where I've put 'FolderName' put the appropriate folder name or remove it if Main.xaml is in the root of the project.

我会检查你的背包URI。下面是我要尝试的uri。可以将“component”视为项目中的根文件夹,并将“FolderName”放入相应的文件夹名,如果是Main,则将其删除。xaml是项目的根。

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

StartupUri =新的Uri(@”包:/ /应用程序:、、/ CompanyName.VisualStudio.UI;组件/ FolderName /主要。xaml”,UriKind.Absolute);

#3


2  

Old question, but this is also helpful:

老问题,但这也很有帮助:

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

and i app.xaml:

我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" />

and rememeber about ShutdownMode : if you remember to open new window before closing last one you should be good

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