Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从带有数据绑定属性的文本框中获取文本,并通过mvvm指示灯通知内容 我有一个用户名和密码框 在它下面我有一个按钮 当我点击该按钮时,我想分析用户名和密码框中的内容_C#_Windows Phone 8_Mvvm Light - Fatal编程技术网

C# 如何从带有数据绑定属性的文本框中获取文本,并通过mvvm指示灯通知内容 我有一个用户名和密码框 在它下面我有一个按钮 当我点击该按钮时,我想分析用户名和密码框中的内容

C# 如何从带有数据绑定属性的文本框中获取文本,并通过mvvm指示灯通知内容 我有一个用户名和密码框 在它下面我有一个按钮 当我点击该按钮时,我想分析用户名和密码框中的内容,c#,windows-phone-8,mvvm-light,C#,Windows Phone 8,Mvvm Light,如何使用mvvm light执行此操作? 这就是我的处境: XAML ...DataContext="{Binding Main, Source={StaticResource Locator}}">... <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0"> <TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" TextWrappi

如何使用mvvm light执行此操作?

这就是我的处境:

XAML

...DataContext="{Binding Main, Source={StaticResource Locator}}">...
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
        <TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="72" Margin="0,27,0,0" TextWrapping="Wrap" Text="{Binding Username}" VerticalAlignment="Top" Width="456"/>
        <TextBlock HorizontalAlignment="Left" Margin="10,99,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top"/>
        <PasswordBox HorizontalAlignment="Left" Height="72" Margin="0,126,0,0" Password="{Binding Password}" VerticalAlignment="Top" Width="456"/>

        <Button Content="Log in" HorizontalAlignment="Center" Margin="167,203,169,0" VerticalAlignment="Top" Command="{Binding LogInCommand}"/>
    </Grid>
MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}
目前正在发生的事情:

  • 当我单击我的按钮时,LogInCommand将运行,它将激发我的方法OnLoginCommand。我在testUsername声明上放了一个断点,看看当点击按钮时,用户名和密码是否反映了输入的内容;它们都是空的。我必须做些什么来确保在有人打字、按下按钮或以任何方式工作时更新这些内容
我现在已经花了大约4周的时间学习mvvm,并尝试获得一个简单的点击事件和绑定。这根本没有意义。。。doh。谢谢你的帮助


p.S-MVVM灯对新来者来说是否太容易混淆?文件是这样的。。光照细节。无示例:(

要将视图和ViewModel“链接”起来以便同步,您需要实现INotifyPropertyChanged(封装在ViewModelBase中)。即:

查看

Windows Phone不包含“UpdateSourceTrigger=PropertyChanged”。您必须使用“显式””并在代码隐藏中手动调用“UpdateSource”,否则当TextBox/PasswordBox失去焦点时,TextBox/PasswordBox的值将升高

不要忘记设置“模式=双向”

视图模型

public class MainViewModel : ViewModelBase
{
    public LoginCredentials LoginCredentials { get; set; }        
    public ICommand LogInCommand { get; private set; }

    public MainViewModel()
    {
        LoginCredentials = new LoginCredentials();
        LogInCommand = new RelayCommand(this.OnLogInCommand);
    }

    private void OnLogInCommand()
    {
        string testUsername = Username;
        string testPassword = Password;
    }

    #region Properties
    public string Username
    {
        get { return LoginCredentials.Username; }
        set { LoginCredentials.Password = value; }
    }
    public string Password
    {
        get { return LoginCredentials.Password; }
        set { LoginCredentials.Password = value; }
    }
    #endregion
}
田地

private RelayCommand _logInCommand;
private string _password;
private string _username;
性质

public bool CanExecuteLogInCommand
{
    get
    {
        return !string.IsNullOrWhiteSpace(this.Username) && !string.IsNullOrWhiteSpace(this.Password);
    }
}

    public RelayCommand LogInCommand
    {
        get
        {
            // or you can create instance in constructor: this.LogInCommand = new RelayCommand(this.ExecuteLogInCommand, () => this.CanExecuteLogInCommand);
            return this._logInCommand ?? (this._logInCommand = new RelayCommand(this.ExecuteLogInCommand, () => this.CanExecuteLogInCommand));
        }
    }

    public string Username
    {
        get { return this._username; }

        set
        {
            // a) shorter alternative -> "True if the PropertyChanged event has been raised, false otherwise"
            if (this.Set(() => this.Username, ref this._username, value))
            {
                // raise CanExecuteLogInCommand
                this.LogInCommand.RaiseCanExecuteChanged();
            }


            // b) longer alternative
            //if (value == this._username) { return; }

            //this._username = value;

            //this.RaisePropertyChanged(() => this.Username);
            //this.LogInCommand.RaiseCanExecuteChanged();
        }
    }

    public string Password
    {
        get { return this._password; }

        set
        {
            if (this.Set(() => this.Password, ref this._password, value))
            {
                this.LogInCommand.RaiseCanExecuteChanged();
            }
        }
    }
方法

    private void ExecuteLogInCommand()
    {
        // .... = this.Username;
        // .... = this.Password;
    }

检查此

首先检查此教程:Windows Phone 8.1通用应用程序模型支持
更新资源记录器=属性更改
<TextBox
    Text="{Binding Path=Username, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
    TextChanged="TextBoxTextChanged" />

<PasswordBox
    Password="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
    PasswordChanged="PasswordBoxPasswordChanged" />

<Button
    Command="{Binding Path=LogInCommand}"
    Content="Log in" />
public bool CanExecuteLogInCommand
{
    get
    {
        return !string.IsNullOrWhiteSpace(this.Username) && !string.IsNullOrWhiteSpace(this.Password);
    }
}

    public RelayCommand LogInCommand
    {
        get
        {
            // or you can create instance in constructor: this.LogInCommand = new RelayCommand(this.ExecuteLogInCommand, () => this.CanExecuteLogInCommand);
            return this._logInCommand ?? (this._logInCommand = new RelayCommand(this.ExecuteLogInCommand, () => this.CanExecuteLogInCommand));
        }
    }

    public string Username
    {
        get { return this._username; }

        set
        {
            // a) shorter alternative -> "True if the PropertyChanged event has been raised, false otherwise"
            if (this.Set(() => this.Username, ref this._username, value))
            {
                // raise CanExecuteLogInCommand
                this.LogInCommand.RaiseCanExecuteChanged();
            }


            // b) longer alternative
            //if (value == this._username) { return; }

            //this._username = value;

            //this.RaisePropertyChanged(() => this.Username);
            //this.LogInCommand.RaiseCanExecuteChanged();
        }
    }

    public string Password
    {
        get { return this._password; }

        set
        {
            if (this.Set(() => this.Password, ref this._password, value))
            {
                this.LogInCommand.RaiseCanExecuteChanged();
            }
        }
    }
    private void ExecuteLogInCommand()
    {
        // .... = this.Username;
        // .... = this.Password;
    }