简体   繁体   English

在加载视图内容时显示活动指示器 Xamarin Forms

[英]Show a activity indicator while View content is loading in Xamarin Forms

Is there a way to show an activity indicator while Page content view is rendering or loading?, i ask this because when i've a lot of controls in a page, and i want to navigate to that page, it takes few second to go to the page.有没有办法在页面内容视图呈现或加载时显示活动指示器?我问这个是因为当我在页面中有很多控件并且我想导航到该页面时,go 需要几秒钟到页面。 So i'd like to kwow if there is a way to navigate the page instant and when the page appear show activity indicator loading the content, and when the content get loaded, show it.所以我想知道是否有一种方法可以即时导航页面,当页面出现时显示加载内容的活动指示器,以及在加载内容时显示它。

Is there a way to show an activity indicator while Page content view is rendering or loading?有没有办法在页面内容视图呈现或加载时显示活动指示器?

  1. we need to create service name ILodingPageService interface in PCL.我们需要在 PCL 中创建服务名称 ILodingPageService 接口。

     public interface ILodingPageService { void InitLoadingPage(ContentPage loadingIndicatorPage = null); void ShowLoadingPage(); void HideLoadingPage(); }

2.creating Transparent Page to display Activity indicator. 2.创建透明页面以显示活动指示器。

<ContentPage.Content>
    <StackLayout
        Padding="30"
        BackgroundColor="Black"
        HorizontalOptions="Center"
        VerticalOptions="Center">

        <ActivityIndicator IsRunning="True" Color="White" />

        <Label
            FontAttributes="Bold"
            Text="Loading..."
            TextColor="White" />

    </StackLayout>
</ContentPage.Content>

3.implement ILodingPageService interface in Android platform. 3.在Android平台实现ILodingPageService接口。

[assembly: Xamarin.Forms.Dependency(typeof(LodingPageServiceDroid))]

namespace IndicatorApp.Droid {命名空间 IndicatorApp.Droid {
public class LodingPageServiceDroid: ILodingPageService {公共 class LodingPageServiceDroid:ILodingPageService {

    private Android.Views.View _nativeView;
    private Dialog _dialog;
    private bool _isInitialized;
    public void HideLoadingPage()
    {
        // Hide the page

        _dialog.Hide();
    }

    public void InitLoadingPage(ContentPage loadingIndicatorPage = null)
    {
        // check if the page parameter is available
        if (loadingIndicatorPage != null)
        {
            // build the loading page with native base
            loadingIndicatorPage.Parent = Xamarin.Forms.Application.Current.MainPage;
            loadingIndicatorPage.Layout(new Rectangle(0, 0,
            Xamarin.Forms.Application.Current.MainPage.Width,
            Xamarin.Forms.Application.Current.MainPage.Height));
            var renderer = loadingIndicatorPage.GetOrCreateRenderer();
            _nativeView = renderer.View;
            _dialog = new Dialog(CrossCurrentActivity.Current.Activity);

            _dialog.RequestWindowFeature((int)WindowFeatures.NoTitle);

            _dialog.SetCancelable(false);

            _dialog.SetContentView(_nativeView);

            Window window = _dialog.Window;

            window.SetLayout(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);

            window.ClearFlags(WindowManagerFlags.DimBehind);

            window.SetBackgroundDrawable(new ColorDrawable(Android.Graphics.Color.Transparent));
            _isInitialized = true;
        }
    }

    public void ShowLoadingPage()
    {
        // check if the user has set the page or not
        if (!_isInitialized)
            InitLoadingPage(new LoadingIndicatorPage1()); // set the default page

        // showing the native loading page

        _dialog.Show();
    }
}
internal static class PlatformExtension
{
    public static IVisualElementRenderer GetOrCreateRenderer(this VisualElement bindable)
    {
        var renderer = XFPlatform.GetRenderer(bindable);
        if (renderer == null)
        {
            renderer = XFPlatform.CreateRendererWithContext(bindable, CrossCurrentActivity.Current.Activity);

            XFPlatform.SetRenderer(bindable, renderer);

        }

        return renderer;

    }

}

} }

I create new simple at githun, you can download it.我在 githun 创建了新的 simple,你可以下载它。

https://github.com/CherryBu/activityindicator https://github.com/CherryBu/activityindicator

If my reply solve your issue, please remember to mark answer for my reply, thanks.如果我的回复解决了您的问题,请记得在我的回复中标记答案,谢谢。

The screenshot is here:截图在这里: 在此处输入图像描述

  1. In your view model, add a boolean property to indicate if the page is loading.在您的视图 model 中,添加 boolean 属性以指示页面是否正在加载。
  2. Implement INotifyPropertyChanged in the model and fire PropertyChanged event when you change the property (at setter code).在 model 中实现 INotifyPropertyChanged 并在更改属性时触发 PropertyChanged 事件(在设置器代码处)。
  3. Bind the "IsRunning" property of your activity indicator to the property you added in the first step.将活动指示器的“IsRunning”属性绑定到您在第一步中添加的属性。

now, at the time your content starts loading set it to true and when you finish loading set it to false.现在,在您的内容开始加载时将其设置为 true,当您完成加载时将其设置为 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM