私はWPF MVVMを使用しています。当然のボタンでの表示の変更MVVM(コードなし)とナビゲーションなしのクリック
私はチャットウィンドウにメッセージを送信するために、特定の検証操作(ユーザー資格情報)を実行する必要がありWCFのチャットサービスを持っている..
これは私が私のボタンを押した瞬間起こる「ログインを!」 。
私が達成したいのは、私がloginを押して検証が成功した瞬間、新しいView(Chatウィンドウビュー)です。
*私はまだチャットビューを持っていないので、私のチャットビューが私のサインアップビューであると仮定することができます(ただのICommandとボタンのクリックでビューを切り替える方法を理解することのプリンシパルの)
私は私はそうやった投稿に行く:
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(このコードは本当にが、念のため、この質問のためにneedeされていません):
<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>
私はここで何かが欠けてる知っているユーザーコントロール)であります。たぶん私は数日間問題を解決しようとしたので、私はトラックを失ってしまったでしょう。
これは私のログインです:
:(まだembeddすることはできません)
私は、 "ログイン" を押した瞬間、私は負荷の隣に表示する必要があります私はこの質問のための担当者を提供することはできません。それでも、私は本当に助けに感謝します。
ネットを調査したところ、適切な質問が見つかりませんでした。