C# 主窗口打开两次(StartupUri、应用程序、加载屏幕)

C# 主窗口打开两次(StartupUri、应用程序、加载屏幕),c#,xaml,mainwindow,C#,Xaml,Mainwindow,我的申请有问题。我在App.xaml中选择了我的Loadscreen.xaml作为“StartupUri”。Loadscreen.xaml.cs包含一个progressbar,它运行到100%,然后关闭并打开主窗口。问题是,它在关闭Loadscreen后会打开主窗口两次。我的谬论是什么 App.xaml: StartupUri="Loadscreen.xaml" Startup="Application_Startup"> Loadscreen.xaml.cs: pub

我的申请有问题。我在App.xaml中选择了我的Loadscreen.xaml作为“StartupUri”。Loadscreen.xaml.cs包含一个progressbar,它运行到100%,然后关闭并打开主窗口。问题是,它在关闭Loadscreen后会打开主窗口两次。我的谬论是什么

App.xaml:

StartupUri="Loadscreen.xaml"
         Startup="Application_Startup">
Loadscreen.xaml.cs:

public void Timer_Tick(object sender, EventArgs e)
    {
        progressBar_Ladebalken.Value = i;
        label_Titel.Content = i + "%";
        Mouse.OverrideCursor = Cursors.Wait;

        if (i < 100)
        {
            i += 1;
        }
        else
        {
            i = 0;
            Mouse.OverrideCursor = null;
            Timer.Stop();

            Window W = new MainWindow();
            W.Show();

            this.Close();
        }

您正在打开两个
加载屏幕的实例:

  • 一个带有
    StartupUri=“Loadscreen.xaml”
  • 另一个来自
    Application\u Startup
    ,由于
    Startup=“Application\u Startup”
    而被调用

只要去掉
StartupUri=“Loadscreen.xaml”
这个问题就应该解决了。

谢谢,它成功了。但是,互斥体不再工作了。如果我再运行一次应用程序,则不会有错误消息表明该应用程序已在运行。@是否删除了
StartupUri
并保留
Startup
?是。但它现在似乎起作用了。。。我只是想解决一些问题。谢谢你们的帮助。令人惊讶的答案,我已经搜索了几个小时,因为我一直在mscorelib defaultbinder的某个地方得到这个愚蠢的空引用。。。在我通过DependencyInjection在构造函数中添加了2个服务之后,一直抛出该错误。我想我找错地方了,因为问题是StartupUri指向mainwindow.xaml的构造函数,可能没有这些注入。。拆下那条线就把它修好了。太高兴了!谢谢
public void Application_Startup(object sender, StartupEventArgs e)
    {
        bool Absicherung;
        Mutex Mutex = new Mutex(true, this.GetType().GUID.ToString(), out Absicherung);

        if (Absicherung)
        {
            Window W = new Loadscreen();
            W.Closed += (sender2, args) => Mutex.Close(); ;
            W.Show();
        }
        else
        {
            MessageBox.Show(FM_Mutex_Meldung, FM_Mutex_Titelleiste, MessageBoxButton.OK, MessageBoxImage.Information);
            Mutex.Close();
            Application.Current.Shutdown();
        }
    }