如何更改 WPF 应用程序的 StartupUri?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1945843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change StartupUri of WPF Application?
提问by Akash Kava
I am trying to modify App.cs and load the WPF XAML files from code behind but its not working as it should.
我正在尝试修改 App.cs 并从后面的代码加载 WPF XAML 文件,但它无法正常工作。
No matter whatever I try to set as StartupUri it doesnt start, the program quits after this.
无论我尝试将什么设置为 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;
}
}
}
Now I even did trace and found out that LoginDialog is working correctly and is returning values correctly but setting "StartupUri" does not work.
现在我什至做了跟踪,发现 LoginDialog 工作正常并且正确返回值,但设置“StartupUri”不起作用。
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.
我检查了反向程序集,App 的 DoStartup 方法在 OnStartup 之后被调用,所以从技术上讲,我的 StartupUri 必须加载,但它没有,在 App.xaml 启动 uri 根本没有定义。
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.
我注意到 ShowDialog 设置 Application.MainWindow 并且当对话框结束时,它将它设置回 null,并且由于这个设置 StartupUri 在 OnStartup 或 Startup 事件中调用 Modal Dialog 后不起作用。
There is no error or exception about invalid uri or anything like that.
没有关于无效 uri 或类似内容的错误或异常。
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.
此方法无需在启动事件或 OnStartup 中调用 DialogBox 即可工作,我认为在此方法上调用 showdialog 会导致其主窗口被设置为过期窗口之类的事情,并在此之后关闭。
采纳答案by Joel Cochran
Do you still have a StartupUri specified in the XAML? If so, remove it and see if that helps.MSDN Source
您是否仍然在 XAML 中指定了 StartupUri?如果是这样,请将其删除,看看是否有帮助。MSDN源码
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.
如果没有,您可能需要采用不同的方法:将 Dialog 作为您的启动项,然后根据所选值打开另一个窗口。
回答by tofutim
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.
Akash,我在尝试实现像您一样的 LoginDialog 时遇到了这个问题。该对话框没有错误,但行为是设计使然。
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.
不是错误。Application 的默认 ShutdownMode 是 OnLastWindowClosed,所以只要第一个窗口关闭,您的应用程序就会开始关闭!更改为 OnExplicitShutdown 它将起作用,但您必须管理关闭。
See this previous StackOverflow question: WPF ShowDialog returns null immediately on second call
请参阅上一个 StackOverflow 问题:WPF ShowDialog 在第二次调用时立即返回 null
回答by Alastair Pitts
instead of overriding the OnStartup() method, hook into the event instead.
不要覆盖 OnStartup() 方法,而是挂钩到事件中。
in the XAML
在 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)
这只是我的测试用例,我已经验证它执行正确(随机:D)
回答by jug
Just try in OnStartup() :
只需尝试 OnStartup() :
StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);