13

假设您有 2 个或更多共享相同结构的网格(行/列计数和属性相等)。

是否可以创建Style并设置RowDefinitions / ColumnDefinitions

我已经试过了。但是当应用程序尝试解析 xaml 时出现此异常:

Xamarin.Forms.Xaml.XamlParseException:位置 14:9。属性值为 null 或不是 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 />标签。

4

5 回答 5

18

您无法在样式中定义RowDefinitionColumnDefinition,但可以将它们创建为可重用资源。我想这就是你要找的。定义 aColumnDefinitionCollection和 aRowDefinitionCollection这样可以让您在许多网格中重用它们以创建一致的外观。

<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>
于 2017-08-14T18:54:28.413 回答
10

我找到了一个更好更简单的解决方案。答案只是在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>
于 2017-08-22T14:56:11.590 回答
7

感谢@jf,我找到了解决方案。我将他的想法与我的想法结合起来,结果如下:

诀窍是将声明添加到单独的资源中并在样式中引用它。

<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>
于 2017-08-14T19:19:17.303 回答
3

您可以创建一个从 Grid 派生的类

public class MyGrid : Grid

设置它,然后在 xamls 中的任何地方使用

<local:MyGrid> 

正如jf所提到的,它被称为用户控制

于 2017-08-14T19:02:10.387 回答
0

诺你不能,在我看来你不应该。

定义网格不是关于样式,而是关于布局。

从 Xamarin.Forms 版本 3 开始,将引入更多类似 CSS 的样式功能。也许您将能够像在 HTML/CSS 中那样设置网格的样式。但对于将实施什么以及如何实施,我们知之甚少。

于 2017-08-14T18:31:03.157 回答