[WP7][MVVM Light 工具包] 使用 mvvm-light 工具包时在绑定之前过早引发按钮命令

发布于 2024-09-29 02:04:21 字数 1297 浏览 0 评论 0原文

我在 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);
        }
    }
}

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

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

提前感谢您的帮助。

I'm using the mvvm-light toolkit in my windows phone 7 application.

I have in my view :

<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}"  />

My view model is :

    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);
        }
    }
}

In the emulator, when I type value in the textbox, and I click the button, I can see that I'm going first in the relaycommand lambda expression and with a breakpoint
I see that MyValue is null. Then, the breakpoint in the setter of MyValue is reached, and the correct value goes in MyValue.

What am I doing wrong ? Of course, I would like that the setter can be reached before the RelayCommand...

Thanks in advance for any help.

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

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

发布评论

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

评论(1

如痴如狂 2024-10-06 02:04:21

您可能遇到了 TextChanged 事件的 TextBox DataBinding 问题。这是 Silverlight 3 中公认的问题,查看讨论此问题的帖子以及解决方法。一个巧妙的解决方案可能是使用 这篇文章

HTH,indyfromoz

It is possible that you have hit the TextBox DataBinding issue with the TextChanged event. This is a recognized problem in Silverlight 3, see this thread discussing this issue and the workaround. A neat solution is perhaps to use behaviors as discussed in this article.

HTH, indyfromoz

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