我应该使用什么控件来显示不同的内容?

时间:2013-03-01 12:18:21

标签: c# silverlight windows-phone-7 user-controls

我需要在Windows Phone应用程序中显示内容。它是文本,图像,音频,视频等。每个Item都有作者姓名和图像,以及List<>内容(不同的计数)。我需要展示它。我现在有一个解决方案 - 将TemplateSelectorLisboxLLS一起使用。但是内容的组合大于30,并且30个temlates - 大约是2000个代码行,我认为这是一个糟糕的解决方案。我尝试进行通用控制,其中包括内容的所有容器(控件),我只填充Item中的内容(空容器最小化),但性能非常差(每个{{1有10-11个控件)。使用一个控件的解决方案很好,但我需要良好的性能。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

我在Windows Phone中显示不同的图钉时遇到同样的问题。 我做了一个usercontrol并在代码隐藏中设置了一个特定的模板:

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

模板在UserControl中定义。