gpt4 book ai didi

c# - 使用MVVM(无任何代码)且无导航时,单击按钮时更改 View

转载 作者:行者123 更新时间:2023-12-03 10:43:40 24 4
gpt4 key购买 nike

我正在使用WPF MVVM。

我有一个WCF聊天服务,该服务应执行某些验证操作(用户凭据)才能将消息发送到聊天窗口。

当然,这是在我按下“登录!”按钮时发生的。

我要完成的事情是在我按下登录并成功通过验证后的新 View (“聊天窗口” View )。

*由于我还没有聊天 View ,因此假设我的聊天 View 是我的注册 View (仅出于了解如何使用ICommand进行按钮单击时切换 View 的原理)

我将发布已完成的操作:

MainWindow.xaml:

    <Window.Resources>
<DataTemplate DataType="{x:Type ViewModels:LoginViewModel}">
<Views:LoginView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:SignUpViewModel}">
<Views:SignUpView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding CurrentView}" />
</Grid>

MainWindow.xaml.cs:
    public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new AppViewModel();
}
}

LoginViewModel.cs
    public class LoginViewModel : ViewModelBase
{
public AuthenticatedUser AuthenticatedUser { get; set; }
private string username;

public string UserName
{
get { return username; }
set {
username = value;
Notify();
}
}
private string password;
public string Password
{
get { return password; }
set
{
password = value;
Notify();
}
}

public ConsoleLog ConsoleLog { get ; set; } // TODO: StringBuilder.
public UserServiceProxy UserServiceProxy { get; set; }
public ChatServiceProxy ChatServiceProxy { get; set; }

public LoginViewModel()
{
AuthenticatedUser = AuthenticatedUser.Instance;
UserServiceProxy = new UserServiceProxy();
ChatServiceProxy = new ChatServiceProxy();
SendLoginRequestCommand = new MyCommand(SendLoginRequest);
ConsoleLog = new ConsoleLog();
ConsoleLog.Document = "Binding Checker.";
}

public ICommand SendLoginRequestCommand { get; set; }

private void SendLoginRequest()
{
LoginResponse response = UserServiceProxy.Login(new LoginRequest { UserName = UserName, Password = Password });
ConsoleLog.Document += $"{response.IsSuccess} {response.Message}";
if(response.IsSuccess == true)
{
AuthenticatedUser.Authentication = response.Authentication;
JoinServiceResponse responseFromChatService = ChatServiceProxy.JoinService(new JoinServiceRequest());
ConsoleLog.Document += responseFromChatService.Message;

/* This is where I think the view should be changed,yet no success for a few days now.
*
*
*
*
*
*/
}

}

private void LoginEventRaised(object sender, OnLoginEventArgs e)
{
ConsoleLog.Document += $"{e.UserName} has logged in.";
}
}

SignUpViewModel.cs:
    public class SignUpViewModel : ViewModelBase
{
public string UserName { get; set; }
public string Password { get; set; }
public string PasswordConfirm { get; set; }
public ConsoleLog ConsoleLog { get; set; }
public UserServiceProxy UserServiceProxy { get; set; }

public ICommand OnClickSignUpCommand { get; set; }

public SignUpViewModel()
{
UserServiceProxy = new UserServiceProxy();
ConsoleLog = new ConsoleLog { Document = "Check Register."};
OnClickSignUpCommand = new MyCommand(SendSignUpRequest);
}

private void SendSignUpRequest()
{
SignUpResponse response = UserServiceProxy.SignUp(new SignUpRequest { UserName = UserName, Password = Password, PasswordConfirm = PasswordConfirm });
if(response.IsSuccess == true)
{
ConsoleLog.Document += $"{response.IsSuccess} {response.Message}";
response.AllOtherUsers.ForEach(u => ConsoleLog.Document += $"{u.UserName} Signed up.");
}
}
}

