Xamarin.forms 如何在每个页面显示ActivityIndicator?
Xamarin.forms how to show ActivityIndicator in every Page?
我制作了一个这样的 ActivityIndicator:
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}"></ActivityIndicator>
这取决于 IsBusy 参数。我的问题是如何在中心的每个页面上添加这个 ActivityIndicator 并保持透明?
您可以使用 ControlTemplate.
创建一个带有 ActivityIndicator 的 LoadingPage
例如:
1.create一个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.define 您的 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.using 在一个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>
我制作了一个这样的 ActivityIndicator:
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}"></ActivityIndicator>
这取决于 IsBusy 参数。我的问题是如何在中心的每个页面上添加这个 ActivityIndicator 并保持透明?
您可以使用 ControlTemplate.
创建一个带有 ActivityIndicator 的 LoadingPage例如:
1.create一个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.define 您的 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.using 在一个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>