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