在样式中设置Grid RowDefinitions/ColumnDefinitions属性

Had*_*ari 8 xaml xamarin.forms xamarin.forms-styles

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

是否可以创建样式并设置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>
Run Code Online (Sandbox Code Playgroud)

异常中的位置14:9指的是第一个<RowDefinition />标签.

j.f*_*.f. 11

你不能定义RowDefinitionColumnDefinition风格中,但您可以创建它们作为可重复使用的资源.我想这就是你要找的东西.定义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>
Run Code Online (Sandbox Code Playgroud)
<Grid 
    ColumnDefinitions="{StaticResource MyColumnDefinitionCollection}"
    RowDefinitions="{StaticResource MyRowDefinitionCollection}">
</Grid>
Run Code Online (Sandbox Code Playgroud)

  • 似乎对我不起作用,我收到错误消息:`类型“ColumnDefinitionCollection”不包括任何可访问的构造函数`和`属性“ColumnDefinitions”没有可访问的设置器。` (2认同)

Had*_*ari 5

感谢@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>
Run Code Online (Sandbox Code Playgroud)


Had*_*ari 5

我找到了一个更好,更简单的解决方案。答案是在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>
Run Code Online (Sandbox Code Playgroud)