加载布局屏幕 Xamarin android 的图标 [英] Loading Icon for layout screen Xamarin android

查看:23
本文介绍了加载布局屏幕 Xamarin android 的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to find out if there is any good ideas/tutorials on how to add loading gif to an android layout when processing happens and when a page layout navigates to another page layout.

我试过使用这个 - AndHud

但它似乎不适用于门户类库 (PCL) 和 PCL 内的服务.我找不到很多有关此组件的示例.

But it seems like it doesn't work well with Portal Class Librarys (PCL) and services inside the PCL. I couldn't find a lot of examples with this component.

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

I see android uses a progress dialog for this but I was hoping for a Xamarin version in C# or any other clever way of doing this.

推荐答案

AndHUD 添加到 Android 项目并将 BTProgressHUD 添加到您的 iOS 项目.

Add AndHUD to the Android project and BTProgressHUD to your iOS project.

然后你只需要像这样在PCL中创建一个接口:

Then you just need to create an interface in the PCL like this:

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 示例):

and concrete implementations in each of the projects (iOS example):

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 上:

And, on 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 差异.

This is basically just a copy of the XHUD.HUD facade which was added to both libraries to smooth the API differences.

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

Then, register your service at the entry point for the platform-specific projects (in this case, AppDelegate.cs) and call it from the PCL. In my case, I am using Xamarin.Forms.Labs so your method of registering it may vary.

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 中,您可以实例化它并执行以下操作:

In the PCL, you can instantiate it and do something like this:

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();
}

这篇关于加载布局屏幕 Xamarin android 的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