在样式中设置网格 RowDefinitions/ColumnDefinitions 属性
Setting Grid RowDefinitions/ColumnDefinitions properties in a style
假设您有 2 个或更多网格共享相同的结构(Rows/Columns 计数和属性相等)。
是否可以创建 Style 并设置 RowDefinitions/ColumnDefinitions?
我试过了。但是当应用程序尝试解析 xaml:
时,我得到了这个异常
Xamarin.Forms.Xaml.XamlParseException: Position 14:9. Property Value is null or is not IEnumerable
我的源代码:
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<Style TargetType="Grid">
<Setter Property="RowDefinitions">
<Setter.Value>
<RowDefinition />
<RowDefinition />
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Grid>
<Label Grid.Row="0" Text="(Grid #1) Row #1" />
<Label Grid.Row="1" Text="(Grid #1) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #2) Row #1" />
<Label Grid.Row="1" Text="(Grid #2) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #3) Row #1" />
<Label Grid.Row="1" Text="(Grid #3) Row #2" />
</Grid>
</StackLayout>
异常中的位置14:9指的是第一个<RowDefinition />
标签。
不可以,我认为也不应该。
定义网格与样式无关,而与布局有关。
从 Xamarin.Forms 版本 3 开始,将引入更多类似于 CSS 的样式功能。或许您随后将能够像在 HTML/CSS 中那样设置网格样式。但对于将实施什么以及如何实施知之甚少。
您不能在样式中定义 RowDefinition
和 ColumnDefinition
,但您可以将它们创建为可重复使用的资源。我想这就是你要找的。像这样定义 ColumnDefinitionCollection
和 RowDefinitionCollection
允许您将它们与许多网格一起重用以创建一致的外观。
<ResourceDictionary>
<ColumnDefinitionCollection
x:Key="MyColumnDefinitionCollection">
<ColumnDefinition
Width="50" />
<ColumnDefinition
Width="100" />
<ColumnDefinition
Width="150" />
</ColumnDefinitionCollection>
<RowDefinitionCollection
x:Key="MyRowDefinitionCollection">
<RowDefinition
Height="50" />
<RowDefinition
Height="50" />
<RowDefinition
Height="100" />
<RowDefinition
Height="100" />
</RowDefinitionCollection>
</ResourceDictionary>
<Grid
ColumnDefinitions="{StaticResource MyColumnDefinitionCollection}"
RowDefinitions="{StaticResource MyRowDefinitionCollection}">
</Grid>
您可以创建一个从 Grid
派生的 class
public class MyGrid : Grid
进行设置,然后在 xaml 中随处使用
<local:MyGrid>
如j.f所述,称为用户控件
感谢@j.f.,我找到了解决方案。我将他的想法与我的结合起来,结果如下:
诀窍是将声明添加到单独的资源中并在样式中引用它。
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<RowDefinitionCollection x:Key="MyRowDefinitionCollection">
<RowDefinition />
<RowDefinition />
</RowDefinitionCollection>
<Style TargetType="Grid">
<Setter Property="RowDefinitions" Value="{StaticResource MyRowDefinitionCollection}" />
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Grid>
<Label Grid.Row="0" Text="(Grid #1) Row #1" />
<Label Grid.Row="1" Text="(Grid #1) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #2) Row #1" />
<Label Grid.Row="1" Text="(Grid #2) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #3) Row #1" />
<Label Grid.Row="1" Text="(Grid #3) Row #2" />
</Grid>
</StackLayout>
我找到了更好更简单的解决方案。答案只是在 RowDefinition 标签周围添加 RowDefinitionCollection 标签。
像这样:
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<Style TargetType="Grid">
<Setter Property="RowDefinitions">
<Setter.Value>
<RowDefinitionCollection>
<RowDefinition />
<RowDefinition />
</RowDefinitionCollection>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Grid>
<Label Grid.Row="0" Text="(Grid #1) Row #1" />
<Label Grid.Row="1" Text="(Grid #1) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #2) Row #1" />
<Label Grid.Row="1" Text="(Grid #2) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #3) Row #1" />
<Label Grid.Row="1" Text="(Grid #3) Row #2" />
</Grid>
</StackLayout>
假设您有 2 个或更多网格共享相同的结构(Rows/Columns 计数和属性相等)。
是否可以创建 Style 并设置 RowDefinitions/ColumnDefinitions?
我试过了。但是当应用程序尝试解析 xaml:
时,我得到了这个异常Xamarin.Forms.Xaml.XamlParseException: Position 14:9. Property Value is null or is not IEnumerable
我的源代码:
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<Style TargetType="Grid">
<Setter Property="RowDefinitions">
<Setter.Value>
<RowDefinition />
<RowDefinition />
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Grid>
<Label Grid.Row="0" Text="(Grid #1) Row #1" />
<Label Grid.Row="1" Text="(Grid #1) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #2) Row #1" />
<Label Grid.Row="1" Text="(Grid #2) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #3) Row #1" />
<Label Grid.Row="1" Text="(Grid #3) Row #2" />
</Grid>
</StackLayout>
异常中的位置14:9指的是第一个<RowDefinition />
标签。
不可以,我认为也不应该。
定义网格与样式无关,而与布局有关。
从 Xamarin.Forms 版本 3 开始,将引入更多类似于 CSS 的样式功能。或许您随后将能够像在 HTML/CSS 中那样设置网格样式。但对于将实施什么以及如何实施知之甚少。
您不能在样式中定义 RowDefinition
和 ColumnDefinition
,但您可以将它们创建为可重复使用的资源。我想这就是你要找的。像这样定义 ColumnDefinitionCollection
和 RowDefinitionCollection
允许您将它们与许多网格一起重用以创建一致的外观。
<ResourceDictionary>
<ColumnDefinitionCollection
x:Key="MyColumnDefinitionCollection">
<ColumnDefinition
Width="50" />
<ColumnDefinition
Width="100" />
<ColumnDefinition
Width="150" />
</ColumnDefinitionCollection>
<RowDefinitionCollection
x:Key="MyRowDefinitionCollection">
<RowDefinition
Height="50" />
<RowDefinition
Height="50" />
<RowDefinition
Height="100" />
<RowDefinition
Height="100" />
</RowDefinitionCollection>
</ResourceDictionary>
<Grid
ColumnDefinitions="{StaticResource MyColumnDefinitionCollection}"
RowDefinitions="{StaticResource MyRowDefinitionCollection}">
</Grid>
您可以创建一个从 Grid
派生的 classpublic class MyGrid : Grid
进行设置,然后在 xaml 中随处使用
<local:MyGrid>
如j.f所述,称为用户控件
感谢@j.f.,我找到了解决方案。我将他的想法与我的结合起来,结果如下:
诀窍是将声明添加到单独的资源中并在样式中引用它。
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<RowDefinitionCollection x:Key="MyRowDefinitionCollection">
<RowDefinition />
<RowDefinition />
</RowDefinitionCollection>
<Style TargetType="Grid">
<Setter Property="RowDefinitions" Value="{StaticResource MyRowDefinitionCollection}" />
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Grid>
<Label Grid.Row="0" Text="(Grid #1) Row #1" />
<Label Grid.Row="1" Text="(Grid #1) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #2) Row #1" />
<Label Grid.Row="1" Text="(Grid #2) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #3) Row #1" />
<Label Grid.Row="1" Text="(Grid #3) Row #2" />
</Grid>
</StackLayout>
我找到了更好更简单的解决方案。答案只是在 RowDefinition 标签周围添加 RowDefinitionCollection 标签。
像这样:
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<Style TargetType="Grid">
<Setter Property="RowDefinitions">
<Setter.Value>
<RowDefinitionCollection>
<RowDefinition />
<RowDefinition />
</RowDefinitionCollection>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Grid>
<Label Grid.Row="0" Text="(Grid #1) Row #1" />
<Label Grid.Row="1" Text="(Grid #1) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #2) Row #1" />
<Label Grid.Row="1" Text="(Grid #2) Row #2" />
</Grid>
<Grid>
<Label Grid.Row="0" Text="(Grid #3) Row #1" />
<Label Grid.Row="1" Text="(Grid #3) Row #2" />
</Grid>
</StackLayout>