WPF:如何从不同程序集中的窗口启动

发布于 2024-10-04 01:21:51 字数 663 浏览 1 评论 0原文

我用谷歌搜索了这个,仍然无法让它工作

我有一个 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”

有什么想法吗?

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.

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

    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"

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭泣的笑容 2024-10-11 01:21:51

本文提供了一个干净的仅 XAML 解决方案。

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

其中:

  • assembly_name 是引用的程序集的名称,无扩展名
  • 路径是组件所在的子文件夹;如果组件位于项目根目录,则省略该元素
  • 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 

This article gives a clean XAML-only solution.

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

Where:

  • assembly_name is the name of the referenced assembly, sans extension
  • path is the subfolder in which the component resides; if the component is at the project root, this element is omitted
  • file_name is the file name of the component

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 
寒冷纷飞旳雪 2024-10-11 01:21:51

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

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

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.

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

蝶…霜飞 2024-10-11 01:21:51

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

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 :如果您记得在关闭最后一个窗口之前打开新窗口,您应该会很好

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:

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文