如何更改 WPF 应用程序的 StartupUri? [英] How to change StartupUri of WPF Application?

查看:19
本文介绍了如何更改 WPF 应用程序的 StartupUri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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;
        }
    }
}

现在我什至做了跟踪,发现 LoginDialog 工作正常并且正确返回值,但设置StartupUri"不起作用.

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

我检查了反向程序集,App 的 DoStartup 方法在 OnStartup 之后被调用,所以从技术上讲,我的 StartupUri 必须加载,但它没有,在 App.xaml 启动 uri 根本没有定义.

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.

注意:错误已确认

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

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.

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

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

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

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.

推荐答案

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

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

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

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.

这篇关于如何更改 WPF 应用程序的 StartupUri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