문제

처리가 발생한 경우 및 페이지 레이아웃이 다른 페이지 레이아웃으로 이동할 때 Android 레이아웃에 GIF를 추가하는 방법에 대한 좋은 아이디어 / 자습서가 있는지 알아 내고 싶습니다.

이 this - Andhud

를 사용해 보았습니다.

그러나 PCL (Portal Class Librarys) 및 PCL 내부의 서비스에서는 잘 작동하지 않는 것처럼 보입니다.이 구성 요소로 많은 예제를 찾을 수 없습니다.

Android가 진행 대화 상자를 사용하지만 C # 또는이 작업을 수행하는 다른 영리한 방식으로 Xamarin 버전을 기대하고있었습니다.

도움이 되었습니까?

해결책

Android 프로젝트 및 btprogresshud andhud 을 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
    }

}
.

이것은 기본적으로 API 차이점을 부드럽게하기 위해 두 라이브러리에 추가 된 xhud.hud 외관의 사본입니다.

그런 다음 플랫폼 별 프로젝트의 진입 점에 서비스를 등록하고 (이 경우 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