利用C# 5 新特性 异步操作UI
2012-05-14 11:27
369 查看
参考文章:http://www.cnblogs.com/mgen/archive/2012/03/10/2389509.html
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Collections.ObjectModel;
usingSystem.ComponentModel;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows;
usingSystem.Windows.Input;
usingMicrosoft.Practices.Composite.Presentation.Commands;
usingTransvalueExhibitionSystem.Control;
usingTransvalueExhibitionSystem.Service;
usingTransvalueExhibitionSystem;
usingMicrosoft.Surface.Presentation.Controls;
usingTransvalueExhibitionSystem.Control.BookControl;
usingSystem.Threading;
usingTransvalueExhibitionSystem.Model;
usingSystem.Windows.Controls;
namespaceTransvalueExhibitionSystem.ViewModel
{
publicclassExhibitionPlatformVM:INotifyPropertyChanged
{
privatestaticDictionary<Predicate<object>,Func<Task<UserControl>>>navCondition=newDictionary<Predicate<object>,Func<Task<UserControl>>>();
publicExhibitionPlatformVM()
{
navigate=newDelegateCommand<object>(menuNavigate,arg=>true);
navCondition.Add(navUrl=>navUrl.Equals("book"),async()=>
{
returnawaitTask.Factory.StartNew(()=>
{
BookViewerbook=newBookViewer();
book.Width=1920;
book.Height=800;
book.BookPath=AppDomain.CurrentDomain.BaseDirectory+"Sample_Photos";
returnbook;
},CancellationToken.None,TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
});
navCondition.Add(navUrl=>navUrl.Equals("time"),async()=>
{
returnawaitTask.Factory.StartNew(()=>
{
returnnewHistoryControl();
},CancellationToken.None,TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
});
varmenuBtnData=MainWindowService.getMenuBtnList();
varmenuBtnList=newList<ElementMenuItem>();
menuBtnData.ForEach(menu=>
{
varmenuBtn=newElementMenuItem();
menuBtn.Command=navigate;
menuBtn.CommandParameter=menu.MenuCommandParam;
menuBtn.Header=menu.MenuText;
menuBtnList.Add(menuBtn);
});
this.MenuBtn=newObservableCollection<ElementMenuItem>(menuBtnList);
}
privateObservableCollection<ElementMenuItem>menuBtn;
publicObservableCollection<ElementMenuItem>MenuBtn
{
get
{
returnthis.menuBtn;
}
set
{
if(this.menuBtn!=value)
{
this.menuBtn=value;
OnPropertyChanged("MenuBtn");
}
}
}
publicExhibitionPlatformView{get;set;}
publicICommandnavigate{get;set;}
publicasyncvoidmenuNavigate(objecto)
{
UserControlnavControl=null;
foreach(varconditioninnavCondition.Where(condition=>condition.Key(o)))
navControl=awaitcondition.Value();
View.cvsContent.Children.Clear();
View.cvsContent.Children.Add(navControl);
}
#regionINotifyPropertyChangedMembers
publiceventPropertyChangedEventHandlerPropertyChanged;
privatevoidOnPropertyChanged(stringpropertyName)
{
PropertyChangedEventHandlerhandler=this.PropertyChanged;
if(handler!=null)
{
handler(this,newPropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
主要变化。。
returnawaitTask.Factory.StartNew(()=>
{
returnnewHistoryControl();
},CancellationToken.None,TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
实用Task对Dispatcher进行替代。
可以异步await
相关文章推荐
- !!c# 委托与多线程 -- 相比background来说,本方法针对当需要通过异步代理发起UI操作,而后线程中进行sleep操作的时使用
- C#使用SocketAsyncEventArgs操作套接字的简单异步通讯
- c# 利用AForge.NET组件操作摄像头
- C#的新特性体验(异步方法的同步调用)
- 在C#中利用wmi操作DNS服务器
- c#利用EventHandler注册操作
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- C# 异步操作 async await
- c#关于异步操作。
- [C#.NET][Winform] 文化特性 - 多國語言訊息方塊 / UI Culture–Multiple Languages MessageBox
- [C#] 走进异步编程的世界 - 在 GUI 中执行异步操作
- C#网络Socket的数据发送与接收处理(利用异步)的模板(模式)
- C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件
- UI开发与异步操作
- C#利用NPOI操作Excel文件
- C#利用反射+特性实现简单的实体映射数据库操作类
- C#异步操作样本1
- C#执行异步操作的几种方式总结比较
- async/await 异步操作 .net csharp C#
- C#客户端的异步操作