C# WPF - 根据某些条件选择启动窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10276074/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:07:53  来源:igfitidea点击:

WPF - choose startup window based on some condition

c#.netwpfwpf-controls

提问by sai sindhu

When running my program by clicking Runor pressing Ctrl + F5, is it possible to open different windows based on some check condition?

通过单击Run或按运行我的程序时Ctrl + F5,是否可以根据某些检查条件打开不同的窗口?

I.e if some condition is satisfied I wish to open a particular window, but if its not I want to open another window.

即,如果满足某些条件,我希望打开一个特定的窗口,但如果不满足,我想打开另一个窗口。

It should be like before opening any window it should first check for the condition like

它应该像在打开任何窗口之前应该首先检查条件一样

if(File.Exists(<path-to-file>)
    Open Window 1
else
    Open Window 2

Is this possible?

这可能吗?

采纳答案by aifarfa

look into App.xaml

查看 App.xaml

remove StartupUri="MainWindow.xaml"

消除 StartupUri="MainWindow.xaml"

add Startup="Application_Startup"new event Handler

添加Startup="Application_Startup"新的事件处理程序

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

form code behind App.xaml.cs create Application_Startup like...

App.xaml.cs 后面的表单代码创建 Application_Startup 就像...

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        //add some bootstrap or startup logic 
        var identity = AuthService.Login();
        if (identity == null)
        {
            LoginWindow login = new LoginWindow();
            login.Show();
        }
        else
        {
            MainWindow mainView = new MainWindow();
            mainView.Show();
        }
    }

回答by Nikhil Agrawal

In App.xamlwe have an Applicationtag having StartupUriattribute. I think u should write this code in App.xaml.cs section

App.xaml我们有一个Application具有StartupUri属性的标签。我认为您应该在 App.xaml.cs 部分编写此代码

public App()
{
      // Your Code
}

and set StartUpUrito desired xaml file.

并设置StartUpUri为所需的 xaml 文件。

回答by Gqqnbig

You can use App.xamlto start up your application and, as Nikhil Agrawal said, change StartupUridynamically.

您可以使用App.xaml来启动您的应用程序,并且正如 Nikhil Agrawal 所说,StartupUri动态更改。

However, you can still start up your application from public static void Main(). Just delete the StartupUri="MainWindow.xaml"attribute in App.xaml, Add a Programclass to your project containing a Mainmethod, and then go to the project properties and set the startup object to YourAssemblyName.Program.

但是,您仍然可以从public static void Main(). 只需删除 中的StartupUri="MainWindow.xaml"属性App.xaml,将Program包含Main方法的类添加到您的项目中,然后转到项目属性并将启动对象设置为YourAssemblyName.Program

[STAThread]
public static void Main(string[] args)
{
    var app = new Application();
    var mainWindow = new MainWindow();
    app.Run(mainWindow);
}

Note, the STAThreadAttributeis required. If you need your own derived version of Application, such as how WPF projects create a derived Appclass by default, you can use that in the Mainin place of Application. But, if you don't need it, you can just use the base Applicationclass directly and remove the derived one from your project.

请注意,STAThreadAttribute是必需的。如果你需要你自己的衍生版本Application,比如如何WPF项目创建一个派生App默认类,你可以使用在Main代替Application。但是,如果您不需要它,您可以直接使用基Application类并从您的项目中删除派生类。