Xamarin.IOS中MvvmCross的plugin使用方式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xamarin.IOS中MvvmCross的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; } }
App
public class App : MvxApplication { public override void Initialize() { base.Initialize(); RegisterNavigationServiceAppStart<TipViewModel>(); } }
以上是关于Xamarin.IOS中MvvmCross的plugin使用方式的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin iOS:MvvMCross 绑定导航项的后退按钮
MvvmCross 4 和 Xamarin.iOS -> 使用 Storyboard 时如何从 Core 加载视图控制器?
Xamarin iOS - MVVMCross:无法使用 ViewModel 中的命令连接自定义单元格中的按钮