Windows runtime 虚拟化网格中Windows 8 XAML控件的加载
我有一个windows8rt存储应用程序(XAML/C#) 我有一个以网格为主要组件的表单。该网格有50行,每行有一个文本框。网格被包装在scrollviewer中:Windows runtime 虚拟化网格中Windows 8 XAML控件的加载,windows-runtime,winrt-xaml,Windows Runtime,Winrt Xaml,我有一个windows8rt存储应用程序(XAML/C#) 我有一个以网格为主要组件的表单。该网格有50行,每行有一个文本框。网格被包装在scrollviewer中: <ScrollViewer> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowD
<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>
…50排
...
加载此表单时,页面加载时会出现明显的停顿,我猜这是因为页面正在绘制
加快加载过程的最佳方法是什么?我可以虚拟化网格/文本框的加载吗
当应用程序在WindowsSurface平板电脑上运行时,速度会很慢,这在我的设计PC上还不错,但显然要强大得多
提前感谢。您可以在
滚动查看器中使用列表视图
而不是网格
,因为它默认支持虚拟化。除此之外,如果有更好的用户体验,而不是要填写一长串可怕的文本框,那就更好了。可以将表单分成多个页面,或者使用FlipView
在字段组之间切换
*编辑示例
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#
使用System.Collections.Generic;
使用App10.Common;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Navigation;
名称空间App10
{
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
此.lv.ItemsSource=
新名单(
新对象[]
{
新的BoolFieldViewModel{Label=“Some bool field”},
新建TextFieldViewModel{Label=“Some text field”},
新建TextFieldViewModel{Label=“Some text field”},
新的BoolFieldViewModel{Label=“Some bool field”},
新的BoolFieldViewModel{Label=“Some bool field”},
新建TextFieldViewModel{Label=“Some text field”},
});
}
///
///当此页面即将显示在框架中时调用。
///
///描述如何访问此页的事件数据。参数
///属性通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
}
}
公共抽象类FieldViewModel:BindableBase
{
公共字符串标签{get;set;}
#区域值
私人T_值;
公共价值
{
获取{返回_值;}
set{this.SetProperty(ref _value,value);}
}
#端区
}
公共类BoolFieldViewModel:FieldViewModel{}
公共类TextFieldViewModel:FieldViewModel{}
公共类FieldTemplateSelector:DataTemplateSelector
{
公共数据模板BoolTemplate{get;set;}
公共数据模板TextTemplate{get;set;}
受保护的覆盖数据模板SelectTemplateCore(对象项,DependencyObject容器)
{
如果(项为BoolFieldViewModel)返回BoolTemplate;
如果(项目为TextFieldViewModel)返回TextTemplate;
返回基地。选择TemplateCore(项目、容器);
}
}
}
如果使用ListView或Flipview,则必须为每个可见项使用模板。因此,如果表单中有文本框以及组合框和复选框,那么我将无法实现此功能。您可以使用ItemTemplateSelector。您可以提供一个示例吗?谢谢!!再次感谢你的帮助,菲利普。
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);
}
}
}