0

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

4

1 回答 1

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

模板在 UserControl 中定义。

于 2013-03-04T09:46:07.003 回答