いくつかのオブジェクトに基づいて動的なUIを作成しています。レンダリングするアイテムが多数あるため、読み込みに時間がかかります。 XML下記参照: -wpfコンテナグリッド内のコントロールの読み込みが遅い
<ScrollViewer Grid.Row="2" ZoomMode="Enabled" x:Name="scrollviewer"
VirtualizingStackPanel.VirtualizationMode="Recycling"
HorizontalScrollMode="Auto"
VerticalScrollMode="Auto"
VerticalSnapPointsType="None"
HorizontalSnapPointsType="None"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
MinZoomFactor="1" IsDoubleTapEnabled="True"
RenderTransformOrigin="0,0">
<Grid Background="#FFDDE6EB" x:Name="mygrid" VerticalAlignment="Center" HorizontalAlignment="Center">
</Grid>
</ScrollViewer>
ScrollViewerのは、* 10などの高さと列の内部にあります。動的にアイテムをロードする
コードは以下である: -
public static Grid LoadGridWithItems(Model m)
{
var grid = new Grid();
grid.UseLayoutRounding = true;
grid.RowDefinitions.Clear();
grid.ColumnDefinitions.Clear();
int count = m.Rows;
for (int i = 0; i < count; i++)
{
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
}
count = m.Columns;
for (int i = 0; i < count; i++)
{
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(m.GetColumnWidth(i), GridUnitType.Star) });
}
foreach (Control control in m.Controls)
{
UIElement uicontrol=null;
if (control.Type != null)
{
switch (control.Type.ToLower())
{
case "textbox":
uicontrol=new TextBox();
break;
case "label":
uicontrol = new Label();;
break;
//bunch of other custom controls
}
}
grid.Children.Add(uicontrol);
}
return grid;
}
この生成されたグリッドは、次いで、上記XAMLで定義された「mygrid」の子として追加されます。時にはアイテムが5000以上で、ロードに約20〜25秒かかります。 このシナリオをより良い方法で実装する方法に関する提案はありますか?
ありがとうございます!
あなたはMVVMを使用していない理由は、任意の理由は?あなたの要求が許せば 'ListView'を' Virtualization'で使うことができます。つまり、表示されたときにのみアイテムを作成するということです。それは、5000の代わりに100個の項目をレンダリングするので、大幅なパフォーマンスの向上になります。 – XAMlMAX
mvvmを使用できないのは、各行に異なるコントロールの組み合わせがあり、したがって使用できる単一のデータテンプレートがないという唯一の理由です。グリッドに追加されたコントロールは、 'コントロール'クラスのプロパティにバインドされますが、各行に対して異なるテンプレートを生成する方法がわかりません。 – AjS
'TemplateSelector'を見て、どのテンプレートを適用するかを選択することができます。また、コンテナを' ListView'に変更すると、 'Virtualization'の前に使用することができます。 – XAMlMAX