-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLoginView.xaml.cs
More file actions
51 lines (43 loc) · 1.86 KB
/
LoginView.xaml.cs
File metadata and controls
51 lines (43 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Windows;
using System.Windows.Controls;
namespace ReactiveUI.Samples.Wpf;
/// <summary>
/// A reactive login view demonstrating WhenActivated, Bind, BindCommand,
/// and manual event marshaling for WPF's PasswordBox.
/// </summary>
public partial class LoginView : ReactiveUserControl<LoginViewModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="LoginView"/> class.
/// </summary>
public LoginView()
{
InitializeComponent();
ViewModel = new LoginViewModel(RxSchedulers.MainThreadScheduler);
this.WhenActivated(d =>
{
this.Bind(ViewModel, vm => vm.UserName, v => v.Username.Text)
.DisposeWith(d);
this.BindCommand(ViewModel, vm => vm.Login, v => v.Login)
.DisposeWith(d);
this.BindCommand(ViewModel, vm => vm.Cancel, v => v.Cancel)
.DisposeWith(d);
// WPF PasswordBox doesn't support data binding, so marshal changes manually.
Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(
h => Password.PasswordChanged += h,
h => Password.PasswordChanged -= h)
.Select(_ => Password.Password)
.BindTo(this, v => v.ViewModel!.Password)
.DisposeWith(d);
ViewModel.Login
.Subscribe(success => MessageBox.Show(
success ? "Welcome!" : "Invalid credentials.",
success ? "Login Successful" : "Login Failed"))
.DisposeWith(d);
});
}
}