【问题标题】:[WP7][MVVM Light toolkit] Button Command raised too early, before binding when using mvvm-light toolkit[WP7][MVVM Light toolkit] 按钮命令提得太早,在使用 mvvm-light 工具包时绑定之前
【发布时间】:2010-10-25 07:01:51
【问题描述】:

我在我的 windows phone 7 应用程序中使用 mvvm-light 工具包。

我认为:

<TextBox Height="78" HorizontalAlignment="Left" Margin="108,33,0,0" VerticalAlignment="Top" Width="313" Text="{Binding MyValue, Mode=TwoWay}" />
<Button Content="Go" Height="78" HorizontalAlignment="Left" Margin="127,252,0,0" Name="button1" VerticalAlignment="Top" Width="213" cmd:ButtonBaseExtensions.Command="{Binding DoCommand}"  />

我的视图模型是:

    public class MainPageViewModel : ViewModelBase
    {
        public ICommand DoCommand { get; internal set; }
    public MainPageViewModel()
    {
        DoCommand = new RelayCommand(() =>
            {
                DoSomethingWith(MyValue);
            }, () => true);

    }

    private const string MyValuePropertyName = "MyValue";
    private string _myValue;
    public string MyValue
    {
        get { return _myValue; }
        set
        {
            if (_myValue == value)
                return;
            _myValue = value;
            RaisePropertyChanged(MyValuePropertyName);
        }
    }
}

在模拟器中,当我在文本框中键入值并单击按钮时,我可以看到我首先在 relaycommand lambda 表达式中进行操作,并带有断点 我看到 MyValue 为空。然后,到达 MyValue 的 setter 中的断点,正确的值进入 MyValue。

我做错了什么?当然,我希望可以在 RelayCommand 之前到达 setter...

提前感谢您的帮助。

【问题讨论】:

    标签: windows-phone-7 mvvm-light


    【解决方案1】:

    您可能遇到了 TextChanged 事件的 TextBox DataBinding 问题。这是 Silverlight 3、see this thread discussing this issue 和解决方法中公认的问题。一个简洁的解决方案可能是使用this article 中讨论的行为。

    HTH,indyfromoz

    【讨论】:

    • 感谢您的回答。你说的是 SL 3,但我使用的是 SL4,这会改变什么?
    • 请注意,Windows Phone 7 Silverlight 是 Silverlight 3,其中有一些来自 Silverlight 4 的部分。看看这篇文章 - silverlighthack.com/post/2010/03/16/…,还有其他帖子讨论了 WP7 上的 Silverlight 版本
    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 2011-02-23
    • 2012-01-11
    • 1970-01-01
    • 2011-03-10
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多