正在加载布局屏幕Xamarin android的图标

我想知道是否有任何好的想法/教程如何在处理发生时以及当页面布局导航到另一个页面布局时将加载gif添加到android布局。

我试过用这个 – AndHud

但似乎它与PCL中的Portal Class Librarys(PCL)和服务不兼容。 我找不到这个组件的很多例子。

我看到android使用了一个进度对话框,但我希望在C#中使用Xamarin版本或任何其他聪明的方法。

AndHUD添加到Android项目,将BTProgressHUD添加到iOS项目。

然后你只需要在PCL中创建一个如下界面:

public enum MaskType { None = 1, Clear, Black, Gradient } public interface IHudService { void Show(string message, MaskType maskType, int progress = -1); void Dismiss(); void ShowToast(string message, bool showToastCentered = true, double timeoutMs = 1000); void ShowToast(string message, MaskType maskType, bool showToastCentered = true, double timeoutMs = 1000); } 

每个项目中的具体实现(iOS示例):

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using BigTed; using Foundation; using MyExample.Services; using UIKit; [assembly: Xamarin.Forms.Dependency(typeof(MyExample.iOS.Services.HudService))] namespace MyExample.iOS.Services { public class HudService : IHudService { public HudService() { } #region IHudService Members public void Show(string message, MaskType maskType, int progress) { float p = (float)progress / 100f; BTProgressHUD.Show(message, p, (ProgressHUD.MaskType)maskType); } public void Dismiss() { BTProgressHUD.Dismiss(); } public void ShowToast(string message, bool showToastCentered = true, double timeoutMs = 1000) { BTProgressHUD.ShowToast(message, showToastCentered, timeoutMs); } public void ShowToast(string message, MaskType maskType, bool showToastCentered = true, double timeoutMs = 1000) { BTProgressHUD.ShowToast(message, (ProgressHUD.MaskType)maskType, showToastCentered, timeoutMs); } #endregion } } 

并且,在Android上:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using AndroidHUD; using MyExample.Services; using Xamarin.Forms; using XHUD; [assembly: Xamarin.Forms.Dependency(typeof(MyExample.Android.Services.HudService))] namespace MyExample.Android.Services { public class HudService : IHudService { //Although, not well documented, for Xamarin.Forms, "Forms.Context" is the current activity public HudService() { } #region IHudService Members public void Show(string message, MyExample.Services.MaskType maskType, int progress) { AndHUD.Shared.Show(Forms.Context, message, progress, (AndroidHUD.MaskType)maskType); } public void Dismiss() { AndHUD.Shared.Dismiss(Forms.Context); } public void ShowToast(string message, bool showToastCentered = true, double timeoutMs = 1000) { AndHUD.Shared.ShowToast(Forms.Context, message, (AndroidHUD.MaskType)MyExample.Services.MaskType.Black, TimeSpan.FromSeconds(timeoutMs / 1000), showToastCentered); } public void ShowToast(string message, MyExample.Services.MaskType maskType, bool showToastCentered = true, double timeoutMs = 1000) { AndHUD.Shared.ShowToast(Forms.Context, message, (AndroidHUD.MaskType)maskType, TimeSpan.FromSeconds(timeoutMs / 1000), showToastCentered); } #endregion } } 

这基本上只是XHUD.HUD外观的一个副本,它被添加到两个库中以平滑API差异。

然后,在特定于平台的项目(在本例中为AppDelegate.cs)的入口点注册您的服务,并从PCL调用它。 就我而言,我使用的是Xamarin.Forms.Labs,因此您注册它的方法可能会有所不同。

 public override bool FinishedLaunching(UIApplication app, NSDictionary options) { SetupIocContainer(); Forms.Init(); FormsMaps.Init(); window = new UIWindow(UIScreen.MainScreen.Bounds); window.RootViewController = App.GetMainPage().CreateViewController(); window.MakeKeyAndVisible(); return true; } private void SetupIocContainer() { var resolverContainer = new SimpleContainer(); var app = new XFormsAppiOS(); app.Init(this); resolverContainer.Register(t => AppleDevice.CurrentDevice) .Register(t => t.Resolve().Display) //EDIT: this does not seem necessary after all and actually //causes it to crash on Android (but works on iOS) //not sure why //.Register(t => t.Resolve()) .Register(app) .Register(t => resolverContainer); Resolver.SetResolver(resolverContainer.GetResolver()); } 

在PCL中,您可以实例化它并执行以下操作:

 private IHudService hudService; public IHudService HudService { get { if(hudService == null) { hudService = DependencyService.Get(); } return this.hudService; } } private async Task Setup() { this.HudService.Show("Long operation occurring", MaskType.Black); await Operation(); this.HudService.Dismiss(); }