点击Run
或按Ctrl + F5
运行我的程序时,是否可以根据某些检查条件打开不同的窗口?
即如果满足某些条件,我希望打开一个特定的窗口,但如果不是我想打开另一个窗口。
应该在打开任何窗口之前先检查
之类的情况if(File.Exists(<path-to-file>)
Open Window 1
else
Open Window 2
这可能吗?
答案 0 :(得分:56)
了解App.xaml
删除StartupUri="MainWindow.xaml"
添加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">
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();
}
}
答案 1 :(得分:7)
您可以使用App.xaml
启动您的应用程序,正如Nikhil Agrawal所说,动态更改StartupUri
。
但是,您仍然可以从public static void Main()
启动您的应用程序。只需删除StartupUri="MainWindow.xaml"
中的App.xaml
属性,将Program
类添加到包含Main
方法的项目中,然后转到项目属性并将启动对象设置为{ {1}}。
YourAssemblyName.Program
注意,[STAThread]
public static void Main(string[] args)
{
var app = new Application();
var mainWindow = new MainWindow();
app.Run(mainWindow);
}
是必需的。如果您需要自己的STAThreadAttribute
派生版本,例如默认情况下WPF项目如何创建派生Application
类,则可以使用App
中的Main
代替Application
。但是,如果您不需要它,您可以直接使用基础Application
类,并从项目中删除派生的类。
答案 2 :(得分:1)
在App.xaml
中,我们有一个Application
标记,其中包含StartupUri
属性。我想你应该在App.xaml.cs部分编写这段代码
public App()
{
// Your Code
}
并将StartUpUri
设置为所需的xaml文件。
答案 3 :(得分:0)
我知道这是一个很老的问题。但是我遇到了类似的问题,最近我在 WPF .NET Core 3.1 中使用依赖注入,感到有人会遇到类似的挑战,因此发布此答案
这是我的启动,您可以在其中设置条件启动窗口
protected override async void OnStartup(StartupEventArgs e)
{
await host.StartAsync();
var userService = host.Services.GetService<IUserRepository>();
var lastActiveUser = userService.LastActive();
if (lastActiveUser != null)
{
DefaultWindow = host.Services.GetRequiredService<MainWindow>();
DefaultWindow.Show();
}
else
{
DefaultWindow = host.Services.GetRequiredService<LoginWindow>();
DefaultWindow.Show();
}
base.OnStartup(e);
}
用于初始化主机的应用程序构造器
public App()
{
host = Host.CreateDefaultBuilder() // Use default settings
//new HostBuilder() // Initialize an empty HostBuilder
.ConfigureAppConfiguration((context, builder) =>
{
builder.SetBasePath(Directory.GetCurrentDirectory())
// Add other configuration files...
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Configuration = builder.Build();
}).ConfigureServices((context, services) =>
{
ConfigureServices(context.Configuration, services);
})
.ConfigureLogging(logging =>
{
// Add other loggers...
logging.ClearProviders();
logging.AddConsole();
}).Build();
}
依赖注入
private void ConfigureServices(IConfiguration configuration,
IServiceCollection services)
{
_services = services;
_services.Configure<AppSettings>(Configuration
.GetSection(nameof(AppSettings)));
_services.AddDbContext<SmartDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("SqlConnection"));
});
_services.AddScoped<IUserRepository, UserRepository>();
_services.AddScoped<ILoginDataContext, LoginDataContext>();
_services.AddTransient(typeof(MainWindow));
_services.AddTransient(typeof(LoginWindow));
}