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