博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silverlight中使用MVVM(6):AutoComplteBox的异步过滤
阅读量:6581 次
发布时间:2019-06-24

本文共 2479 字,大约阅读时间需要 8 分钟。

转自

 

Toolkit中AutoCompleteBox控件的功能非常强大,其用法可以参考这篇文章。

在实际的开发中,数据一般通过WCF异步传递的,要实现AutoCompleteBox的过滤数据,主要用到的是Populating事件和PopulateComplete方法

private void autoCompleteBox1_Populating(object sender, PopulatingEventArgs e)
{
ServiceAPI.FilterServiceClient filterServiceClient = new FilterServiceClient();
filterServiceClient.DoWorkCompleted += (obj, arg) =>
{
this.autoCompleteBox1.ItemsSource = arg.Result;
this.autoCompleteBox1.PopulateComplete();
};
filterServiceClient.DoWorkAsync();
}

上面的代码是比较常见的操作方式,这里我们关注一下如何使用MVVM的方式将其分离到ViewModel中

首先定义名为FilterAsyncParameters的类,其有2个属性,FilterCriteria表示过滤参数,即文本框中的值,FilterComplete 则封装了下PopulateComplete方法

public class FilterAsyncBehavior : Behavior
{
public ICommand FilterAsyncCommand
{
get
{
return (ICommand)GetValue(FilterAsyncCommandProperty);
}
set
{
SetValue(FilterAsyncCommandProperty, value);
}
}
public static readonly DependencyProperty FilterAsyncCommandProperty = DependencyProperty.Register("FilterAsyncCommand",
typeof(ICommand),
typeof(FilterAsyncBehavior),
new PropertyMetadata(null));
...
}

这里定义的FilterAsyncCommand用于执行ViewModel里面的ICommand,FilterAsyncBehavior重写了OnAttached,OnDetaching方法

protected override void OnAttached()        {
base.OnAttached(); AssociatedObject.Populating += AssociatedObject_Populating; } protected override void OnDetaching() {
AssociatedObject.Populating -= AssociatedObject_Populating; base.OnDetaching(); } private void AssociatedObject_Populating(object sender, PopulatingEventArgs e) {
ICommand filterCommand = FilterAsyncCommand; if (filterCommand != null) {
//创建过滤参数 var parameters = new FilterAsyncParameters(AssociatedObject.PopulateComplete, e.Parameter); filterCommand.Execute(parameters); e.Cancel = true; } }

ViewModel中定义了ICommand属性

 

public AcbAsyncViewModel()

{

FilterCommand = new RelayCommand<FilterAsyncParameters>(ExecuteFilter);

}
private void ExecuteFilter(FilterAsyncParameters filterAsyncParameters)
{
ServiceAPI.FilterServiceClient filterServiceClient=new FilterServiceClient();
filterServiceClient.DoWorkCompleted += (obj, arg) =>
{
…..

var filter = arg.UserState as FilterAsyncParameters;

filter.FilterComplete();
};
filterServiceClient.DoWorkAsync(filterAsyncParameters);
}

View中Xaml代码

这样就实现了以MVVM实现了AutoCompleteBox的异步过滤数据,实现过程中需要对Behavior等概念有一定理解,希望这篇文章给各位提供一点思路。
代码下载:

转载地址:http://ezfno.baihongyu.com/

你可能感兴趣的文章
法瑞意游记+攻略 二(巴黎,十二月二十七)未完
查看>>
算法笔记之 并查集入门 POJ 1611
查看>>
sql语句实例析解
查看>>
ExtJs 起始日期 结束日期 验证
查看>>
Getting over the dangers of rm command in Linux---reference
查看>>
25个有用的和方便的 WordPress 速查手册
查看>>
我的Android开发相关文�
查看>>
jquery和css3实现的很酷的菜单导航
查看>>
Nginx.conf简介
查看>>
Cucumber 入门一
查看>>
Openfire开发配置,Openfire源码配置,OpenFire二次开发配置
查看>>
Effective JavaScript Item 30 理解prototype, getPrototypeOf和__proto__的不同
查看>>
iOS7隐藏状态栏 status Bar
查看>>
Android中View绘制流程以及invalidate()等相关方法分析
查看>>
安装ESXI 5.5卡在LSI_MR3.V00解决方案
查看>>
在SQL Server 2014里可更新的列存储索引 (Updateable Column Store Indexes)
查看>>
centos 7 卸载 mariadb 的正确命令
查看>>
NSString 的常用操作
查看>>
poj 1274The Perfect Stall
查看>>
[Jmeter系列]Jmeter源码编译步骤(转)
查看>>