2014-06-16 105 views
0

我動態創建說,在環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應用程序。

+0

那麼,你可以創建一個圖像/視圖/網格的集合,將類似於:列表 usedImages。然後你可以使用這個列表。但爲什麼你需要300個觀點?也許有更簡單的解決方案。 – Olter

+0

@Olter我在屏幕上工作時,每個根佈局將有3 childern是圖像和2文本。你能向我展示你指向的東西嗎? – user2056563

+0

Em,剛纔看到這個:http://stackoverflow.com/questions/24190302/reusing-text-views-and-grid請不要提出重複的問題。 – Olter

回答

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;} 
} 

//代碼隱藏僅僅添加數據

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

+0

我試過這給了我內存不足error.any其他解決方案 – user2056563

+0

[StackReference](http://stackoverflow.com/questions/13816569/why-do-i-get-out-outofmemoryexception-when-i-have-images-in-my-listbox?rq = 1) – Eldho

1

添加布局根300項將definatly使頁面加載緩慢。您需要使用實現像列表框這樣的虛擬化的控件。這裏是如何在你的頁面上使用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; } 
} 

採取虛擬化的優勢,請加在電網控制你的列表框。否則它可以拋出內存異常,也將是緩慢的

+0

感謝我的情況,我沒有列表框,它的自定義類它擴展面板 – user2056563

+0

比嘗試從virtualizedpanel擴展它 –

+0

我的同事甚至已經通過郵件給你的項目你?你還記得嗎 ? – user2056563