为布局屏幕加载图标xamarin android
-
21-12-2019 - |
题
我想知道如何在处理发生处理时如何将加载GIF加载到Android布局时有任何好的想法/教程,并且页面布局导航到另一个页面布局时。
我已经尝试过这个 - adhud
但它似乎与PCL内部的门户类库(PCL)和PCL内的服务不太好。我找不到这个组件的很多例子。
我看到Android使用进度对话框,但我希望在C#中的Xamarin版本或任何其他聪明的方式。
解决方案
为android项目添加 andhud 以及 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 facade的副本,它被添加到两个库中以平滑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<IDevice>(t => AppleDevice.CurrentDevice)
.Register<IDisplay>(t => t.Resolve<IDevice>().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<IHudService>(t => t.Resolve<IHudService>())
.Register<IXFormsApp>(app)
.Register<IDependencyContainer>(t => resolverContainer);
Resolver.SetResolver(resolverContainer.GetResolver());
}
.
在PCL中,您可以确定它并做这样的事情:
private IHudService hudService;
public IHudService HudService
{
get
{
if(hudService == null)
{
hudService = DependencyService.Get<IHudService>();
}
return this.hudService;
}
}
private async Task Setup()
{
this.HudService.Show("Long operation occurring", MaskType.Black);
await Operation();
this.HudService.Dismiss();
}
. 不隶属于 StackOverflow