C#WPF如何更新启动事件
当应用程序打开时,将显示带有加载消息的启动屏幕,然后登录窗口将在启动屏幕前弹出。我现在的问题是,当成功登录时,如何继续,登录窗口将消失,启动屏幕仍将显示不同的加载消息,然后启动屏幕将完全消失,最后将执行应用程序的主窗口?是否可以更新启动事件 以下是我在App.xaml.cs中的代码:C#WPF如何更新启动事件,c#,wpf,C#,Wpf,当应用程序打开时,将显示带有加载消息的启动屏幕,然后登录窗口将在启动屏幕前弹出。我现在的问题是,当成功登录时,如何继续,登录窗口将消失,启动屏幕仍将显示不同的加载消息,然后启动屏幕将完全消失,最后将执行应用程序的主窗口?是否可以更新启动事件 以下是我在App.xaml.cs中的代码: public partial class App : Application { protected override void OnStartup(StartupEventArgs e) {
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
SplashScreen splashScreen = new SplashScreen();
splashScreen.Show();
var startupTask = new Task(() =>
{
//Set custom message on screen
splashScreen.Dispatcher.BeginInvoke((Action)(() => splashScreen.LoadingMsg = "Please wait..."));
Thread.Sleep(500);
splashScreen.Dispatcher.BeginInvoke((Action)(() => splashScreen.LoadingMsg = "Please provide login information..."));
Thread.Sleep(300);
});
//When loading finished, show login window
startupTask.ContinueWith(t =>
{
Login loginWindow = new Login();
//When login window is loaded, the splash screen stays behind the login window
loginWindow.Loaded += (sender, args) => splashScreen.InvalidateVisual();
this.MainWindow = loginWindow;
//Show the login window in modal mode
loginWindow.ShowDialog();
}, TaskScheduler.FromCurrentSynchronizationContext());
startupTask.Start();
}
}
嗯,我会担心使用任务延续来编排视图事件 那么,您可以使用一种中介模式来获取各种屏幕的ViewModels,以响应其他ViewModels引发的事件,该模式如何? 例如:
你能把你的登录屏幕和启动屏幕结合起来吗?
using System;
using System.Collections.Generic;
namespace UnitTestProject2
{
public class ViewModel1
{
IMediator mediator;
public ViewModel1(IMediator mediator)
{
this.mediator = mediator;
}
public string UserId { get; set; }
public void Login(string userid)
{
this.UserId = userid;
this.mediator.RaiseEvent("LoggedIn", this.UserId);
}
}
public class VieowModel2
{
IMediator mediator;
public VieowModel2(IMediator mediator)
{
this.mediator = mediator;
this.mediator.ListenFor("LoggedIn", LoggedIn);
}
protected string UserId;
protected void LoggedIn(Object sender, EventArgs e)
{
UserId = sender.ToString();
}
}
public interface IMediator
{
void ListenFor(string eventName, EventHandler action);
void RaiseEvent(string eventName, object data);
}
}