WPF技巧(1)异步绑定
2010-04-26 22:07
225 查看
与大家共勉
1.定义一个集合属性private IList<string> _list;
public IList<string> List
{
get
{
if (_list == null)
{
_list = new List<string>();
for (int i = 0; i < 100; i++)
{
_list.Add("item" + i);
}
}
return _list;
}
}
2.绑定属性<Window x:Class="WpfRecipe1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfRecipe1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox
ItemsSource="{Binding List,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}"
Height="179" HorizontalAlignment="Left" Margin="76,31,0,0" Name="listBox1"
VerticalAlignment="Top" Width="135" />
</Grid>
</Window>
上面的代码可以很好的运行.
事实上我们取数据都没这么简单,假设这段数据是从数据里取的,花费3秒时间,我们以线程模拟
private IList<string> _list;
public IList<string> List
{
get
{
System.Threading.Thread.Sleep(3000);
//省略代码
return _list;
}
}
下面重新运行代码,你将会发现程序会先停滞三秒.结论在刚开始已经提到
即使下面的代码也是遵循上面的原则 <TabControl>
<TabItem Header="tab1"></TabItem>
<TabItem Header="tab2">
<ListBox
ItemsSource="{Binding List,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}"
Height="179" HorizontalAlignment="Left" Margin="76,31,0,0" Name="listBox1"
VerticalAlignment="Top" Width="135" />
</TabItem>
</TabControl>
这是相当郁闷的一段代码,因为用户什么也没看到,却让用户等了三分钟.
首先想到的是自己异步取数据,其实还有一个更简单的方法就是在绑定时将IsAsync设置为True <ListBox
ItemsSource="{Binding List, IsAsync=True, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}"
Height="179" HorizontalAlignment="Left" Margin="76,31,0,0" Name="listBox1"
VerticalAlignment="Top" Width="135" />
说实话我也刚刚知道有这么一个属性,这个属性几乎被我们忽略,平时用到很少,因为我们第一思维就是自己异步去加载数据,学了一招
当属性值填充好后,与该属性绑定的界面才会开始加载(属性绑定优于控件加载)
这个技巧很简单,但却影响着运行的速度.以下为测试1.定义一个集合属性private IList<string> _list;
public IList<string> List
{
get
{
if (_list == null)
{
_list = new List<string>();
for (int i = 0; i < 100; i++)
{
_list.Add("item" + i);
}
}
return _list;
}
}
2.绑定属性<Window x:Class="WpfRecipe1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfRecipe1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox
ItemsSource="{Binding List,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}"
Height="179" HorizontalAlignment="Left" Margin="76,31,0,0" Name="listBox1"
VerticalAlignment="Top" Width="135" />
</Grid>
</Window>
上面的代码可以很好的运行.
事实上我们取数据都没这么简单,假设这段数据是从数据里取的,花费3秒时间,我们以线程模拟
private IList<string> _list;
public IList<string> List
{
get
{
System.Threading.Thread.Sleep(3000);
//省略代码
return _list;
}
}
下面重新运行代码,你将会发现程序会先停滞三秒.结论在刚开始已经提到
即使下面的代码也是遵循上面的原则 <TabControl>
<TabItem Header="tab1"></TabItem>
<TabItem Header="tab2">
<ListBox
ItemsSource="{Binding List,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}"
Height="179" HorizontalAlignment="Left" Margin="76,31,0,0" Name="listBox1"
VerticalAlignment="Top" Width="135" />
</TabItem>
</TabControl>
这是相当郁闷的一段代码,因为用户什么也没看到,却让用户等了三分钟.
首先想到的是自己异步取数据,其实还有一个更简单的方法就是在绑定时将IsAsync设置为True <ListBox
ItemsSource="{Binding List, IsAsync=True, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MainWindow}}}"
Height="179" HorizontalAlignment="Left" Margin="76,31,0,0" Name="listBox1"
VerticalAlignment="Top" Width="135" />
说实话我也刚刚知道有这么一个属性,这个属性几乎被我们忽略,平时用到很少,因为我们第一思维就是自己异步去加载数据,学了一招
相关文章推荐
- WPF技巧(1)异步绑定
- WPF TreeView递归异步绑定
- WPF listbox异步绑定数据IsAsync=True
- 【WPF】请注意一个 异步绑定 的陷阱!
- 们只是「电脑玩物」 首页 实用技巧 免费资源 超好玩的游戏 软件推荐 IT技术 资讯 编程 其它 MVVM设计模式和WPF中的实现(四) 事件绑定 07net01.com 发布于 2015-10-08
- WPF技巧(2)绑定到附加属性
- WPF技巧(2)绑定到附加属性
- WPF使用异步绑定数据
- WPF TreeView递归异步绑定
- WPF使用异步+绑定的方式处理大数据量
- 参照WPF实现Silverlight中的多值绑定特性
- [WPF]实现密码框的密码绑定
- WPF Button的属性 Content绑定字符串失败
- ko knockoutjs动态属性绑定技巧应用
- WPF技巧(4)使用Visual元素
- WPF入门(三):简单绑定 - 绑定到页面元素
- WPF入门(四):简单绑定 - 静态资源绑定
- 【WPF】MVVM前台绑定一组RadioButton按钮
- ASP.NET 2.0 绑定高级技巧
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.