将附加属性复制到xamarin表单中的新实例

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

我正在创建网格对象的新实例,其中有两个标签如下。

<Grid x:Name="Grid">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="70*"></ColumnDefinition>
                                    <ColumnDefinition Width="30*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition></RowDefinition>
                                </Grid.RowDefinitions>
                                <Label x:Name="Label1" Text="Label1" Grid.Column="0" Grid.Row="0"></Label>
                                <Label x:Name="Label2" Text="Label2" Grid.Column="1" Grid.Row="0"></Label>
                            </Grid>

我正在使用Activator.CreateInstance方法,如下所示:

Grid ClonedView =(Grid) Activator.CreateInstance(Grid.GetType());


            foreach (PropertyInfo propertyInfo in Grid.GetType().GetProperties())
            {
                if (propertyInfo.CanRead && propertyInfo.CanWrite  )
                {

                    object PropertyValue = propertyInfo.GetValue(Grid, null);

                    propertyInfo.SetValue(ClonedView, PropertyValue);

                }
            }

当我在我的页面中使用此ClonedView时,它克隆网格不明白Label2应该出现在第二列网格中。我做了很多关于它的研究,但找不到任何解决方案。如果有人有任何解决方案,请告诉我。

谢谢

xamarin.forms system.reflection
1个回答
1
投票

他们在考虑这个问题,感觉非常喜欢我。您想通过复制所有属性来创建深层副本。然而,Xamarin对象对于这样的东西来说太复杂了(即使你能够进行深度复制,例如Bindings也不能用于简单的复制)。

因此,不是制作单个Grid对象并尝试创建它的副本,而是另一种查看它的方式是“我想定义一个模板,然后根据该模板创建多个实例”。为此,Xamarin有DataTemplate类,这使我们能够在xaml中做这样的事情:

<ContentPage>
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="MyGridTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70*"></ColumnDefinition>
                        <ColumnDefinition Width="30*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Label Text="Label1" Grid.Column="0" Grid.Row="0"></Label>
                    <Label Text="Label2" Grid.Column="1" Grid.Row="0"></Label>
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>
    <Button Text="Add Grid" Clicked="AddGridClicked">
    </Button>
    <StackLayout x:Name="GridHolder">
    <StackLayout>
<ContentPage>

在我们的代码背后,我们可以像这样实现AddGridClicked

private void AddGridClicked(object sender, EventArgs e)
{
    var myGridTemplate = (DataTemplate) Resources["MyGridTemplate"];
    GridHolder.Children.Add((View)myGridTemplate.CreateContent());
}

现在的缺点是你需要定义一个DataTemplate,而不是仅仅指向一个对象并说一个副本。另请注意,x:Name中的ViewDataTemplate无法在您页面后面的代码中访问,但是具有不需要的良好Xamarin设置。好处是,这可以完成可能的绑定,所以你可以在你的网格中定义一个Label,如下所示:

<Label Text="{Binding Name}" Grid.Column="0" Grid.Row="0"></Label>

然后你可以给每个实例提供你的模板qazxsw poi一个不同的qazxsw poi来将不同的数据绑定到它的视图。

希望这可以帮助

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