【发布时间】:2017-08-08 19:58:25
【问题描述】:
我在整个应用程序中都有活动指示器,但我发现这是重复的。我想知道是否可以定义一个活动指示器并在整个应用程序中使用它?
下面是我在应用程序中拥有的五个活动指标之一的代码。除了标签文字变化之外,每个人都完全一样。
<AbsoluteLayout x:Name="ActivityInd" IsVisible="False" BackgroundColor="Black" Opacity=".75" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" VerticalOptions="Center" HorizontalOptions="Center">
<ActivityIndicator x:Name="Activityblocker" Color="White"/>
<Label Text="Processing Request" TextColor="White"/>
</StackLayout>
</AbsoluteLayout>
如果可能,最好将其放置在哪里,以便可以从应用程序中的任何位置调用它?
编辑:
一切正常,并用我创建的控件替换了所有活动指示器。以下是代码,希望这可以帮助将来的人。
使用绑定控制 xaml
<?xml version="1.0" encoding="UTF-8"?>
<AbsoluteLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Myapp.ActivityBlocker"
x:Name="ActivityIndAL" IsVisible="{Binding IsBusy, Source={x:Reference ActivityIndAL}}" BackgroundColor="Black" Opacity=".75" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" VerticalOptions="Center" HorizontalOptions="Center">
<ActivityIndicator x:Name="Activityblocker" Color="White" IsEnabled="{Binding IsBusy, Source={x:Reference ActivityIndAL}}" IsRunning="{Binding IsBusy, Source={x:Reference ActivityIndAL}}"/>
<Label Text="{Binding Text, Source={x:Reference ActivityIndAL}}" x:Name="ActivityIndLabel" TextColor="White"/>
</StackLayout>
</AbsoluteLayout>
控制 xaml.cs
public partial class ActivityBlocker : AbsoluteLayout
{
public ActivityBlocker()
{
InitializeComponent();
}
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(ActivityBlocker));
public static readonly BindableProperty Running = BindableProperty.Create(nameof(IsBusy), typeof(bool), typeof(ActivityBlocker), false);
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
public bool IsBusy
{
get
{
return (bool)GetValue(Running);
}
set
{
SetValue(Running, value);
}
}
}
如何使用该控件:您将xmlns:control="clr-namespace:MyApp" 添加到您要使用该控件的内容页面中。现在添加控件和文本。注意:IsBusy="False" 不是必需的,因为它在BindableProperty.Create 中的默认值为 false。通过这样做indicator.IsBusy = true;
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:control="clr-namespace:MyApp"
....
<ContentPage.Content>
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<control:ActivityBlocker x:Name="indicator" Text="Processing Request" IsBusy="False"/>
</AbsoluteLayout>
</ContentPage.Content>
....
【问题讨论】:
-
可以创建自定义控件
-
@YuriS 这太完美了。不知道您可以在 Xamarin 中创建自定义控件。 this 帮助指南所说的我会做的差不多。正确的?如果是这样,我会试一试并在我得到一些工作后发布代码。谢谢。
-
是的,这是一篇很棒的文章。他正在做 Grid 而不是 ContentView。如果你想要那个布局,你会做 AbsoluteLayout,但 Grid 也适合你。我还会质疑您是否需要在里面使用 StackLayout。您将创建例如 BusyText 的可绑定属性并将其绑定到您的标签文本
-
@YuriS 我已经在问题中添加了代码。随时给我任何反馈。我还尝试使用网格并删除堆栈布局,但我很难让活动指示器和标签居中。它一直在左上角,没有覆盖整个视图。
标签: xamarin xamarin.forms