【问题标题】:BindableProperty for Image Source in contentview内容视图中图像源的 BindableProperty
【发布时间】:2019-03-01 10:00:16
【问题描述】:

我正在尝试在我的 Contentview 中绑定 DogImage 源。我正在尝试构建可重用的视图对象,例如框架按钮。我只需要从 contentview 之外给他们图像和文本。我正在使用资源文件夹中的图像。

这是我的内容视图

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PawsApp.Views.AboutMyDogViews.DogsBreedPicker"
             BindingContext="{Binding .}">
    <ContentView.Content>
        <StackLayout x:Name="breedStack"  Margin="30,2,30,2">
            <Frame  HeightRequest="64" CornerRadius="8" BorderColor="White" HasShadow="False" BackgroundColor="White" 
               VerticalOptions="FillAndExpand" Padding="18,0,18,0">
                <Frame.Content>
                    <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand">
                        <Grid ColumnSpacing="18">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"></ColumnDefinition>
                                <ColumnDefinition Width="9*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Image x:Name="DogImage" Source="{Binding ImageSourceOf}"  Grid.Row="0" Grid.Column="0" ></Image>
                            <Label  x:Name="breedSelector" Text="Breed" FontSize="Medium" TextColor="#5f5d70" Grid.Row="0" Grid.Column="1">
                            </Label>
                        </Grid>
                    </StackLayout>
                </Frame.Content>
            </Frame>
        </StackLayout>
    </ContentView.Content>
</ContentView>

这是CS文件

public partial class DogsBreedPicker : ContentView
{
    public static readonly BindableProperty ImageSourceProperty =
        BindableProperty.Create("ImageSourceOf", typeof(ImageSource), typeof(DogsBreedPicker));

    public ImageSource ImageSourceOf
    {
        get { return GetValue(ImageSourceProperty) as ImageSource; }
        set { SetValue(ImageSourceProperty, value);}           
    }

    public DogsBreedPicker()
    {
        InitializeComponent ();
        BindingContext = ImageSourceOf;
    }
}

这就是我想要使用它的方式。

<views:DogsBreedPicker ImageSourceOf="dog" TextOf="Breed Selector" x:Name="DogBreed" ></views:DogsBreedPicker>

【问题讨论】:

  • 乍一看,这看起来不错(不过,为了遵循通常的命名约定,我会将 ImageSourceOf 重命名为 ImageSourceImageSourcePropertyImageSourceOfProperty),您的具体名称是什么问题?
  • @PaulKertscher BindingContext = ImageSourceOf;始终为空 :(
  • 我明白了。给我一分钟。

标签: xamarin xamarin.forms bindable


【解决方案1】:

您在构造函数中将BindingContext 设置为ImageSourceOf。无论如何,此时没有设置属性,因此ImageSourceOf 仍然是null。但是,在这种情况下,您无论如何都不需要使用BindingContext,因为您可以直接绑定到ImageSourceOf 属性:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PawsApp.Views.AboutMyDogViews.DogsBreedPicker"
             x:Name="View">
      <!-- Elided all the other stuff -->
      <Image x:Name="DogImage" Source="{Binding ImageSourceOf, Source={x:Reference View}}"  Grid.Row="0" Grid.Column="0" />

</ContentView>

从你的构造函数中移除BindingContext的赋值。

为什么会这样?

所有绑定的默认来源是BindingContext(视图的,它被传播到所有子视图)。无论如何,您绝不限于将BindingContext 作为绑定源,而是可以将绑定的Source 设置为另一个对象。在这种情况下,我们通过其名称引用视图(我们将其命名为Viewx:Name="View")并将其用作ImageSource 的绑定源。由于绑定的PathImageSourceOf,所以Image.Source会绑定到View.ImageSourceOf

【讨论】:

  • 感谢您的快速回答,这让我大开眼界并说清楚了。我只是删除 bindingcontext 并从视图参考中给出它。
【解决方案2】:
Content.BindingContext = this;

解决了我的问题。但是有没有办法让它变得更好呢?请添加评论。

【讨论】:

  • 我只是在写一个答案,等等。
猜你喜欢
  • 2018-07-11
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2013-05-25
  • 2014-09-26
相关资源
最近更新 更多