Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重用动态创建的视图_C#_Wpf_Windows Phone 8_Windows Phone - Fatal编程技术网

C# 重用动态创建的视图

C# 重用动态创建的视图,c#,wpf,windows-phone-8,windows-phone,C#,Wpf,Windows Phone 8,Windows Phone,我正在For循环中动态创建300个视图: 例: 对于(int j=0;j您可以尝试使用数据绑定功能,而不是创建此功能 // You can bound items from your Class here <ListBox x:Name="ListBox1" Margin="5" Width="450" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding SongsList}"> ListBox.Item

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

例:


对于(int j=0;j您可以尝试使用数据绑定功能,而不是创建此功能

 // 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>
//代码隐藏只需添加数据

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.

observateCollection SongsList=new observateCollection在布局根目录中添加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>

若要利用虚拟化,请将您的列表框添加到网格控件中。否则它可能会抛出内存不足异常,而且速度也会很慢

好吧,您可以创建一个图像/视图/网格的集合,类似于:List usedImages。然后您可以使用此列表。但为什么需要300个视图?可能有更简单的解决方案。@Olter我正在屏幕上工作,每个根布局将有3个孩子,即图像和2个文本。你能给我看一下你指的是什么吗?Em,刚才看到了:请不要问重复的问题。@Olter是的,我之前问过,但没有得到足够的注意,你能帮我解决这个问题吗这给了我内存不足的错误。任何其他解决方案谢谢我的情况下,我没有一个列表框,它的自定义类扩展面板,而不是尝试从虚拟化面板扩展它我的collegue甚至通过邮件向你发送了项目?你记得吗?啊,同样的问题是的,我记得我没有找到太多时间来解决这个问题,我会试试看如果你能解决问题,我会发邮件给你。:(好的,我会等待你的回复
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.
<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; }
}