Xamarin.IOS中MvvmCross的plugin使用方式

來源:互聯網
上載者:User

標籤: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使用方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.