我有一个ActivityIndicator,我在许多这样的不同页面中都有:
<ActivityIndicator HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
Color="DarkBlue" IsVisible="{Binding IsLoading}" IsRunning="{Binding IsLoading}">
</ActivityIndicator>-
我正在尝试通过创建一个类似于以下内容的“ ContentView”来使此组件更易于重用:
Loading.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentView 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"
x:Class="Framework.Controls.Loading" x.Name="Loading">
<ContentView.Content>
<ActivityIndicator HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Color="DarkBlue"
IsVisible="{Binding Source={x:Reference Name=Loading},Path=IsLoading}"
IsRunning="{Binding Source={x:Reference Name=Loading},Path=IsLoading}">
</ActivityIndicator>
</ContentView.Content>
</ContentView>
我正在尝试使用以下代码:
<controls:Loading IsLoading="{Binding IsLoading}"></controls:Loading>
我遇到的问题是我不确定如何创建绑定,以便可以在ActivityIndicator上设置IsVisible和IsRunning属性。
我在组件后面的代码如下:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Loading : ContentView
{
public static readonly BindableProperty LoadingIndicatorProperty =
BindableProperty.Create(
propertyName: nameof(IsLoading), typeof(bool),
typeof(Loading), default(string), BindingMode.OneWayToSource);
public bool IsLoading
{
get => (bool)GetValue(LoadingIndicatorProperty);
set => SetValue(LoadingIndicatorProperty, value);
}
public Loading()
{
InitializeComponent();
}
}
使用当前代码,我遇到构建错误,因此这里显然不正确。
错误如下:
如何正确创建将应用于“活动指示器”上的IsVisible和IsRunning属性的绑定?而且为什么编译器不喜欢ContentView'x.Name'属性?
答案 0 :(得分:1)
首先,您有一个错字:
现在是x:Name
,而您有x.Name
因此,在您的视图中:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView 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"
x:Class="Framework.Controls.Loading"
x:Name="Loading">
<ContentView.Content>
<ActivityIndicator HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Color="DarkBlue"
IsVisible="{Binding IsLoading"
IsRunning="{Binding IsLoading">
</ActivityIndicator>
</ContentView.Content>
</ContentView>
然后,在代码背后,您可以:
XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Loading : ContentView
{
public static readonly BindableProperty LoadingIndicatorProperty =
BindableProperty.Create(
propertyName: nameof(IsLoading), typeof(bool),
typeof(Loading), default(string));
public bool IsLoading
{
get => (bool)GetValue(LoadingIndicatorProperty);
set => SetValue(LoadingIndicatorProperty, value);
}
public Loading()
{
InitializeComponent();
BindingContext=this;
}
}
答案 1 :(得分:0)
我创建了一个Xamarin.Forms控件,它完全可以做到这一点以及执行更多操作(处理错误和空状态)。 您可以在这里找到它:
https://github.com/roubachof/Sharpnado.Presentation.Forms#taskloaderview
详细解释它的博客文章在这里:
https://www.sharpnado.com/taskloaderview-async-init-made-easy/