0

我有一个 Window 8 RT 商店应用程序 (XAML/C#)。

我有一个以网格为主要组件的表单。该网格有 50 行,每行都有一个 TextBox。网格被包裹在一个滚动查看器中:

<ScrollViewer>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            ...50 rows
         </Grid.RowDefinitions>

         <TextBox Grid.Row="0" />
         <TextBox Grid.Row="1" />
         <TextBox Grid.Row="2" />
         ...
         <TextBox Grid.Row="50" />
    </Grid>
 </ScrollViewer>

加载此表单时,加载页面时会出现明显的暂停,我猜这是因为正在绘制页面。

加快此加载过程的最佳方法是什么?我可以虚拟化网格/文本框的加载吗?

一旦应用程序在 Windows Surface 平板电脑上运行,速度就会很明显,这在我的设计 PC 上还不错,但显然更强大。

提前致谢。

4

1 回答 1

1

您可以使用 aListView而不是Gridin a ScrollViewer,因为它默认支持虚拟化。除此之外 - 拥有更好的用户体验可能比要填写的可怕的长文本框列表更好 - 可能将您的表单分解为多个页面或使用 aFlipView在字段组之间翻转。

*编辑 - 示例

XAML

<Page
    x:Class="App10.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App10"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <DataTemplate
            x:Key="TextFieldTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition
                        Height="Auto" />
                    <RowDefinition
                        Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock
                    Text="{Binding Label}" />
                <TextBox
                    Text="{Binding Value, Mode=TwoWay}"
                    Grid.Row="1" />
            </Grid>
        </DataTemplate>
        <DataTemplate
            x:Key="BoolFieldTemplate">
            <CheckBox
                Content="{Binding Label}"
                IsChecked="{Binding Value, Mode=TwoWay}" />
        </DataTemplate>
        <local:FieldTemplateSelector
            x:Key="FieldTemplateSelector"
            TextTemplate="{StaticResource TextFieldTemplate}"
            BoolTemplate="{StaticResource BoolFieldTemplate}" />
    </Page.Resources>
    <Grid
        Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ListView
            x:Name="lv"
            ItemTemplateSelector="{StaticResource FieldTemplateSelector}" />
    </Grid>
</Page>

C#

using System.Collections.Generic;
using App10.Common;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App10
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.lv.ItemsSource =
                new List<object>(
                    new object[]
                        {
                            new BoolFieldViewModel { Label = "Some bool field" },
                            new TextFieldViewModel { Label = "Some text field" },
                            new TextFieldViewModel { Label = "Some text field" },
                            new BoolFieldViewModel { Label = "Some bool field" },
                            new BoolFieldViewModel { Label = "Some bool field" },
                            new TextFieldViewModel { Label = "Some text field" },
                        });
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }

    public abstract class FieldViewModel<T> : BindableBase
    {
        public string Label { get; set; }

        #region Value
        private T _value;
        public T Value
        {
            get { return _value; }
            set { this.SetProperty(ref _value, value); }
        }
        #endregion
    }

    public class BoolFieldViewModel : FieldViewModel<bool> { }
    public class TextFieldViewModel : FieldViewModel<string> { }

    public class FieldTemplateSelector : DataTemplateSelector
    {
        public DataTemplate BoolTemplate { get; set; }
        public DataTemplate TextTemplate { get; set; }

        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (item is BoolFieldViewModel) return BoolTemplate;
            if (item is TextFieldViewModel) return TextTemplate;

            return base.SelectTemplateCore(item, container);
        }
    }
}
于 2013-04-16T18:38:58.713 回答