如何更改WPF应用程序的StartupUri?

发布于 2024-08-15 15:48:25 字数 1265 浏览 3 评论 0原文

我正在尝试修改 App.cs 并从代码后面加载 WPF XAML 文件,但它无法正常工作。

无论我尝试将 StartupUri 设置为什么,它都不会启动,程序会在此之后退出。

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        LoginDialog dlg = new LoginDialog();
        if (dlg.ShowDialog() != true)
            return;

        switch (dlg.ChoiceApp) { 
            case ChoiceApp.CustomerEntry:
                StartupUri = new Uri("/MyApp;component/Forms/CustomerEntry.xaml", 
                    UriKind.Relative);
                break;
            case ChoiceApp.VendorEntry:
                StartupUri = new Uri("/MyApp;component/Forms/VendorEntry.xaml", 
                    UriKind.Relative);
                break;
        }
    }
}

现在我什至进行了跟踪,发现 LoginDialog 工作正常并且正确返回值,但设置“StartupUri”不起作用。

我在反向汇编中检查了 App 的 DoStartup 方法在 OnStartup 之后被调用,因此从技术上讲,我的 StartupUri 必须加载,但事实并非如此,在 App.xaml 中,启动 uri 根本没有定义。

注意:已确认错误

我注意到 ShowDialog 设置了 Application.MainWindow,当对话框结束时,它将其设置回 null,并且由于此设置,在 OnStartup 或 Startup 事件中调用模态对话框后 StartupUri 不起作用。

没有关于无效 uri 或类似内容的错误或异常。

此方法无需在启动事件或 OnStartup 中调用 DialogBox 即可工作,我认为在此方法上调用 showdialog 会导致其主窗口被设置为过期窗口,并在此之后关闭。

I am trying to modify App.cs and load the WPF XAML files from code behind but its not working as it should.

No matter whatever I try to set as StartupUri it doesnt start, the program quits after this.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        LoginDialog dlg = new LoginDialog();
        if (dlg.ShowDialog() != true)
            return;

        switch (dlg.ChoiceApp) { 
            case ChoiceApp.CustomerEntry:
                StartupUri = new Uri("/MyApp;component/Forms/CustomerEntry.xaml", 
                    UriKind.Relative);
                break;
            case ChoiceApp.VendorEntry:
                StartupUri = new Uri("/MyApp;component/Forms/VendorEntry.xaml", 
                    UriKind.Relative);
                break;
        }
    }
}

Now I even did trace and found out that LoginDialog is working correctly and is returning values correctly but setting "StartupUri" does not work.

I checked in reverse assembly that DoStartup method of App gets called after OnStartup, so technically my StartupUri must load, but it doesnt, in App.xaml startup uri is not at all defined.

Note: Bug Confirmed

I noticed that ShowDialog sets Application.MainWindow and when dialog ends, it sets it back to null, and because of this setting StartupUri does not work after calling Modal Dialog in OnStartup or Startup event.

There is no error or exception about invalid uri or anything like that.

This method works without DialogBox being called in Startup event or OnStartup, i think calling showdialog on this method causes something like its mainwindow being set to expired window and it shuts down after this.

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

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

发布评论

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

评论(4

挖鼻大婶 2024-08-22 15:48:25

阿卡什,我在尝试实现像您一样的 LoginDialog 时遇到了这个问题。该对话框没有错误,但其行为是设计使然。

不是错误。默认的ShutdownMode为
应用程序是OnLastWindowClosed,所以
第一个窗口关闭后
您的应用程序将开始关闭
向下!更改为 OnExplicitShutdown 和
它会起作用,但你必须
管理关闭。

请参阅之前的 StackOverflow 问题:WPF ShowDialog 在第二次调用时立即返回 null

Akash, I ran into this exactly issue trying to implement a LoginDialog just like yours. The dialog does not have a bug, but rather the behavior is by design.

Not a bug. The default ShutdownMode of
Application is OnLastWindowClosed, so
as soon as the first window is closed
your application will start shutting
down! Change to OnExplicitShutdown and
it will work, but you'll have to
manage the shutdown.

See this previous StackOverflow question: WPF ShowDialog returns null immediately on second call

望喜 2024-08-22 15:48:25

您是否仍然在 XAML 中指定了 StartupUri?如果是这样,请将其删除并查看是否有帮助。MSDN 源代码

如果没有,您可能需要采用不同的方法:将对话框作为启动项,然后根据所选值打开另一个窗口。

Do you still have a StartupUri specified in the XAML? If so, remove it and see if that helps.MSDN Source

If not, you may need to approach this differently: have your Dialog as your startup, then from that point open another Window based on the selected value.

乖不如嘢 2024-08-22 15:48:25

不要重写 OnStartup() 方法,而是挂钩事件。

XAML 中

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

    </Application.Resources>
</Application>

在后面代码的

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        var rnd = new Random();

        if (rnd.NextDouble() > 0.5)
            StartupUri = new Uri("/SOTestWPF;component/Window1.xaml", UriKind.Relative);
        else
            StartupUri = new Uri("/SOTestWPF;component/Window2.xaml", UriKind.Relative);

    }

:这只是我的测试用例,我已经验证它是否正确执行(随机:D)

instead of overriding the OnStartup() method, hook into the event instead.

in the XAML

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

    </Application.Resources>
</Application>

in the code behind:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        var rnd = new Random();

        if (rnd.NextDouble() > 0.5)
            StartupUri = new Uri("/SOTestWPF;component/Window1.xaml", UriKind.Relative);
        else
            StartupUri = new Uri("/SOTestWPF;component/Window2.xaml", UriKind.Relative);

    }

This is only my test case and I have verified that it performs correctly (randomly :D)

风为裳 2024-08-22 15:48:25

只需尝试 OnStartup() :

StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);

Just try in OnStartup() :

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