【问题标题】:Xamarin.forms how to show ActivityIndicator in every Page?Xamarin.forms 如何在每个页面中显示 ActivityIndi​​cator?
【发布时间】:2020-07-13 12:51:04
【问题描述】:

我制作了一个这样的 ActivityIndi​​cator: <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}"></ActivityIndicator> 取决于 IsBusy 参数。我的问题是如何在中心的每个页面上添加这个 ActivityIndi​​cator 并保持透明?

【问题讨论】:

标签: xamarin.forms activity-indicator


【解决方案1】:

您可以使用ControlTemplate 创建一个带有ActivityIndi​​cator 的LoadingPage。

例如:

1.创建一个LoadingPage

public class LoadingPage: ContentPage
{
    public static readonly BindableProperty RootViewModelProperty =
       BindableProperty.Create(
           "RootViewModel", typeof(object), typeof(LoadingPage),
           defaultValue: default(object));

    public object RootViewModel
    {
        get { return (object)GetValue(RootViewModelProperty); }
        set { SetValue(RootViewModelProperty, value); }
    }
}

2.在您的 App.xaml 中定义一个 ControlTemplate:

<Application.Resources>
    <ResourceDictionary>
      <ControlTemplate x:Key="LoaderViewTemplate">
        <AbsoluteLayout Padding = "0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
           <ContentPresenter AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All"/>
           <ActivityIndicator Color= "White" IsRunning= "{TemplateBinding RootViewModel.IsBusy}" AbsoluteLayout.LayoutBounds=".5,.5,100,100" AbsoluteLayout.LayoutFlags="PositionProportional" />                   
        </AbsoluteLayout>
      </ControlTemplate>
    </ResourceDictionary>
</Application.Resources>
 

3.在contentpage中使用,让页面扩展LoadingPage:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyPage: LoadingPage
{
    public MyPage()
    {
        InitializeComponent();
        ViewModel viewmodel = new ViewModel () { IsBusy = true };// your viewmodel must have property IsBusy
        BindingContext = viewmodel;
    }
}

MyPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<local:LoadingPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         xmlns:local="clr-namespace:EntryCa"
         RootViewModel="{Binding}"
         ControlTemplate="{StaticResource LoaderViewTemplate}"
         x:Class="EntryCa.MyPage">
   <ContentPage.Content>
     <StackLayout>
        <Label Text="Welcome to Xamarin.Forms!"
            VerticalOptions="Start" 
            HorizontalOptions="Start" />
     </StackLayout>
   </ContentPage.Content>
</local:LoadingPage>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多