Xaml ActivityIndicator的可重用ContentView绑定
我有一个ActivityIndicator,我在许多不同的页面中都有,如下所示:Xaml ActivityIndicator的可重用ContentView绑定,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我有一个ActivityIndicator,我在许多不同的页面中都有,如下所示: <ActivityIndicator HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Color="DarkBlue" IsVisible="{Binding IsLoading}" IsRunning="{Binding IsLoading}"> </Activit
<ActivityIndicator HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
Color="DarkBlue" IsVisible="{Binding IsLoading}" IsRunning="{Binding IsLoading}">
</ActivityIndicator>-
<controls:Loading IsLoading="{Binding IsLoading}"></controls:Loading>
[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();
}
}
在当前代码中,我会遇到构建错误,因此这里显然不存在一些问题
错误如下:
在Loading.xaml文件中,我收到一条消息,该消息表示对于x.Name=Loading,在xmls中找不到类型x,并且在类型“x”中找不到可附加属性“Name”。
在我尝试使用控件的页面上,我得到一个错误:没有找到“IsLoading”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配。
如何正确创建将应用于我的活动指示器上的IsVisible和IsRunning属性的绑定?为什么编译器不喜欢ContentView的“x.Name”属性 首先,您有一个输入错误: 它是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>
我创建了一个Xamarin.Forms控件,该控件正好可以处理错误和空状态。 你可以在这里找到它: 详细解释的博客帖子如下:
谢谢还有一件事我弄错了,我的BindableProperty和我在使用控件的页面上使用的绑定名称必须不同。我还有一个问题,如果您有时间,我已经在这里详细描述了这个问题:
<?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;
}
}