添加登录视图



我想向我的 WPF 应用程序添加登录视图。在应用程序中,我有一个主窗口,它保存/控制用户控件之间的流。

我将登录用户控件添加到应用程序中,并在我的主窗口中具有以:

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
}

所以我需要的是为经过身份验证的客户端登录视图设置一个适当的值,并将会话生命周期更改为会话生命周期.设置以启动应用程序。如果有人能帮助我如何在MainWindow,LoginView和SINSessionViewModel之间传递参数,我将不胜感激。

所以我找到了解决方案: 首先,由于MainWindow包含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 };
}
}
};

在我的登录视图中有以下命令:

.
.
.
<Grid>
<StackPanel Margin="10">
<Label>Username:</Label>
<TextBox Height="25" Margin="0,0,0,0" Text="{Binding Username}" Name="txtUsername"/>
<Label>Password:</Label>
<PasswordBox Height="25" Margin="0,-20,0,0" Name="txtPassword"
ff:PasswordBoxAssistant.BindPassword="true"  ff:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Margin="10" TabIndex="99" Height="25" Content="Login"
Command="{Binding Path=LoginCommand}">
</Button>
</StackPanel>
</Grid>
.
.
public LoginCommandClass LoginCommand
{
get
{
//the validation (code is deleted)
async() =>
{
//login process  (code is deleted)
Lifecycle = SessionLifecycle.Setup;//this will raise an event 
} );
return _login;
}
}
public event EventHandler LifecycleChanged;
public SessionLifecycle Lifecycle
{
get { return _lifecycle; }
private set
{
_lifecycle = value;
if (LifecycleChanged != null) { LifecycleChanged(this, EventArgs.Empty); }
}
}

并在主窗口中:

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)));
}

读取包含视图的字典并加载它们

最新更新