C# 添加登录视图
我想在我的WPF应用程序中添加一个登录视图。在应用程序中,我有一个主窗口,用于保存/控制我的用户控件之间的流 我将登录用户控件添加到应用程序中,并在我的主窗口中显示此流:C# 添加登录视图,c#,wpf,C#,Wpf,我想在我的WPF应用程序中添加一个登录视图。在应用程序中,我有一个主窗口,用于保存/控制我的用户控件之间的流 我将登录用户控件添加到应用程序中,并在我的主窗口中显示此流: public partial class MainWindow : Window, INotifyPropertyChanged{ private async void Window_Loaded(object sender, RoutedEventArgs e) { Thread.Cu
public partial class MainWindow : Window, INotifyPropertyChanged{
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
SINSessionViewModel vm = null;
vm = new SINSessionViewModel(dialogProvider, authenticatedClient);
vm.CreateSession.Execute(SessionPurpose.Clinic);
ViewModel = vm;
ViewModel_LifecycleChanged(vm, EventArgs.Empty);
}
private Dictionary<SessionLifecycle, Func<SINSessionViewModel, Control>> LifecycleViews = new Dictionary<SessionLifecycle, Func<SINSessionViewModel, Control>>()
{
{ SessionLifecycle.Login, vm => {
Control c = new LoginView(vm.Lifecycle);
c.DataContext = vm;
return c;
} },
{ SessionLifecycle.Setup, vm => {
Control c = new ClinicSINSessionView();
c.DataContext = vm;
return c;
} },
{ SessionLifecycle.Testing, vm => new SINListView() { DataContext = vm.CurrentListViewModel } },
{
SessionLifecycle.Finished, vm => {
vm.ExportAsCsvAsync();
Logger.Singleton.SessionComplete();
return new SessionResultView() { DataContext = vm };
}
}
};
}
class SINSessionViewModel{
public SessionLifecycle Lifecycle
{
get { return _lifecycle; }
private set
{
_lifecycle = value;
if (LifecycleChanged != null) { LifecycleChanged(this, EventArgs.Empty); }
}
}
//some other actions which can change the value of Lifecycle
}
void ViewModel_LifecycleChanged(object sender, EventArgs e)
{
var requestedView = LifecycleViews[ViewModel.Lifecycle](ViewModel);
MainView.Content = requestedView;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsInSession)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CanRecoverSession)));
}
public分部类主窗口:窗口,INotifyPropertyChanged{
已加载专用异步无效窗口(对象发送方,RoutedEventArgs e)
{
Thread.CurrentThread.CurrentUICulture=Thread.CurrentThread.CurrentCulture;
SINSessionViewModel vm=null;
vm=新的SINSessionViewModel(dialogProvider、authenticatedClient);
CreateSession.Execute(SessionPurpose.Clinic);
ViewModel=vm;
ViewModel\u LifecycleChanged(vm,EventArgs.Empty);
}
private Dictionary LifecycleViews=新字典()
{
{SessionLifecycle.Login,vm=>{
控制c=新登录视图(vm.Lifecycle);
c、 DataContext=vm;
返回c;
} },
{SessionLifecycle.Setup,vm=>{
控件c=新的ClinicSINSessionView();
c、 DataContext=vm;
返回c;
} },
{SessionLifecycle.Testing,vm=>new SINListView(){DataContext=vm.CurrentListViewModel},
{
SessionLifecycle.Finished,vm=>{
ExportAsCsvAsync();
Logger.Singleton.SessionComplete();
返回新的SessionResultView(){DataContext=vm};
}
}
};
}
类SINSessionViewModel{
公共会话生命周期
{
获取{return\u lifecycle;}
专用设备
{
_生命周期=价值;
if(LifecycleChanged!=null){LifecycleChanged(this,EventArgs.Empty);}
}
}
//其他一些可以改变生命周期价值的操作
}
因此,我需要为authenticatedClient LoginView设置一个适当的值,并将SessionLifecycle更改为SessionLifecycle.Setup以启动应用程序。如果有人能帮助我如何在MainWindow、LoginView和SINSessionViewModel之间传递参数,我将不胜感激。因此我找到了解决方案:
首先,由于主窗口包含LoginView,LoginView可以访问SINSessionViewModel,因此我需要做的是对上述代码进行如下更改:
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
SINSessionViewModel vm = null;
vm = new SINSessionViewModel(dialogProvider);
vm.CreateSession.Execute(SessionPurpose.Clinic);
ViewModel = vm;
ViewModel_LifecycleChanged(vm, EventArgs.Empty);
}
private Dictionary<SessionLifecycle, Func<SINSessionViewModel, Control>> LifecycleViews = new Dictionary<SessionLifecycle, Func<SINSessionViewModel, Control>>()
{
{ SessionLifecycle.Login, vm => {
Control c = new LoginView();
c.DataContext = vm;
return c;
} },
{ SessionLifecycle.Setup, vm => {
Control c = new ClinicSINSessionView();
c.DataContext = vm;
return c;
} },
{ SessionLifecycle.Testing, vm => new SINListView() { DataContext = vm.CurrentListViewModel } },
{
SessionLifecycle.Finished, vm => {
vm.ExportAsCsvAsync();
Logger.Singleton.SessionComplete();
return new SessionResultView() { DataContext = vm };
}
}
};
在主窗口中:
public partial class MainWindow : Window, INotifyPropertyChanged{
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
SINSessionViewModel vm = null;
vm = new SINSessionViewModel(dialogProvider, authenticatedClient);
vm.CreateSession.Execute(SessionPurpose.Clinic);
ViewModel = vm;
ViewModel_LifecycleChanged(vm, EventArgs.Empty);
}
private Dictionary<SessionLifecycle, Func<SINSessionViewModel, Control>> LifecycleViews = new Dictionary<SessionLifecycle, Func<SINSessionViewModel, Control>>()
{
{ SessionLifecycle.Login, vm => {
Control c = new LoginView(vm.Lifecycle);
c.DataContext = vm;
return c;
} },
{ SessionLifecycle.Setup, vm => {
Control c = new ClinicSINSessionView();
c.DataContext = vm;
return c;
} },
{ SessionLifecycle.Testing, vm => new SINListView() { DataContext = vm.CurrentListViewModel } },
{
SessionLifecycle.Finished, vm => {
vm.ExportAsCsvAsync();
Logger.Singleton.SessionComplete();
return new SessionResultView() { DataContext = vm };
}
}
};
}
class SINSessionViewModel{
public SessionLifecycle Lifecycle
{
get { return _lifecycle; }
private set
{
_lifecycle = value;
if (LifecycleChanged != null) { LifecycleChanged(this, EventArgs.Empty); }
}
}
//some other actions which can change the value of Lifecycle
}
void ViewModel_LifecycleChanged(object sender, EventArgs e)
{
var requestedView = LifecycleViews[ViewModel.Lifecycle](ViewModel);
MainView.Content = requestedView;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsInSession)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CanRecoverSession)));
}
它读取包含视图的字典并加载它们