CM IResult and Coroutines 学习笔记

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using Caliburn.Micro;
using CMApplication1.Loaders;
using Xceed.Wpf.Toolkit;
namespace CMApplication1.ViewModels
{
    [Description("测试 IResult")]
    [Export(typeof(TestViewModel))]
    public class IResultTestViewModel : PropertyChangedBase, IShell
    {
        //public void Test(BusyIndicator bi)
        //{
        //    var lloader = new ImageLoader("正在加载......");
        //    lloader.Completed += (loader, e) =>
        //    {
        //        bi.IsBusy = false;
        //        Console.WriteLine(loader);
        //        Console.WriteLine(e);
        //    };
        //    lloader.Execute(new CoroutineExecutionContext { View = bi });
        //}
        public IEnumerable<IResult> Test(BusyIndicator bi)
        {
            var lloader = new ImageLoader2("加载");
            lloader.Completed += (loader, e) =>
            {
                bi.Dispatcher.BeginInvoke(new System.Action(() =>
                {
                    bi.IsBusy = false;
                }));
                Console.WriteLine(loader);
                Console.WriteLine(e);
            };
            //同步操作
            //yield return lloader;//.Execute(new CoroutineExecutionContext { View = bi });
            //异步操作
            var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                lloader.ExecuteAsync(new CoroutineExecutionContext { View = bi });
            });
            yield return task.AsResult();
        }
        //public IEnumerable<IResult> Test(BusyIndicator bi)
        //{
        //    yield return BusyLoader.Show().ExecuteAsync(new CoroutineExecutionContext { View = bi }).AsResult();
                       
        //    var lloader = new ImageLoader3();
        //    lloader.Completed += (loader, e) =>
        //    {
        //        Console.WriteLine(loader);
        //        Console.WriteLine(e);
        //    };
        //    yield return lloader.ExecuteAsync(new CoroutineExecutionContext { }).AsResult();
        //    yield return BusyLoader.Hide().ExecuteAsync(new CoroutineExecutionContext { View = bi }).AsResult();
        //}
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using System.Windows;
using Xceed.Wpf.Toolkit;
using System.Net;
using System.IO;
namespace CMApplication1.Loaders
{
    public class ImageLoader : IResult
    {
        readonly string message;
        readonly bool hide;
        public ImageLoader(string message)
        {
            this.message = message;
        }
        public ImageLoader(bool hide)
        {
            this.hide = hide;
        }
        public void Execute(CoroutineExecutionContext context)
        {
            var view = context.View as FrameworkElement;
            while (view != null)
            {
                var busyIndicator = view as BusyIndicator;
                if (busyIndicator != null)
                {
                    if (!string.IsNullOrEmpty(message))
                        busyIndicator.BusyContent = message;
                    busyIndicator.IsBusy = !hide;
                    break;
                }
            }
            view = view.Parent as FrameworkElement;
            WebClient client = new WebClient();
            client.DownloadFileCompleted += (a, b) =>
            {
                Completed(this, new ResultCompletionEventArgs());
            };
            client.DownloadFileAsync(new Uri("http://www.cmono.net/content/plugins/kl_album/upload/201310/7c92e19ab0d99dd5518e3656070f7b152013101319385228654.png"), DateTime.Now.ToString("yyyy-MM-dd-hh-mm-sss") + ".png", new { });
           
        }
        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
        public static IResult Show(string message = null)
        {
            return new ImageLoader(message);
        }
        public static IResult Hide()
        {
            return new ImageLoader(true);
        }
    }
    public class ImageLoader2 : IResult
    {
        readonly string message;
        readonly bool hide;
        public ImageLoader2(string message)
        {
            this.message = message;
        }
        public ImageLoader2(bool hide)
        {
            this.hide = hide;
        }
        public void Execute(CoroutineExecutionContext context)
        {
            var view = context.View as FrameworkElement;
            while (view != null)
            {
                var busyIndicator = view as BusyIndicator;
                if (busyIndicator != null)
                {
                    busyIndicator.Dispatcher.BeginInvoke(new System.Action(() => {
                        if (!string.IsNullOrEmpty(message))
                            busyIndicator.BusyContent = message;
                        busyIndicator.IsBusy = !hide;
                    }));
                    break;
                }
            }
            WebClient client = new WebClient();
            client.DownloadFileCompleted += (a, b) =>
            {
                Completed(this, new ResultCompletionEventArgs());
            };
            client.DownloadFileAsync(new Uri("http://www.cmono.net/content/plugins/kl_album/upload/201310/7c92e19ab0d99dd5518e3656070f7b152013101319385228654.png"), DateTime.Now.ToString("yyyy-MM-dd-hh-mm-sss") + ".png", new { });
        }
        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
        public static IResult Show(string message = null)
        {
            return new ImageLoader(message);
        }
        public static IResult Hide()
        {
            return new ImageLoader(true);
        }
    }
    public class ImageLoader3 : IResult
    {
        readonly string message;
        readonly bool hide;
        public ImageLoader3()
        {
        }
        public ImageLoader3(string message)
        {
            this.message = message;
        }
        public ImageLoader3(bool hide)
        {
            this.hide = hide;
        }
        public void Execute(CoroutineExecutionContext context)
        {
            WebClient client = new WebClient();
            client.DownloadFileCompleted += (a, b) =>
            {
                Completed(this, new ResultCompletionEventArgs());
            };
            client.DownloadFileAsync(new Uri("http://www.cmono.net/content/plugins/kl_album/upload/201310/7c92e19ab0d99dd5518e3656070f7b152013101319385228654.png"), DateTime.Now.ToString("yyyy-MM-dd-hh-mm-sss")+".png", new { });
        }
        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
        public static IResult Show(string message = null)
        {
            return new ImageLoader(message);
        }
        public static IResult Hide()
        {
            return new ImageLoader(true);
        }
    }
    public class BusyLoader : IResult
    {
        readonly string message;
        readonly bool hide;
        public BusyLoader(string message)
        {
            this.message = message;
        }
        public BusyLoader(bool hide)
        {
            this.hide = hide;
        }
        public void Execute(CoroutineExecutionContext context)
        {
            var view = context.View as FrameworkElement;
            while (view != null)
            {
                var busyIndicator = view as BusyIndicator;
                if (busyIndicator != null)
                {
                    if (!string.IsNullOrEmpty(message))
                        busyIndicator.BusyContent = message;
                    busyIndicator.IsBusy = !hide;
                    break;
                }
                view = view.Parent as FrameworkElement;
            }
            Completed(this, new ResultCompletionEventArgs());
        }
        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
        public static IResult Show(string message = null)
        {
            return new BusyLoader(message);
        }
        public static IResult Hide()
        {
            return new BusyLoader(true);
        }
    }
}


知识共享许可协议
《CM IResult and Coroutines 学习笔记》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     161.30ms