我向页面添加模板100次。有什么办法可以加快速度吗?

时间:2018-11-21 05:48:58

标签: xamarin xamarin.forms

这是我拥有的代码:

        details.Children.Add(new LineTemplate());
        foreach (var x in App.cardSetWithWordCount.Select((r, i) => new { Row = r, Index = i }))
        {
            if (x.Index != 0) details.Children.Add(new SeparatorTemplate());
            if (selected == true) x.Row.IsToggled = true;
            if (selected == false) x.Row.IsToggled = false;

            var cell = new SwitchGridTemplate
            {
                IsToggled = x.Row.IsToggled,
                BindingContext = x.Row,
                Text = x.Row.Name,
                Label = x.Row.TotalWordCount.ToString(),
                ToggledCommand = (Command)vm.SelectCardSetCmd,
                RowId = x.Row.Id
            };
            details.Children.Add(cell);
        }

这需要8到9秒的时间才能运行,我想对一些建议,如果我可以通过其他方式添加它,这可能会更快。

请注意,details是我页面上的堆栈布局。

作为参考,这里是SwitchGridTemplate:

<?xml version="1.0" encoding="UTF-8"?>
<jt:BaseGridTemplate xmlns="http://xamarin.com/schemas/2014/forms" 
                     xmlns:jt="clr-namespace:Japanese.Templates" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                     xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
                     xmlns:b="clr-namespace:Behaviors;assembly=Behaviors" 
                     xmlns
 :converters="clr-namespace:Japanese.Converters;assembly=Japanese" 
                 x:Class="Japanese.Templates.SwitchGridTemplate" 
                 x:Name="this"
                 BackgroundColor="{DynamicResource GridBackgroundColor}" >
<StackLayout Padding="20,0" HeightRequest="49" Margin="0" Orientation="Vertical" Spacing="0">
    <Grid VerticalOptions="CenterAndExpand">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Text="{Binding Text,  Source={x:Reference this}}" VerticalTextAlignment="Center" 
               Style="{StaticResource LabelText}"
               TextColor="{DynamicResource LabelTextColor}" />
        <Label Grid.Column="1" Text="{Binding Label,  Source={x:Reference this}}" VerticalTextAlignment="Center" 
               Style="{StaticResource LabelText}" 
               TextColor="{DynamicResource LabelColor}" />
        <local:ExtSwitch Grid.Column="2" SwitchThumbColor="White" SwitchOnColor="Red" SwitchOffColor="Gray" 
                         HorizontalOptions="End" VerticalOptions="Center" IsToggled="{Binding IsToggled, Source={x:Reference this}}" >
            <local:ExtSwitch.Behaviors>
                <b:EventHandlerBehavior EventName="Toggled">
                    <b:InvokeCommandAction 
                    Command="{Binding ToggledCommand, Source={x:Reference this}}" 
                    CommandParameter="{Binding ., Source={x:Reference this}}" />
                </b:EventHandlerBehavior>
            </local:ExtSwitch.Behaviors>
        </local:ExtSwitch>
    </Grid>
</StackLayout>

0 个答案:

没有答案