重用动态创建的视图

时间:2014-06-16 10:01:43

标签: c# wpf windows-phone-8 windows-phone

我在For循环中动态创建300个视图:

例如:

for (int j = 0; j <= 300; j++)
{
    Image image = new Image();
    image.Source = new BitmapImage(new Uri("/Images/sample256.png", UriKind.RelativeOrAbsolute));

    Grid titleGrid = new Grid();
    titleGrid.HorizontalAlignment = HorizontalAlignment.Center;
    titleGrid.VerticalAlignment = VerticalAlignment.Center;

    TextBlock titleText = new TextBlock();
    titleText.TextWrapping = TextWrapping.Wrap;
    titleGrid.Children.Add(titleText);

    Grid subtitleGrid = new Grid();
    subtitleGrid.HorizontalAlignment = HorizontalAlignment.Center;
    subtitleGrid.VerticalAlignment = VerticalAlignment.Center;

    TextBlock subtitleText = new TextBlock();
    subtitleText.TextWrapping = TextWrapping.Wrap;

    subtitleGrid.Children.Add(subtitleText);

    //add all views to root layout
    LayoutRoot.Children.Add(image);
    LayoutRoot.Children.Add(titleGrid);
    LayoutRoot.Children.Add(subtitleGrid);
}

现在应用程序存在延迟,因为我每次都要添加新视图,如何重用已创建的视图?我正在使用Windows Phone 8应用程序。

2 个答案:

答案 0 :(得分:1)

在布局根目录中添加300个项目肯定会使页面加载速度变慢。你需要使用像listbox这样实现虚拟化的控件。这是如何

页面中的ListBox XAML。

<ListBox Name="myListBox" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Image Source="{Binding ImageUrl}">
                        </Image>
                        <TextBlock Text="{Binding Question}"></TextBlock>
                        <TextBlock Text="{Binding Answer}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

您的代码

code to bind data 

  List<MyData> list = new List<MyData>();
        for (int i = 0; i < 300; i++)
        {
            var data = new MyData();
            data.Question = "//yourquestion";
            data.Answer = "// your answer";
            data.ImageSource = new BitmapImage(new Uri("yourimagepat"));
        }
        myListBox.ItemsSource = list;

数据类

  public class MyData {
    public string Question { get; set; }

    public string Answer { get; set; }

    public BitmapImage ImageSource { get; set; }
}

利用虚拟化,请在网格控件中添加列表框。否则它会抛出内存异常并且也会很慢

答案 1 :(得分:0)

您可以尝试使用DataBinding功能,而不是创建它。

 // You can bound items from your Class here

<ListBox x:Name="ListBox1" Margin="5"
 Width="450" Height="200" HorizontalAlignment="Left"
 ItemsSource="{Binding SongsList}">
ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel CleanUpVirtualizedItemEvent="VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1">
                </VirtualizingStackPanel>
            </ItemsPanelTemplate>

 <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal" Margin="2">
        <TextBlock Text="Artist:" Margin="2" />
        <TextBlock Text="{Binding Artist}" Margin="2" />
        <TextBlock Text="CD:" Margin="10,2,0,2" />
        <TextBlock Text="{Binding Name}" Margin="2" />
     </StackPanel>
  </DataTemplate>
</ListBox.ItemTemplate>

// your class should be like this
public Songs
{
 public string Artist{get;set;}
 public string Name {get;set;}
}

// CodeBehind只需添加数据

ObservableCollection<Songs> SongsList=new ObservableCollection<SongsL();

for (int j = 0; j <= 300; j++)
{
   SongsList.Add(new Songs{Artist="Aritst Name",Name="Song Name"});
}

// Set this Collection from the codebehind or xaml .
ListBox1.ItemSource=SongsList; // it will the bind the public properties in this Songs.

DataBinding Controls on Windows phone

Msdn Databindin greference

Binding data Grid

Databinding from Jesse Liberty