【问题标题】:Xamarin Forms Bind ImageSource to element inside elementXamarin Forms 将 ImageSource 绑定到元素内的元素
【发布时间】:2020-05-20 20:40:04
【问题描述】:

我有问题。我有以下列表:

Set AlbumsForList = new Set
{
    Name = album.Name,
    Pictures = new SetPictures
    {
        Picture = new List<SetPicture>()
    },
    Price = album.Price
};

albumList.Add(AlbumsForList);

SetPicture 有一个名为imageSource 的元素,我想将其绑定到图像。 这是 XAML:

<CollectionView ItemsSource="{Binding albumList}" SelectionMode="Single"
    SelectionChanged="OnCollectionViewSelectionChanged">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid HeightRequest="100" VerticalOptions="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="10"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="3"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="3"/>
                </Grid.ColumnDefinitions>

                <ff:CachedImage  Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Aspect="AspectFill" Source="{Binding DemoImage}" />

                <Label Grid.Row="0" Grid.Column="3" VerticalTextAlignment="Center" VerticalOptions="Center"  
                        HorizontalOptions="StartAndExpand" Text="{Binding Name}" TextColor="Black" FontAttributes="Bold" 
                        FontSize="18" />
                <ScrollView Orientation="Horizontal" Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2">
                    <StackLayout BindableLayout.ItemsSource="{Binding Pictures}" Orientation="Horizontal">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <ff:CachedImage  HeightRequest="40" WidthRequest="55" Aspect="AspectFill" Source="{Binding imageSource}"/>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </StackLayout>
                </ScrollView>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

最后是我的课程:

