Xamarin.forms 如何设置ListView中图像的大小?

Xamarin.forms 如何设置ListView中图像的大小?,xamarin.forms,Xamarin.forms,如何设置ListView中图像的大小 目标设备是Windows Phone 10(即Windows通用平台) 我发现了以下几点: 请注意,针对Windows Phone 8.1时,ImageCell将不会缩放 默认情况下为图像。另外,请注意,Windows Phone 8.1是唯一的 以相同颜色和字体显示详细文本的平台 默认情况下作为主文本。Windows Phone 8.0将ImageCell渲染为 见下文: 我试过了: <ListView Grid.Row="1" ItemsSourc

如何设置ListView中图像的大小

目标设备是Windows Phone 10(即Windows通用平台)

我发现了以下几点:

请注意,针对Windows Phone 8.1时,ImageCell将不会缩放 默认情况下为图像。另外,请注意,Windows Phone 8.1是唯一的 以相同颜色和字体显示详细文本的平台 默认情况下作为主文本。Windows Phone 8.0将ImageCell渲染为 见下文:

我试过了:

<ListView Grid.Row="1" ItemsSource="{Binding Photos}" SelectedItem="{Binding SelectedPhoto, Mode=TwoWay}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ImageCell ImageSource="{Binding ImageSource}" Text="{Binding Description}" TextColor="Silver" />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
<ListView Grid.Row="1" ItemsSource="{Binding Photos}" SelectedItem="{Binding SelectedPhoto, Mode=TwoWay}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <Image Source="{Binding ImageSource}" Aspect="AspectFit" />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

上图显示了一个完整的图像,没有限制图像的大小以适合作为listview项目

我也尝试过:

<ListView Grid.Row="1" ItemsSource="{Binding Photos}" SelectedItem="{Binding SelectedPhoto, Mode=TwoWay}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ImageCell ImageSource="{Binding ImageSource}" Text="{Binding Description}" TextColor="Silver" />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
<ListView Grid.Row="1" ItemsSource="{Binding Photos}" SelectedItem="{Binding SelectedPhoto, Mode=TwoWay}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <Image Source="{Binding ImageSource}" Aspect="AspectFit" />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

上面的代码没有显示任何图像。它只显示一个白色背景。

几件事:-

ImageCell
无法指定图像宽度/高度(v2.0.x)

您的第二个示例更接近正轨,但是,在处理
列表视图时,您需要将其包装在
ViewCell
中,如下所示:-

<ListView ItemsSource="{Binding Photos}" HasUnevenRows="True">
  <ListView.ItemTemplate>
    <DataTemplate>            
      <ViewCell>
        <Image Source="{Binding MyImage}" Aspect="AspectFit" WidthRequest="{Binding MyWidth}" HeightRequest="{Binding MyHeight}" />
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView> 

另外,请注意,
ListView
的默认值设置为行高equal

因此,如果您有不同的图像大小,则很可能无法生成所需的输出

若要解决此问题,请指定
hasRows='True'

如果您进一步执行
BindableProperties
以获得所需的
ImageWidth
ImageHeight
视图模型中的
,如上例所示,使用
WidthRequest
HeightRequest
Image
指定不同的值时,输出结果如下:-


只需在熟食中添加盐,您还可以动态添加:

 <Slider x:Name="slider" Maximum="600"  Minimum="30"  />

    <ListView RowHeight="55" x:Name="lv_prayers_categories_page" 
              ItemsSource="{Binding SimpleList}"  
              HasUnevenRows="true" 
              BackgroundColor="Transparent" 
              SeparatorVisibility="None">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
        <StackLayout BackgroundColor="#eee" HeightRequest="50"   >
                            <StackLayout Orientation="Horizontal">
                                <Image Aspect="AspectFill" Source="{Binding Image}" HeightRequest="{Binding Source={x:Reference slider}, Path=Value}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
                                <Label Text="{Binding Name}"
                                TextColor="#f35e20" />
                                <Label Text="{Binding ID}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#503026" />
                            </StackLayout>
                        </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

您是否尝试过设置宽度请求和水平选项?谢谢Pete。当我使用StackLayouts时,这是有效的。但当我用网格替换stacklayouts时,这不起作用。这是一个已知的bug吗?@ScottNimrod最好创建一个新的问题,并提供一个代码示例,因为它不清楚您是如何构建内容的,这将有助于提供帮助。