سؤال

أريد معرفة ما إذا كانت هناك أي أفكار/برامج تعليمية جيدة حول كيفية إضافة صورة GIF للتحميل إلى تخطيط Android عند حدوث المعالجة وعندما ينتقل تخطيط الصفحة إلى تخطيط صفحة آخر.

لقد حاولت استخدام هذا - وهود

ولكن يبدو أنها لا تعمل بشكل جيد مع مكتبات Portal Class (PCL) والخدمات الموجودة داخل PCL.لم أتمكن من العثور على الكثير من الأمثلة مع هذا المكون.

أرى أن Android يستخدم مربع حوار التقدم لهذا الغرض ولكني كنت أتمنى الحصول على إصدار Xamarin في C# أو أي طريقة ذكية أخرى للقيام بذلك.

هل كانت مفيدة؟

المحلول

يضيف وHUD إلى مشروع 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
    }
}

وعلى أندرويد:

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<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();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top