silverlight – MVVM Light太快了:)

silverlight – MVVM Light太快了:),第1张

概述我有一个带TextBox的简单WM7页面.此外,我将EventToCommand(一个RelayCommand< string>)分配给此TextBox,对TextChanged事件做出反应.为了测试pourposes,我在页面的代码后面添加了另外的方法TextBox_TextChanged.命令和TextBox_TextChanged都打印带有文本框内容的消息框. TextBox的初始值是“AB 我有一个带TextBox的简单WM7页面.此外,我将EventToCommand(一个RelayCommand< string>)分配给此TextBox,对TextChanged事件做出反应.为了测试pourposes,我在页面的代码后面添加了另外的方法TextBox_TextChanged.命令和TextBox_TextChanged都打印带有文本框内容的消息框.

TextBox的初始值是“ABC”.然后我按下D和:

> TextBox_TextChanged打印ABCD.
>该命令打印ABC. D缺失了.

为什么命令这么快?

命令声明:

public RelayCommand<string> TextChanged {get; private set;}

命令初始化:

TextChanged = new RelayCommand<string>((s) => MessageBox.Show(s));

命令绑定:

<TextBox x:name="SearchTextBox" margin="10,0" textwrapPing="Wrap" Text="{Binding SearchString,Mode=TwoWay}" FontStyle="Italic" TextChanged="SearchTextBox_TextChanged" >    <i:Interaction.Triggers>        <i:EventTrigger Eventname="TextChanged">            <galaSoft_Mvvmlight_Command:EventToCommand Command="{Binding TextChanged,Mode=OneWay}" CommandParameter="{Binding Text,Elementname=SearchTextBox}"/>        </i:EventTrigger>    </i:Interaction.Triggers></TextBox>
解决方法 我无法重现这种行为.我尝试过使用EventToCommand和一个Behavior(它只是监听TextChanged事件).

在没有看到代码的情况下,我怀疑这可能与您如何获取搜索框的文本或其他地方的逻辑错误有关.

这是我如何使用EventToCommand的片段:

<TextBox name="SearchTextBox">  <i:Interaction.Triggers>    <i:EventTrigger Eventname="TextChanged">      <cmd:EventToCommand Command="{Binding TestTextChangedCommand,Mode=OneWay}" CommandParameter="{Binding Path=Text,Elementname=SearchTextBox}"/>    </i:EventTrigger>  <i:Interaction.Triggers></TextBox>

在viewmodel中

m_TestTextChangedCommand = new RelayCommand<string>(val => System.Diagnostics.DeBUG.Writeline(val));

如您所见,我使用命令参数将文本框的值传递给viewmodel.这样,viewmodel不必知道文本框以获取文本值.

此方法的替代方法是使用行为和TwoWay绑定来更新属性:

<TextBox name="SearchTextBox" Text="{Binding TextInviewmodel,Mode=TwoWay}" >  <i:Interaction.Behaviors>    <sc:UpdateOnTextChangedBehavior/>  </i:Interaction.Behaviors></TextBox>

UpdateOnTextChangedBehavior类:

public class UpdateOnTextChangedBehavior : Behavior<TextBox>    {        protected overrIDe voID OnAttached()        {            base.OnAttached();            this.Associatedobject.TextChanged +=                 new TextChangedEventHandler(Associatedobject_TextChanged);        }        voID Associatedobject_TextChanged(object sender,TextChangedEventArgs e)        {            System.Diagnostics.DeBUG.Writeline(((TextBox)sender).Text);            BindingExpression binding =                 this.Associatedobject.GetBindingExpression(TextBox.TextProperty);            if (binding != null)            {                binding.UpdateSource();            }        }        protected overrIDe voID OnDetaching()        {            base.OnDetaching();            this.Associatedobject.TextChanged -=                 new TextChangedEventHandler(Associatedobject_TextChanged);        }    }

以上是做什么模仿桌面WPF绑定与UpdateSourceTrigger = PropertyChanged的行为,Silverlight中缺少这种行为.那么当你在文本框中键入TextInviewmodel属性时,会发生什么.这个属性不是DependencyProperty,它可能只是一个普通的CLR属性.

总结

以上是内存溢出为你收集整理的silverlight – MVVM Light太快了:)全部内容,希望文章能够帮你解决silverlight – MVVM Light太快了:)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1004791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存