locked
Wrapping a third party control? RRS feed

  • Question

  • What's the right way to wrap a third party control?  For example, the PivotViewer control doesn't work at all in Blend.  I created a UserControl, put the PivotViewer inside, programmatically assigned it to UserControl.Content (unless in design mode), pass all runtime calls through to the internal PivotViewer, and now it works great in Blend.  The advantage over this solution is I never have to change any XAML: by wrapping PivotViewer I now have a class that works well in Blend and at run time.  However, the performance of this control is now sluggish.  When clearing filters at runtime, the animations within the PivotViewer are clearly off.  Switching back to just the plain PivotViewer eliminates the problem so clearly my wrapper class is the issue.  I don't understand (1) why it's causing a performance hit and (2) how to properly wrap third party controls in Silverlight so they work in Blend.  (I've heard a number of controls have issues in Blend so I'm guessing this is a common issue, but I can't seem to find an answer.)  Any suggestions?

     

    Here's my XAML:

    <UserControl x:Class="System.Windows.Pivot.BlendablePivotViewer"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    </UserControl>
    

    Here's my code behind:

    namespace System.Windows.Pivot
    {
      public partial class BlendablePivotViewer
      {
        private readonly PivotViewer _realPivot;
        private static bool? _isInDesignMode;
    
        public BlendablePivotViewer() : this(new PivotViewer())
        {
        }
    
        public BlendablePivotViewer(ResourceDictionary rd) : this(new PivotViewer(rd))
        {
        }
    
        public BlendablePivotViewer(PivotViewer pv)
        {
          _realPivot = pv;
          InitializeComponent();
    
          if (!IsInDesignMode)
            Content = _realPivot;
        }
    
        /// <summary>
        /// Gets a value indicating whether the control is in design mode
        /// (running in Blend or Visual Studio).
        /// </summary>
        public static bool IsInDesignMode
        {
          get
          {
            if (!_isInDesignMode.HasValue)
              _isInDesignMode = DesignerProperties.IsInDesignTool;
    
            return _isInDesignMode.Value;
          }
        }
    
        public event PropertyChangedEventHandler PropertyChanged
        {
          add { _realPivot.PropertyChanged += value; }
          remove { _realPivot.PropertyChanged -= value; }
        }
    
        public IDictionary<string, IList<string>> AppliedFilters
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.AppliedFilters;
          }
        }
    
        public int CollectionItemCount
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.CollectionItemCount;
          }
        }
    
        public string CollectionName
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.CollectionName;
          }
        }
    
        public Uri CollectionUri
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.CollectionUri;
          }
        }
    
        public string CurrentItemId
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.CurrentItemId;
          }
          set
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            _realPivot.CurrentItemId = value;
          }
        }
    
        public ICollection<string> InScopeItemIds
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.InScopeItemIds;
          }
        }
    
        public string SortFacetCategory
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.SortFacetCategory;
          }
        }
    
        public string ViewerState
        {
          get
          {
            if (IsInDesignMode)
              throw new NotImplementedException();
    
            return _realPivot.ViewerState;
          }
        }
    
        public event EventHandler CollectionLoadingCompleted
        {
          add { _realPivot.CollectionLoadingCompleted += value; }
          remove { _realPivot.CollectionLoadingCompleted -= value; }
        }
    
        public event EventHandler<CollectionErrorEventArgs> CollectionLoadingFailed
        {
          add { _realPivot.CollectionLoadingFailed += value; }
          remove { _realPivot.CollectionLoadingFailed -= value; }
        }
    
        public event EventHandler<ItemActionEventArgs> ItemActionExecuted
        {
          add { _realPivot.ItemActionExecuted += value; }
          remove { _realPivot.ItemActionExecuted -= value; }
        }
    
        public event EventHandler<ItemEventArgs> ItemDoubleClicked
        {
          add { _realPivot.ItemDoubleClicked += value; }
          remove { _realPivot.ItemDoubleClicked -= value; }
        }
    
        public event EventHandler<LinkEventArgs> LinkClicked
        {
          add { _realPivot.LinkClicked += value; }
          remove { _realPivot.LinkClicked -= value; }
        }
    
        public PivotItem GetItem(string id)
        {
          if (IsInDesignMode)
            throw new NotImplementedException();
    
          return _realPivot.GetItem(id);
        }
    
        public void LoadCollection(string collectionUri, string viewerState)
        {
          if (IsInDesignMode)
            throw new NotImplementedException();
    
          _realPivot.LoadCollection(collectionUri, viewerState);
        }
    
        public override void OnApplyTemplate()
        {
          _realPivot.OnApplyTemplate();
        }
    
        public static void SetResourceManager(ResourceManager resourceManager)
        {
          PivotViewer.SetResourceManager(resourceManager);
        }
      }
    }
    

    Wednesday, August 18, 2010 10:36 PM