C# 加载布局屏幕图标Xamarin android

C# 加载布局屏幕图标Xamarin android,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,我想了解一下,当处理发生时,以及当一个页面布局导航到另一个页面布局时,对于如何将加载gif添加到android布局,是否有什么好的想法/教程 我试过用这个- 但它似乎不能很好地与门户类库(PCL)和PCL内部的服务配合使用。我找不到很多这个组件的例子 我看到android为此使用了一个进度对话框,但我希望C#中的Xamarin版本或任何其他聪明的方法。将和HUD添加到android项目中,将和BTProgressHUD添加到iOS项目中 然后您只需要在PCL中创建一个如下所示的接口: publi

我想了解一下,当处理发生时,以及当一个页面布局导航到另一个页面布局时,对于如何将加载gif添加到android布局,是否有什么好的想法/教程

我试过用这个-

但它似乎不能很好地与门户类库(PCL)和PCL内部的服务配合使用。我找不到很多这个组件的例子


我看到android为此使用了一个进度对话框,但我希望C#中的Xamarin版本或任何其他聪明的方法。将和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);
}
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();
}
以及每个项目中的具体实施(iOS示例):

在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());
}
public override bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
SetupIocContainer();
Forms.Init();
formsmap.Init();
窗口=新的UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController=App.GetMainPage().CreateViewController();
window.MakeKeyAndVisible();
返回true;
}
私有void SetupIocContainer()
{
var resolverContainer=新的SimpleContainer();
var app=new XFormsAppiOS();
app.Init(本);
resolverContainer.Register(t=>AppleDevice.CurrentDevice)
.Register(t=>t.Resolve().Display)
//编辑:这似乎根本没有必要,事实上
//导致它在Android上崩溃(但在iOS上工作)
//不知道为什么
//.Register(t=>t.Resolve())
.注册(应用程序)
.Register(t=>resolverContainer);
SetResolver(resolverContainer.GetResolver());
}
在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);
}
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();
}
私人IHudService服务;
公共IHudService服务
{
得到
{
if(hudService==null)
{
hudService=DependencyService.Get();
}
退回此服务;
}
}
专用异步任务设置()
{
this.HudService.Show(“长操作发生”,MaskType.Black);
等待操作();
this.HudService.disclose();
}

和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);
}
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();
}
以及每个项目中的具体实施(iOS示例):

在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());
}
public override bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
SetupIocContainer();
Forms.Init();
formsmap.Init();
窗口=新的UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController=App.GetMainPage().CreateViewController();
window.MakeKeyAndVisible();
返回true;
}
私有void SetupIocContainer()
{
var resolverContainer=新的SimpleContainer();
var app=new XFormsAppiOS();
app.Init(本);
resolverContainer.Register(t=>AppleDevice.CurrentDevice)
.Register(t=>t.Resolve().Display)
//编辑:这似乎根本没有必要,事实上
//导致它在Android上崩溃(但在iOS上工作)
//不知道为什么
//.Register(t=>t.Resolve())
.注册(应用程序)
.Register(t=>resolverContainer);
SetResolver(resolverContainer.GetResolver());
}
在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);
}
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();
}
私人IHudService服务;
公共IHudService服务
{
得到
{
if(hudService==null)
{
hudService=DependencyService.Get();
}
退回此服务;
}
}
专用异步任务设置()
{
this.HudService.Show(“长操作发生”,MaskType.Black);
等待操作();
this.HudService.disclose();
}

为什么要在PCL中使用特定于平台的组件?我尝试了AndHud,但意识到它不值得使用,所以我想尝试另一种方法。是否要添加ProgressBar(LoadingCircle)?Android ProgressBars/LoadingCircles上的stackoverflow有很多问题。诚然,大部分是用Java编写的,但你可以把同样的想法带到Xamarin的C#中去。是的,我是。我看到的示例实际上是Java,但我没有看到使用MVVMCross的示例。为什么要在PCL中使用特定于平台的组件?我尝试了AndHud,但意识到它不值得使用,所以我想尝试另一种方法。您是否希望添加ProgressBar(加载循环)?Android ProgressBars/LoadingCircles上的stackoverflow有很多问题。诚然,大部分是用Java编写的,但你可以把同样的想法带到Xamarin的C#中去。是的,我是。我看到的示例实际上是Java,但我没有看到使用MVVMCross的示例。