尝试将文本框与viewmodel绑定但未发生更新

时间:2016-10-05 17:12:56

标签: c# mvvm uwp prism

我已经设置了一个小型测试项目并创建了一个带有用户名和密码的登录页面。我正在使用MVVM方法和Prism框架。我正在关注此视频(https://www.youtube.com/watch?v=ZfBy2nfykqY)。

这是xaml:

<Page x:Class="Control_Center.Views.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:Control_Center.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Grid.Row="0">
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Center">
                <TextBlock Name="LoginResponse"
                           Text="{Binding LoginStatus}" />
            </StackPanel>
        </StackPanel>
        <StackPanel VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Grid.Row="1">
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Center">
                <TextBox Name="UsernameBox"
                         Text="{Binding Username, UpdateSourceTrigger=PropertyChanged}"
                         AcceptsReturn="False"
                         PlaceholderText="Username..."
                         HorizontalAlignment="Center"
                         VerticalAlignment="Center"
                         FontSize="14"
                         Width="200"
                         Height="25" />
            </StackPanel>
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Center">
                <PasswordBox Name="PasswordBox"
                             PasswordRevealMode="Peek"
                             PlaceholderText="Password..."
                             Width="200"
                             Height="25"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Center"
                             FontSize="14"
                             PasswordChanged="PasswordBox_PasswordChanged" />
            </StackPanel>
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Right">
                <Button Name="LoginButton"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Bottom"
                        Margin="0,10,0,0"
                        FontSize="12"
                        Content="Login"
                        Command="{Binding LoginCommand}" />
                <Button Name="CancelButton"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Bottom"
                        Margin="10,10,0,0"
                        FontSize="12"
                        Content="Cancel" />

            </StackPanel>
        </StackPanel>
    </Grid>
</Page>

viewModel(注意:BindableBase是Prism的一部分:https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Mvvm/BindableBase.cs):

using System;
using System.Diagnostics;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Prism.Commands;
using Prism.Mvvm;

namespace EJD_Control_Center.ViewModels
{
    class ViewLoginViewModel : BindableBase
    {
        private string _username;

        public string Username
        {
            get { return _username; }
            set { SetProperty(ref _username, value); }
        }

        private string _password;

        public string Password
        {
            get { return _password; }
            set { SetProperty(ref _password, value); }
        }

        private string _loginStatus;

        public string LoginStatus
        {
            get { return _loginStatus; }
            set { SetProperty(ref _loginStatus, value); }
        }

        public DelegateCommand LoginCommand { get; set; }

        public ViewLoginViewModel()
        {
            LoginCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => Username).ObservesProperty(() => Password);
        }

        private bool CanExecute()
        {
            Debug.WriteLine($"Username: {Username}, password: {Password}");
            Debug.WriteLine("Username is not null or whitespace? " + !string.IsNullOrWhiteSpace(Username) + ", and password: " + !string.IsNullOrWhiteSpace(Password));
            return !string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password);
        }

        private void Execute()
        {
            LoginStatus = $"Login Successful! Username: {Username} Password: {Password}"; 
            //actually return the status of the login request, but we don't have this yet. then switch screens.
        }
    }
}

完成后,代码隐藏:

using Windows.Foundation;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Control_Center.ViewModels;

namespace Control_Center.Views
{
    /// <summary>
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();

            ApplicationView.PreferredLaunchViewSize = new Size(500, 320);
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

            DataContext = new ViewLoginViewModel();
        }

        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            var vm = (ViewLoginViewModel) DataContext;
            vm.Password = PasswordBox.Password;
        }
    }
}

现在在任何人开始之前,我知道这不是处理传递密码的安全方法。这不是这个问题的重点。

问题是当我将用户名文本框绑定到ViewModel中的Username变量时,它似乎没有更新该String。如果我将ViewModel中的用户名设置为某些内容,例如&#34; test&#34;,则会在文本框中显示该文件夹,但对该文本框的更新不会更改ViewModel中的Username变量。

修改这两个框时得到的输出是:

Username: , password: d
Username is not null or whitespace? False, and password: True

即使用户名文本框中包含某些内容。这些调试行完全出现的唯一原因是我处理密码的方式是调用canExecute,但我处理用户名的方式并不是因为据我所知,它应该是

为什么"{Binding Username, UpdateSourceTrigger=PropertyChanged}"没有将用户名文本框文本发送到ViewModel中的用户名变量?

1 个答案:

答案 0 :(得分:2)

这是因为Binding的默认模式设置是OneWay - 所以从代码到布局的更新工作正常,但从布局到代码赢了。 设置Mode = TwoWay让它双向工作。

"{Binding Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"