【问题标题】:DynamicResource styling of listview and grid row heights in XamarinXamarin 中列表视图和网格行高的 DynamicResource 样式
【发布时间】:2018-12-20 21:06:35
【问题描述】:

我正在使用动态资源来设置我的 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】:

    在您的列表视图中,XAML HasUnevenRows 为假。如果您想要动态行高,请尝试将其设置为 True

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多