2013-03-01 57 views
0

我需要在Windows Phone應用程序中顯示內容。它是文本,圖像,音頻,視頻等。每個Item都有作者姓名和圖片,以及List<>內容(不同的計數)。我需要展示它。現在有一個解決方案 - 使用TemplateSelectorLisboxLLS。但是內容的組合大於30和30個階段 - 大約是2000代碼行,我認爲它是一個糟糕的解決方案。我試圖進行通用控制,其中包括內容的所有容器(控件),並且我僅填充了Item(空容器剛剛最小化)的內容,但性能非常差(每個DataTemplate都有10-11個控件) 。一個控件的解決方案是好的,但我需要一個很好的性能。有什麼辦法解決這個問題嗎?我應該使用什麼控件來顯示不同的內容?

回答

0

我有同樣的問題在Windows Phone中顯示不同的圖釘。 我做了一個用戶控件,並設置在代碼隱藏特定的模板:

public partial class Map : UserControl 
{ 
    public Map() 
    { 
     InitializeComponent(); 
     this.Loaded += Map_Loaded; 
    } 

    void Map_Loaded(object sender, RoutedEventArgs e) 
    { 
     (this.DataContext as MyViewModel).LoadingItemsCompleted += OnLoadingItemsCompleted; 
    } 

    private void OnLoadingItemsCompleted(object sender, EventArgs eventArgs) 
    { 
     // Don't care about thats 
     ObservableCollection<DependencyObject> children = Microsoft.Phone.Maps.Toolkit.MapExtensions.GetChildren(map); 
     MapItemsControl itemsControl = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl; 

     // Here ! 
     foreach (GeolocalizableModel item in (this.DataContext as MyViewModel).Items) 
     { 
      Pushpin pushpin = new Pushpin(); 
      switch (item.PushpinTemplate) 
      { 
       case Server.PushpinTemplate.First: 
        pushpin.Template = this.Resources["firstPushpinTemplate"] as ControlTemplate; 
        break; 
       case Server.PushpinTemplate.Second: 
        pushpin.Template = this.Resources["secondPushpinTemplate"] as ControlTemplate; 
        break; 
       case Server.PushpinTemplate.Third: 
        pushpin.Template = this.Resources["thirdPushpinTemplate"] as ControlTemplate; 
        break; 
       default: 
        if (PushpinTemplate != null) pushpin.Template = PushpinTemplate; 
        break; 
      } 

      pushpin.Content = item.PushpinContent; 
      pushpin.GeoCoordinate = item.Location; 
      itemsControl.Items.Add(pushpin); 
     } 
    } 

    public ControlTemplate PushpinTemplate 
    { 
     get { return (ControlTemplate)GetValue(PushpinTemplateProperty); } 
     set { SetValue(PushpinTemplateProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for PushpinTemplate. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty PushpinTemplateProperty = 
     DependencyProperty.Register("PushpinTemplate", typeof(ControlTemplate), typeof(Map), new PropertyMetadata(null)); 
} 

的模板在用戶控件定義。

相關問題