您的位置:首页 > 产品设计 > UI/UE

利用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

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: