从 WPF 中的 View 观察 ViewModel 中的变量

发布于 2025-01-09 18:46:24 字数 1237 浏览 0 评论 0原文

我的 ViewModel 中有一个布尔变量,我想观察它的任何变化。 ViewModel:

    internal class AuthenticationViewModel : ViewModelBase
    {
        APIClient apiClient;
        bool _loginStatus;

        public AuthenticationViewModel()
        {
        }

        public bool LoginStatus { get { return _loginStatus; } 
            set { 
                _loginStatus = value; 
                NotifyPropertyChanged("LoginStatus"); 
            } }
}
public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

我尝试在我的视图中使用它,如下所示:

public MainWindow()
        {
            InitializeComponent();
            ViewModel = new AuthenticationViewModel();
            _ = ViewModel.GetReadyForUnlockWithBLEAsync();
            if(ViewModel.LoginStatus)
            {
                AuthenticationSuccess();
            }
        }

但我无法从 ViewModel 观察变量。我无法在 ViewModel 中进行任何更改时在 View 中获取其更新值。

I have a boolean variable in my ViewModel and I want to observe any changes to it.
ViewModel:

    internal class AuthenticationViewModel : ViewModelBase
    {
        APIClient apiClient;
        bool _loginStatus;

        public AuthenticationViewModel()
        {
        }

        public bool LoginStatus { get { return _loginStatus; } 
            set { 
                _loginStatus = value; 
                NotifyPropertyChanged("LoginStatus"); 
            } }
}
public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

I am trying to use it in my View as following:

public MainWindow()
        {
            InitializeComponent();
            ViewModel = new AuthenticationViewModel();
            _ = ViewModel.GetReadyForUnlockWithBLEAsync();
            if(ViewModel.LoginStatus)
            {
                AuthenticationSuccess();
            }
        }

But I am not able to observe the variable from ViewModel. I can't get its updated value in View on any changes in ViewModel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹂绝世的画 2025-01-16 18:46:24

创建视图模型后,MainWindow 构造函数中的代码会检查一次 LoginStatus 属性的值。没有任何东西可以注册 PropertyChanged 事件,例如数据绑定。

您可以手动注册一个 PropertyChanged 处理程序,如下所示,并在窗口的 Loaded 事件的异步处理程序中等待可等待方法调用。

public MainWindow()
{
    InitializeComponent();

    ViewModel = new AuthenticationViewModel();

    ViewModel.PropertyChanged += OnViewModelPropertyChanged;

    Loaded += OnWindowLoaded;
}

private async void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    await ViewModel.GetReadyForUnlockWithBLEAsync();
}

private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(ViewModel.LoginStatus))
    {
        AuthenticationSuccess();
    }
}

也许您根本不需要 PropertyChanged 处理。假设通过 GetReadyForUnlockWithBLEAsync 方法更新了 LoginStatus,这也可能有效:

public MainWindow()
{
    InitializeComponent();

    ViewModel = new AuthenticationViewModel();

    Loaded += OnWindowLoaded;
}

private async void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    await ViewModel.GetReadyForUnlockWithBLEAsync();

    AuthenticationSuccess();
}

The code in your MainWindow constructor checks the value of the LoginStatus property once after creating the view model. There is nothing that would register a PropertyChanged event, like for example a data binding.

You may manually register a PropertyChanged handler like shown below, and also await the awaitable method call in an async handler of the Window's Loaded event.

public MainWindow()
{
    InitializeComponent();

    ViewModel = new AuthenticationViewModel();

    ViewModel.PropertyChanged += OnViewModelPropertyChanged;

    Loaded += OnWindowLoaded;
}

private async void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    await ViewModel.GetReadyForUnlockWithBLEAsync();
}

private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(ViewModel.LoginStatus))
    {
        AuthenticationSuccess();
    }
}

Perhaps you don't need the PropertyChanged handling at all. Assuming that LoginStatus is updated by the GetReadyForUnlockWithBLEAsync method, this may also work:

public MainWindow()
{
    InitializeComponent();

    ViewModel = new AuthenticationViewModel();

    Loaded += OnWindowLoaded;
}

private async void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    await ViewModel.GetReadyForUnlockWithBLEAsync();

    AuthenticationSuccess();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文