有没有办法在页面内容视图渲染或加载时显示活动指示器?我问这个是因为当我在页面中有很多控件并且我想导航到该页面时,需要几秒钟的时间到页面。所以我想知道是否有一种方法可以即时导航页面,并且当页面出现时显示加载内容的活动指示器,以及当内容加载时显示它。
有没有办法在页面内容视图渲染或加载时显示活动指示器?
-
我们需要在PCL中创建服务名称ILodingPageService接口。
public interface ILodingPageService
{
void InitLoadingPage(ContentPage loadingIndicatorPage = null);
void ShowLoadingPage();
void HideLoadingPage();
}
-
创建透明页面以显示活动指示器。
<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>
-
在Android平台上实现ILodingPageService接口。
[assembly: Xamarin.Forms.Dependency(typeof(LodingPageServiceDroid))]
namespace IndicatorApp.Droid
{
public 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;
}
}
}
截图在这里:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)