ViewModelBase.cs:
    public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public virtual void Notify([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

AppViewModel.cs:
    public class AppViewModel : ViewModelBase
{
private ViewModelBase currentView;

public ViewModelBase CurrentView
{
get { return currentView; }
set {
currentView = value;
Notify();
}
}

public ICommand ViewLoginCommand { get; }
public ICommand ViewSignUpCommand { get; }

public AppViewModel()
{
ViewLoginCommand = new MyCommand(SetCurrentViewToLoginViewModel);
ViewSignUpCommand = new MyCommand(SetCurrentViewToSignUpViewModel);
CurrentView = new LoginViewModel(); //temporary loading the first view the use will see.
}
private void SetCurrentViewToLoginViewModel()
{
CurrentView = new LoginViewModel();
}
public void SetCurrentViewToSignUpViewModel()
{
CurrentView = new SignUpViewModel();
}
}

LoginView.xaml(这是一个用户控件):
    <Grid>
<Button x:Name="login" Content="login" HorizontalAlignment="Left" Margin="292,171,0,0" VerticalAlignment="Top" Width="114" Height="33" Command="{Binding SendLoginRequestCommand}"/>
<TextBox x:Name="passwordTextBox" HorizontalAlignment="Left" Height="34" Margin="292,103,0,0" TextWrapping="Wrap" Text="{Binding Password}" VerticalAlignment="Top" Width="141"/>
<TextBox x:Name="userNameTextBox" HorizontalAlignment="Left" Height="34" Margin="292,51,0,0" TextWrapping="Wrap" Text="{Binding UserName}" VerticalAlignment="Top" Width="141"/>
<TextBlock x:Name="consoleLog" HorizontalAlignment="Left" Margin="24,33,0,0" TextWrapping="Wrap" Text="{Binding Path = ConsoleLog.Document}" VerticalAlignment="Top" Height="232" Width="218" />
<Button x:Name="logout" Content="logout" HorizontalAlignment="Left" Margin="292,232,0,0" VerticalAlignment="Top" Width="114" Height="33"/>
</Grid>

SignUpView.xaml(此代码并非真正需要此代码,但以防万一):
    <Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="30" Margin="310,24,0,0" TextWrapping="Wrap" Text="{Binding UserName}" VerticalAlignment="Top" Width="130"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="30" Margin="310,74,0,0" TextWrapping="Wrap" Text="{Binding Password}" VerticalAlignment="Top" Width="130"/>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="30" Margin="310,124,0,0" TextWrapping="Wrap" Text="{Binding PasswordConfirm}" VerticalAlignment="Top" Width="130"/>
<Button x:Name="register" Content="Register" HorizontalAlignment="Left" Margin="310,176,0,0" VerticalAlignment="Top" Width="130" Command="{Binding OnClickSignUpCommand}"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="25,10,0,0" TextWrapping="Wrap" Text="{Binding Path=ConsoleLog.Document}" VerticalAlignment="Top" Height="289" Width="253"/>
</Grid>

我知道我在这里想念什么。可能是因为我试图解决几天问题,所以我迷路了。

这是我的登录信息:(尚无法嵌入)

LoginView.xaml

当我按下“登录”时,需要下一个 View 加载:

SignUpView.xaml

对于这个问题,我无法提供任何代表。不过,我非常感谢您的帮助。

浏览了网络,找不到合适的问题。

最佳答案

您确实为问题提供了很多代码,请参阅此Minimal, Complete, and Verifiable code example
在发布您的问题之前。

如果我了解您要在程序中执行的操作,则AppViewModel是您的“MainNavigation”类,该类负责管理所有 View 并在它们之间进行导航。

我可以提供的一种解决方案是将一个事件添加到LoginViewModel中,该事件将在用户成功登录后引发。在AppViewModel中,您将注册到此事件并实现一个处理程序,该处理程序会将CurrentView属性更改为SignUpViewModel
LoginViewModel.cs的附加内容将如下所示:

public class LoginViewModel : ViewModelBase
{
...

public delegate void UserLoginSuccessfullyHandler();
public event UserLoginSuccessfullyHandler UserLoginSuccessfullyEvent;

private void SendLoginRequest()
{
LoginResponse response = UserServiceProxy.Login(new LoginRequest { UserName = UserName, Password = Password });
ConsoleLog.Document += $"{response.IsSuccess} {response.Message}";
if(response.IsSuccess == true)
{
AuthenticatedUser.Authentication = response.Authentication;
JoinServiceResponse responseFromChatService = ChatServiceProxy.JoinService(new JoinServiceRequest());
ConsoleLog.Document += responseFromChatService.Message;

// This is where you inform the AppViewModel to change his CurrentView
if (UserLoginSuccessfullyEvent!= null)
UserLoginSuccessfullyEvent();
}
}

...
}
AppViewModel.cs的附加内容将如下所示:
    public class AppViewModel : ViewModelBase
{
...

public AppViewModel()
{
var loginViewModel = new LoginViewModel();
loginViewModel.UserLoginSuccessfullyEvent += new UserLoginSuccessfullyHandler(myUserLoginSuccessfullyHandler);
CurrentView = loginViewModel;
}

private void myUserLoginSuccessfullyHandler()
{
CurrentView = new SignUpViewModel();
}

...
}

您可以在这里看到更多有关事件的信息 Events in C#

关于c# - 使用MVVM(无任何代码)且无导航时,单击按钮时更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com