利用C# 5 新特性 異步操作UI

 
參考文章:http://www.cnblogs.com/mgen/archive/2012/03/10/2389509.html
 
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using TransvalueExhibitionSystem.Control;
using TransvalueExhibitionSystem.Service;
using TransvalueExhibitionSystem;
using Microsoft.Surface.Presentation.Controls;
using TransvalueExhibitionSystem.Control.BookControl;
using System.Threading;
using TransvalueExhibitionSystem.Model;
using System.Windows.Controls;
namespace TransvalueExhibitionSystem.ViewModel
{
    public class ExhibitionPlatformVM : INotifyPropertyChanged
    {
        private static Dictionary<Predicate<object>, Func<Task<UserControl>>> navCondition = new Dictionary<Predicate<object>, Func<Task<UserControl>>>();
        public ExhibitionPlatformVM()
        {
            navigate = new DelegateCommand<object>(menuNavigate, arg => true);
            navCondition.Add(navUrl => navUrl.Equals("book"), async () =>
            {
                return await Task.Factory.StartNew(() =>
                {
                    BookViewer book = new BookViewer();
                    book.Width = 1920;
                    book.Height = 800;
                    book.BookPath = AppDomain.CurrentDomain.BaseDirectory + "Sample_Photos";
                    return book;
                }, CancellationToken.None, TaskCreationOptions.None,
                TaskScheduler.FromCurrentSynchronizationContext());
            });
            navCondition.Add(navUrl => navUrl.Equals("time"), async () =>
            {
                return await Task.Factory.StartNew(() =>
                 {
                     return new HistoryControl();
                 }, CancellationToken.None, TaskCreationOptions.None,
                 TaskScheduler.FromCurrentSynchronizationContext());
            });
            var menuBtnData = MainWindowService.getMenuBtnList();
            var menuBtnList = new List<ElementMenuItem>();
            menuBtnData.ForEach(menu =>
            {
                var menuBtn = new ElementMenuItem();
                menuBtn.Command = navigate;
                menuBtn.CommandParameter = menu.MenuCommandParam;
                menuBtn.Header = menu.MenuText;
                menuBtnList.Add(menuBtn);
            });
            this.MenuBtn = new ObservableCollection<ElementMenuItem>(menuBtnList);
        }

        private ObservableCollection<ElementMenuItem> menuBtn;
        public ObservableCollection<ElementMenuItem> MenuBtn
        {
            get
            {
                return this.menuBtn;
            }
            set
            {
                if (this.menuBtn != value)
                {
                    this.menuBtn = value;
                    OnPropertyChanged("MenuBtn");
                }
            }
        }

        public ExhibitionPlatform View { get; set; }

        public ICommand navigate { get; set; }

        public async void menuNavigate(object o)
        {
            UserControl navControl = null;
            foreach (var condition in navCondition.Where(condition => condition.Key(o)))
                navControl = await condition.Value();
            View.cvsContent.Children.Clear();
            View.cvsContent.Children.Add(navControl);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
 
 
主要變化。。
return await Task.Factory.StartNew(() =>
                 {
                     return new HistoryControl();
                 }, CancellationToken.None, TaskCreationOptions.None,
                 TaskScheduler.FromCurrentSynchronizationContext());
實用Task 對Dispatcher 進行替代。
可以異步await 
 

發佈了38 篇原創文章 · 獲贊 35 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章