Xamarin.IOS中MvvmCross的plugin使用方式_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Xamarin.IOS中MvvmCross的plugin使用方式

Xamarin.IOS中MvvmCross的plugin使用方式

 2017/12/9 15:25:38  禾刀  程序员俱乐部  我要评论(0)
  • 摘要:MvvmCross从4.0之后plugin的注册接口做了重构,网上例子不多,这里给个参考。本例子使用MvvmCross.Plugins.DownloadCache和MvvmCross.Plugins.File.PluginLoader来显示网上的一个图片。1,View里先给个UIKit.UIImageView用来做界面显示,命名为_imageView2,使用nuget,安装MvvmCross,MvvmCross.Plugin.DownloadCache,MvvmCross.Plugin
  • 标签:使用 iOS Plugin 方式

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;
        }
     
    }
logs_code_collapse">View Code

 App

    public class App : MvxApplication
    {
        public override void Initialize()
        {
            base.Initialize();

            RegisterNavigationServiceAppStart<TipViewModel>();
        }
    }
View Code

 

发表评论
用户名: 匿名