[XmlRoot(ElementName = "picture")]
public class SinglePicture
{
    [XmlElement(ElementName = "id")]
    public string Id { get; set; }
    [XmlElement(ElementName = "name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "sizes")]
    public Sizes Sizes { get; set; }
    [XmlIgnore]
    public ImageSource imageSource { get; set; }
}

[XmlRoot(ElementName = "pictures")]
public class SinglePictures
{
    [XmlElement(ElementName = "picture")]
    public List<SinglePicture> Picture { get; set; }
}

[XmlRoot(ElementName = "picture")]
public class SetPicture
{
    [XmlElement(ElementName = "id")]
    public string Id { get; set; }
    [XmlElement(ElementName = "name")]
    public string Name { get; set; }
    [XmlElement(ElementName = "size")]
    public string Size { get; set; }
    [XmlElement(ElementName = "price")]
    public string Price { get; set; }
    [XmlElement(ElementName = "quantity")]
    public string Quantity { get; set; }
    [XmlElement(ElementName = "sizes")]
    public Sizes Sizes { get; set; }
    [XmlIgnore]
    public ImageSource imageSource { get; set; }
}

[XmlRoot(ElementName = "pictures")]
public class SetPictures
{
    [XmlElement(ElementName = "picture")]
    public List<SetPicture> Picture { get; set; }
}

[XmlRoot(ElementName = "set")]
public class Set
{
    [XmlElement(ElementName = "name")]
    public string Name { get; set; }
    [XmlElement(ElementName = "price")]
    public string Price { get; set; }
    [XmlElement(ElementName = "pictures")]
    public SetPictures Pictures { get; set; }
    [XmlIgnore]
    public ImageSource DemoImage { get; set; }
}

[XmlRoot(ElementName = "sets")]
public class Sets
{
    [XmlElement(ElementName = "set")]
    public List<Set> Set { get; set; }
}

[XmlRoot(ElementName = "size")]
public class Size
{
    [XmlElement(ElementName = "id")]
    public string Id { get; set; }
    [XmlElement(ElementName = "name")]
    public string Name { get; set; }
    [XmlElement(ElementName = "price")]
    public string Price { get; set; }
}

[XmlRoot(ElementName = "sizes")]
public class Sizes
{
    [XmlElement(ElementName = "size")]
    public List<Size> Size { get; set; }
}

[XmlRoot(ElementName = "data")]
public class Data
{
    [XmlElement(ElementName = "sets")]
    public Sets Sets { get; set; }
    [XmlElement(ElementName = "pictures")]
    public SinglePictures Pictures { get; set; }
}

现在我们已经在 listAlbums 中了,所以我定义了 Binding Pictures,但是在 CachedImage 中我需要进入 Picture 以获取元素 imageSource,但我该怎么做呢?

【问题讨论】:

  • Stacklayout 的父视图是什么?您可以在 xmal 和 ViewModel 中提供完整的代码。
  • 我将完整的 XAML 添加到问题中,但我认为 ViewModel 与此问题无关。我需要知道如何从对象中的对象访问元素。在我的情况下:albumList -&gt; Pictures -&gt; Picture -&gt; imageSource.... 我已经知道如何访问 Picture,但是接下来呢?
  • 你可以查看我的答案。

标签: xaml xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

您的页面似乎包含 CollectionView 中的可绑定布局。

其实你可以用list in list作为CollectionView的ItemsSource。

在代码后面

public class VM_CounterList
{
  

    public ObservableCollection<AlbumsForList> albumList { get; set; }


    public VM_CounterList()
    {
        albumList = new ObservableCollection<AlbumsForList>() {


             new AlbumsForList(){ 
             
                 Name="No.1",
                 DemoImage = "screen.png",
                 Pictures = new ObservableCollection<MyImage>{ new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" } },
             },

             new AlbumsForList(){

                 Name="No.1",
                 DemoImage = "screen.png",
                 Pictures = new ObservableCollection<MyImage>{ new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" } },
             },

             new AlbumsForList(){

                 Name="No.1",
                 DemoImage = "screen.png",
                 Pictures = new ObservableCollection<MyImage>{ new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" } },
             },

             new AlbumsForList(){

                 Name="No.1",
                 DemoImage = "screen.png",
                 Pictures = new ObservableCollection<MyImage>{ new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" },new MyImage() {ImageSource="screen.png" } },
             },

        };


        
    }

}

public class AlbumsForList : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public string Name { get; set; }

    public ObservableCollection<MyImage> Pictures { get; set; }

    private string demoImage;
    public string DemoImage
    {
        set
        {
            if (demoImage != value)
            {
                demoImage = value;
                NotifyPropertyChanged("DemoImage");
            }
        }
        get { return demoImage; }
    }
}
  

public class MyImage : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    

    private string imageSource;
    public string ImageSource
    {
        set
        {
            if (imageSource != value)
            {
                imageSource = value;
                NotifyPropertyChanged("ImageSource");
            }
        }
        get { return imageSource; }
    }
}
<DataTemplate>
    <Image  HeightRequest="40" WidthRequest="55" Aspect="AspectFill" Source="{Binding ImageSource}"/>
</DataTemplate>

因此您不需要更改 xaml 中的代码,它可以像以下屏幕截图一样正常工作。

我使用本地图像只是为了演示。

【讨论】:

  • 好的,但我认为这对我不起作用。我也分享了我的课程。你能看看那个然后把你的代码修改成我想要的吗?
  • 您可以将属性public List&lt;SinglePicture&gt; Picture { get; set; } 直接放在Set 中。定义这么多层次的模型是不明智的。
  • 好的,但是如何使用该类中的 XML 标签设置它。因为那么我需要在 List 中设置 XML 的数组名称:pictures 和元素名称:picture。我怎样才能做到这一点?因为现在我的图片列表是空的!
  • 你可以检查我的答案,只需要更改类名
  • 是的,我明白这一点,但是你所说的关于更改我的类集中的属性,XML 的解析没有成功,因为List&lt;SinglePicture&gt; Picture 保持为空。这是因为我的 XML 有一个标签 &lt;pictures&gt;,然后是 &lt;picture&gt;,我需要这两个标签
猜你喜欢
  • 1970-01-01
  • 2020-10-15
  • 2019-03-30
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多