Xamarin.forms, как отображать ActivityIndicator на каждой странице? - PullRequest
0 голосов
/ 13 июля 2020

Я сделал ActivityIndicator следующим образом: <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}"></ActivityIndicator>, который зависит от параметра IsBusy. У меня вопрос, как добавить этот ActivityIndicator на каждую страницу в центре и сделать его прозрачным?

1 Ответ

1 голос
/ 14 июля 2020

Вы можете создать LoadingPage с ActivityIndicator, используя ControlTemplate .

Например:

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. определите ControlTemplate в своем App.xaml:

<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. используя страницу содержимого, пусть страница расширяет 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...