码迷,mamicode.com
首页 > 移动开发 > 详细

Xamarin.IOS中MvvmCross的plugin使用方式

时间:2017-12-09 14:00:41      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:image   key   cache   uiimage   option   一个   ble   dict   bind   

MvvmCross从4.0之后plugin的注册接口做了重构,网上例子不多,这里给个参考。本例子使用MvvmCross.Plugins.DownloadCache和MvvmCross.Plugins.File.PluginLoader来显示网上的一个图片。

1,View里先给个UIKit.UIImageView用来做界面显示,命名为 _imageView

2,使用nuget,安装MvvmCross,MvvmCross.Plugin.DownloadCache,MvvmCross.Plugin.File

3,setup代码中加载plugin,代码如下

   public class Setup : MvxIosSetup
    {
        public Setup(MvxApplicationDelegate appDelegate, IMvxIosViewPresenter presenter)
            : base(appDelegate, presenter)
        {
        }

        protected override IMvxApplication CreateApp ()
        {
            return new App();
        }
protected override void AddPluginsLoaders(MvxLoaderPluginRegistry registry) {
       //注册plugin,一定要对应好接口与平台实现 registry.Register
<MvvmCross.Plugins.DownloadCache.PluginLoader, MvvmCross.Plugins.DownloadCache.iOS.Plugin>(); registry.Register<MvvmCross.Plugins.File.PluginLoader, MvvmCross.Plugins.File.iOS.Plugin>(); base.AddPluginsLoaders(registry); } public override void LoadPlugins(IMvxPluginManager pluginManager) { base.LoadPlugins(pluginManager);        //加载plugin pluginManager.EnsurePluginLoaded<MvvmCross.Plugins.DownloadCache.PluginLoader>(); pluginManager.EnsurePluginLoaded<MvvmCross.Plugins.File.PluginLoader>(); } }

4.View和ViewModel的绑定

Viewmodel定义如下

    class TipViewModel : MvxViewModel
    {

        private string _imagePath = @"http://i.imgur.com/VTC3ptT.png";
        public TipViewModel()
        {
        }

        public override void Start()
        {
        }

        public string TestImagePath
        {
            get => _imagePath;
            set => SetProperty(ref _imagePath, value, "TestImagePath");
        }
    }

View绑定代码

    public partial class TipViewController : MvxViewController<TipViewModel>
    {
        private readonly MvxImageViewLoader _loader;
        public TipViewController() : base("TipView", null)
        {
            _loader = new MvxImageViewLoader(() => this._imageView, () => { });
        }
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren‘t in use.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.CreateBinding(this._loader).To<TipViewModel>(vm => vm.TestImagePath).Apply();
        }
    }

 5,额外代码:

AppDelegate

技术分享图片
    [Register("AppDelegate")]
    public class AppDelegate : MvxApplicationDelegate
    {
        // class-level declarations

        public override UIWindow Window
        {
            get;
            set;
        }

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            Window = new UIWindow(UIScreen.MainScreen.Bounds);

            var presenter = new MvxIosViewPresenter(this, Window);

            var setup = new Setup(this, presenter);
            setup.Initialize();

            var startup = Mvx.Resolve<IMvxAppStart>();
            startup.Start();

            Window.MakeKeyAndVisible();

            return true;
        }
     
    }
View Code

 App

技术分享图片
    public class App : MvxApplication
    {
        public override void Initialize()
        {
            base.Initialize();

            RegisterNavigationServiceAppStart<TipViewModel>();
        }
    }
View Code

 

Xamarin.IOS中MvvmCross的plugin使用方式

标签:image   key   cache   uiimage   option   一个   ble   dict   bind   

原文地址:http://www.cnblogs.com/jiayanyan/p/8011007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!