如何更改WPF应用程序的StartupUri?
我正在尝试修改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
阿卡什,我在尝试实现像您一样的 LoginDialog 时遇到了这个问题。该对话框没有错误,但其行为是设计使然。
请参阅之前的 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.
See this previous StackOverflow question: WPF ShowDialog returns null immediately on second call
您是否仍然在 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.
不要重写 OnStartup() 方法,而是挂钩事件。
XAML 中
在后面代码的
:这只是我的测试用例,我已经验证它是否正确执行(随机:D)
instead of overriding the OnStartup() method, hook into the event instead.
in the XAML
in the code behind:
This is only my test case and I have verified that it performs correctly (randomly :D)
只需尝试 OnStartup() :
Just try in OnStartup() :