Xamarin中Listview的动态资源样式和网格行高度

问题描述 投票:0回答:1

我正在使用动态资源来设置UI元素的字体大小,以便在小型平板电脑上,文本稍大一些以便于阅读,但在非常小的屏幕设备上也需要支持。效果很好。

没有什么用的是缩放包含该文本的行。例如(为简洁起见编辑):

App.xaml中的资源定义:

    <ResourceDictionary>
        <x:Int32 x:Key="Std32RowHeight">50</x:Int32>
        <x:Double x:Key="StdRowHeight">40</x:Double>

        <x:Double x:Key="StdFontSize">20 </x:Double>
        <Style x:Key="ListText" TargetType="Label"  ApplyToDerivedTypes="True">
            <Setter Property="TextColor" Value="Black" />
            <Setter Property="FontSize" Value="{DynamicResource StdFontSize}" />
        </Style>
        ...

Xaml代码:

        <ListView x:Name="locationList"
            Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5"
            ItemsSource="{Binding DisplayGroups}"
            HasUnevenRows="true"
            CachingStrategy="RecycleElement" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Height="{DynamicResource StdRowHeight}">  <!-- Causes a runtime error -->
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Title}" 
                                   TextColor="Black"
                                   FontSize="{DynamicResource StdFontSize}"  <!-- Works -->
                                   VerticalTextAlignment="Center" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

...导致此运行时错误:

“无法分配属性“高度”:属性不存在或不可分配,或者值和属性之间的类型不匹配”

我也尝试过这种方法:

        <ListView x:Name="locationList"
            Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5"
            ItemsSource="{Binding DisplayGroups}"
            HasUnevenRows="false"
            RowHeight="<DynamicResource Std32RowHeight}"  <!-- This row height is an int32 instead of double... -->
            CachingStrategy="RecycleElement" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Title}" TextColor="Black" FontSize="{DynamicResource StdFontSize}"
                                   VerticalTextAlignment="Center"
                                   Margin="20,0,0,0"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

...无效。

我已经阅读了很多有关样式应用程序的文章/帖子/示例,但是它们仅更改字体大小或颜色。 (极好的示例:Sizing objects based on screen dimensions

如果您更改字体大小,如何更改行大小以使其匹配?为了获得额外的信用,您将如何为网格行做这件事?我涉猎GridLength资源,但也无法使它正常工作。

感谢您的帮助!

listview xamarin.forms grid-layout dynamicresource
1个回答
0
投票

在您的Listview中,XAML HasUnevenRows为false。如果要动态行高,请尝试将其设置为True。

© www.soinside.com 2019 - 2024. All rights reserved.