Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C# 4.0

Dynamically Loading Silverlight Video Players using MEF

,
Rate me:
Please Sign up or sign in to vote.
4.97/5 (10 votes)
24 Aug 2010Ms-PL8 min read 55.3K   2.7K   16  
An example using MEF to dynamically load Silverlight Video Players
  • completeprojectwithallsamplevideoplayers.zip
    • PhilMiddlemissTheme
    • SkinableVideoPlayer.sln
    • SkinableVideoPlayer.Web
    • SkinableVideoPlayer
      • App.xaml
      • App.xaml.cs
      • Bin
        • Debug
          • AppManifest.xaml
          • ar
            • System.Windows.Controls.resources.dll
          • bg
            • System.Windows.Controls.resources.dll
          • ca
            • System.Windows.Controls.resources.dll
          • cs
            • System.Windows.Controls.resources.dll
          • da
            • System.Windows.Controls.resources.dll
          • de
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • el
            • System.Windows.Controls.resources.dll
          • es
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • et
            • System.Windows.Controls.resources.dll
          • eu
            • System.Windows.Controls.resources.dll
          • fi
            • System.Windows.Controls.resources.dll
          • fr
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • he
            • System.Windows.Controls.resources.dll
          • hr
            • System.Windows.Controls.resources.dll
          • hu
            • System.Windows.Controls.resources.dll
          • id
            • System.Windows.Controls.resources.dll
          • it
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • ja
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • ko
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • lt
            • System.Windows.Controls.resources.dll
          • lv
            • System.Windows.Controls.resources.dll
          • Microsoft.Expression.Interactions.dll
          • Microsoft.Expression.Interactions.xml
          • ms
            • System.Windows.Controls.resources.dll
          • nl
            • System.Windows.Controls.resources.dll
          • no
            • System.Windows.Controls.resources.dll
          • pl
            • System.Windows.Controls.resources.dll
          • pt
            • System.Windows.Controls.resources.dll
          • pt-BR
            • System.Windows.Controls.resources.dll
          • ro
            • System.Windows.Controls.resources.dll
          • ru
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • sk
            • System.Windows.Controls.resources.dll
          • SkinableVideoPlayer.dll
          • SkinableVideoPlayer.xap
          • SkinableVideoPlayerTestPage.html
          • SkinableVideoPlayerViewModel.dll
          • sl
            • System.Windows.Controls.resources.dll
          • sr-Cyrl-CS
            • System.Windows.Controls.resources.dll
          • sr-Latn-CS
            • System.Windows.Controls.resources.dll
          • sv
            • System.Windows.Controls.resources.dll
          • System.ComponentModel.Composition.dll
          • System.ComponentModel.Composition.Initialization.dll
          • System.ComponentModel.Composition.Initialization.xml
          • System.ComponentModel.Composition.xml
          • System.CoreEx.dll
          • System.Observable.dll
          • System.Reactive.dll
          • System.Windows.Controls.dll
          • System.Windows.Controls.Toolkit.dll
          • System.Windows.Controls.Toolkit.Internals.dll
          • System.Windows.Controls.Toolkit.xml
          • System.Windows.Controls.xml
          • System.Windows.Interactivity.dll
          • System.Windows.Interactivity.xml
          • th
            • System.Windows.Controls.resources.dll
          • tr
            • System.Windows.Controls.resources.dll
          • uk
            • System.Windows.Controls.resources.dll
          • vi
            • System.Windows.Controls.resources.dll
          • zh-Hans
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • zh-Hant
            • System.ComponentModel.Composition.Initialization.resources.dll
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
        • Release
      • classes
      • Models
      • Properties
      • Service References
        • wsSkinableVideoPlayer
          • configuration.svcinfo
          • configuration91.svcinfo
          • Reference.cs
          • Reference.svcmap
          • SkinableVideoPlayer.wsSkinableVideoPlayer.GetVideosResponse.datasource
          • SkinableVideoPlayer.wsSkinableVideoPlayer.GetXapsResponse.datasource
          • WebService.disco
          • WebService.wsdl
      • ServiceReferences.ClientConfig
      • SkinableVideoPlayer.csproj
      • SkinableVideoPlayer.csproj.user
      • ViewModels
      • Views
    • SkinableVideoPlayer_View
    • SkinableVideoPlayerViewModel
      • Bin
        • Debug
          • ar
          • bg
          • ca
          • cs
          • da
          • de
          • el
          • es
          • et
          • eu
          • fi
          • fr
          • he
          • hr
          • hu
          • id
          • it
          • ja
          • ko
          • lt
          • lv
          • ms
          • nl
          • no
          • pl
          • pt
          • pt-BR
          • ro
          • ru
          • sk
          • SkinableVideoPlayerViewModel.dll
          • sl
          • sr-Cyrl-CS
          • sr-Latn-CS
          • sv
          • th
          • tr
          • uk
          • vi
          • zh-Hans
          • zh-Hant
        • Release
      • ClassDiagram1.cd
      • classes
      • Model
      • Properties
      • Service References
        • wsSkinableVideoPlayer
          • configuration.svcinfo
          • configuration91.svcinfo
          • Reference.cs
          • Reference.svcmap
          • SkinableVideoPlayerViewModel.wsSkinableVideoPlayer.GetVideosResponse.datasource
          • SkinableVideoPlayerViewModel.wsSkinableVideoPlayer.GetXapsResponse.datasource
          • WebService.disco
          • WebService.wsdl
      • ServiceReferences.ClientConfig
      • SkinableVideoPlayerViewModel.csproj
      • SkinableVideoPlayerViewModel.csproj.user
      • ViewModel
  • versionone.zip
    • SkinableVideoPlayer.sln
    • WebService.cs
    • SkinableVideoPlayer.xap
    • VersionOne.xap
    • Default.aspx
    • Silverlight.js
    • desktop.ini
    • niceday.wmv
    • Thumbs.db
    • web.config
    • WebService.asmx
    • App.xaml
    • App.xaml.cs
    • AppManifest.xaml
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • Microsoft.Expression.Interactions.dll
    • Microsoft.Expression.Interactions.xml
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • SkinableVideoPlayer.dll
    • SkinableVideoPlayer.xap
    • SkinableVideoPlayerTestPage.html
    • SkinableVideoPlayerViewModel.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.dll
    • System.ComponentModel.Composition.Initialization.dll
    • System.ComponentModel.Composition.Initialization.xml
    • System.ComponentModel.Composition.xml
    • System.Windows.Controls.dll
    • System.Windows.Controls.Toolkit.dll
    • System.Windows.Controls.Toolkit.Internals.dll
    • System.Windows.Controls.Toolkit.xml
    • System.Windows.Controls.xml
    • System.Windows.Interactivity.dll
    • System.Windows.Interactivity.xml
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • DelegateCommand.cs
    • DeploymentCatalogService.cs
    • Extensions.cs
    • SilverlightXaps.cs
    • AppManifest.xml
    • AssemblyInfo.cs
    • configuration.svcinfo
    • configuration91.svcinfo
    • Reference.cs
    • Reference.svcmap
    • SkinableVideoPlayer.wsSkinableVideoPlayer.GetVideosResponse.datasource
    • SkinableVideoPlayer.wsSkinableVideoPlayer.GetXapsResponse.datasource
    • WebService.disco
    • WebService.wsdl
    • ServiceReferences.ClientConfig
    • SkinableVideoPlayer.csproj
    • SkinableVideoPlayer.csproj.user
    • MainPageModel.cs
    • MainPage.xaml
    • MainPage.xaml.cs
    • SkinableVideoPlayerViewModel.dll
    • SkinableVideoPlayerViewModel.pdb
    • DelegateCommand.cs
    • SilverlightVideos.cs
    • obj
    • VersionOne
      • App.xaml
      • App.xaml.cs
      • Bin
        • Debug
          • AppManifest.xaml
          • ar
            • System.Windows.Controls.resources.dll
          • bg
            • System.Windows.Controls.resources.dll
          • ca
            • System.Windows.Controls.resources.dll
          • cs
            • System.Windows.Controls.resources.dll
          • da
            • System.Windows.Controls.resources.dll
          • de
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • el
            • System.Windows.Controls.resources.dll
          • es
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • et
            • System.Windows.Controls.resources.dll
          • eu
            • System.Windows.Controls.resources.dll
          • fi
            • System.Windows.Controls.resources.dll
          • fr
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • he
            • System.Windows.Controls.resources.dll
          • hr
            • System.Windows.Controls.resources.dll
          • hu
            • System.Windows.Controls.resources.dll
          • id
            • System.Windows.Controls.resources.dll
          • it
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • ja
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • ko
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • lt
            • System.Windows.Controls.resources.dll
          • lv
            • System.Windows.Controls.resources.dll
          • ms
            • System.Windows.Controls.resources.dll
          • nl
            • System.Windows.Controls.resources.dll
          • no
            • System.Windows.Controls.resources.dll
          • pl
            • System.Windows.Controls.resources.dll
          • pt
            • System.Windows.Controls.resources.dll
          • pt-BR
            • System.Windows.Controls.resources.dll
          • ro
            • System.Windows.Controls.resources.dll
          • ru
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • sk
            • System.Windows.Controls.resources.dll
          • sl
            • System.Windows.Controls.resources.dll
          • sr-Cyrl-CS
            • System.Windows.Controls.resources.dll
          • sr-Latn-CS
            • System.Windows.Controls.resources.dll
          • sv
            • System.Windows.Controls.resources.dll
          • System.ComponentModel.Composition.dll
          • System.ComponentModel.Composition.xml
          • System.Windows.Controls.dll
          • System.Windows.Controls.Toolkit.dll
          • System.Windows.Controls.Toolkit.Internals.dll
          • System.Windows.Controls.Toolkit.pdb
          • System.Windows.Controls.Toolkit.xml
          • System.Windows.Controls.xml
          • System.Windows.Interactivity.dll
          • System.Windows.Interactivity.xml
          • th
            • System.Windows.Controls.resources.dll
          • tr
            • System.Windows.Controls.resources.dll
          • uk
            • System.Windows.Controls.resources.dll
          • VersionOne.dll
          • VersionOne.pdb
          • VersionOne.xap
          • VersionOneTestPage.html
          • vi
            • System.Windows.Controls.resources.dll
          • zh-Hans
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
          • zh-Hant
            • System.ComponentModel.Composition.resources.dll
            • System.Windows.Controls.resources.dll
        • Release
      • obj
      • Properties
      • VersionOne.csproj
      • VersionOne.csproj.user
      • Views
  • base.zip
    • SkinableVideoPlayer.sln
    • WebService.cs
    • SkinableVideoPlayer.xap
    • Default.aspx
    • Silverlight.js
    • desktop.ini
    • niceday.wmv
    • Thumbs.db
    • web.config
    • WebService.asmx
    • App.xaml
    • App.xaml.cs
    • AppManifest.xaml
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • Microsoft.Expression.Interactions.dll
    • Microsoft.Expression.Interactions.xml
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • SkinableVideoPlayer.dll
    • SkinableVideoPlayer.xap
    • SkinableVideoPlayerTestPage.html
    • SkinableVideoPlayerViewModel.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.dll
    • System.ComponentModel.Composition.Initialization.dll
    • System.ComponentModel.Composition.Initialization.xml
    • System.ComponentModel.Composition.xml
    • System.Windows.Controls.dll
    • System.Windows.Controls.Toolkit.dll
    • System.Windows.Controls.Toolkit.Internals.dll
    • System.Windows.Controls.Toolkit.xml
    • System.Windows.Controls.xml
    • System.Windows.Interactivity.dll
    • System.Windows.Interactivity.xml
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • System.ComponentModel.Composition.Initialization.resources.dll
    • System.ComponentModel.Composition.resources.dll
    • System.Windows.Controls.resources.dll
    • DelegateCommand.cs
    • DeploymentCatalogService.cs
    • Extensions.cs
    • SilverlightXaps.cs
    • AppManifest.xml
    • AssemblyInfo.cs
    • configuration.svcinfo
    • configuration91.svcinfo
    • Reference.cs
    • Reference.svcmap
    • SkinableVideoPlayer.wsSkinableVideoPlayer.GetVideosResponse.datasource
    • SkinableVideoPlayer.wsSkinableVideoPlayer.GetXapsResponse.datasource
    • WebService.disco
    • WebService.wsdl
    • ServiceReferences.ClientConfig
    • SkinableVideoPlayer.csproj
    • SkinableVideoPlayer.csproj.user
    • MainPageModel.cs
    • MainPage.xaml
    • MainPage.xaml.cs
    • SkinableVideoPlayerViewModel.dll
    • DelegateCommand.cs
    • SilverlightVideos.cs
    • AssemblyInfo.cs
    • configuration.svcinfo
    • configuration91.svcinfo
    • Reference.cs
    • Reference.svcmap
    • SkinableVideoPlayerViewModel.wsSkinableVideoPlayer.GetVideosResponse.datasource
    • SkinableVideoPlayerViewModel.wsSkinableVideoPlayer.GetXapsResponse.datasource
    • WebService.disco
    • WebService.wsdl
    • ServiceReferences.ClientConfig
    • SkinableVideoPlayerViewModel.csproj
    • SkinableVideoPlayerViewModel.csproj.user
    • MainViewModel.cs
// From: http://codebetter.com/blogs/glenn.block/archive/2009/11/30/building-the-hello-mef-dashboard-in-silverlight-4-part-i.aspx
// From: http://www.davidezordan.net/blog/?p=1734 & http://www.davidezordan.net/blog/?p=1576
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel;

namespace SkinableVideoPlayer
{
    // A type from which to derive the contract name that is used to export the
    // type or member marked with this attribute,
    public interface IDeploymentCatalogService
    {
        // The class that will implement this interface will implement these two methods
        void AddXap(string uri, Action<AsyncCompletedEventArgs> completedAction = null);
        void RemoveXap(string uri);
    }

    // See: Building Hello MEF – Part IV – DeploymentCatalog
    // http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx
    // See: Silverlight 4, MEF and MVVM: MEFModules, Dynamic XAP Loading and Navigation Applications
    // http://blog.answermyquery.com/2010/04/silverlight-4-mef-and-mvvm-mefmodules-dynamic-xap-loading-and-navigation-applications/
    [Export(typeof(IDeploymentCatalogService))]
    public class DeploymentCatalogService : IDeploymentCatalogService
    {
        private static AggregateCatalog _aggregateCatalog;
        Dictionary<string, DeploymentCatalog> _catalogs;

        public DeploymentCatalogService()
        {
            _catalogs = new Dictionary<string, DeploymentCatalog>();
        }

        public static void Initialize()
        {
            _aggregateCatalog = new AggregateCatalog();
            _aggregateCatalog.Catalogs.Add(new DeploymentCatalog());
            CompositionHost.Initialize(_aggregateCatalog);
        }

        public void AddXap(string uri, Action<AsyncCompletedEventArgs> completedAction = null)
        {
            // Add a .xap to the catalog
            DeploymentCatalog catalog;
            if (!_catalogs.TryGetValue(uri, out catalog))
            {
                catalog = new DeploymentCatalog(uri);

                if (completedAction != null)
                {
                    catalog.DownloadCompleted += (s, e) => completedAction(e);
                }
                else
                {
                    catalog.DownloadCompleted += catalog_DownloadCompleted;
                }

                catalog.DownloadAsync();
                _catalogs[uri] = catalog;
                _aggregateCatalog.Catalogs.Add(catalog);
            }
        }

        void catalog_DownloadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            // Chekcks for errors loading the .xap
            if (e.Error != null)
            {
                throw e.Error;
            }
        }

        public void RemoveXap(string uri)
        {
            // Remove a .xap from the catalog
            DeploymentCatalog catalog;
            if (_catalogs.TryGetValue(uri, out catalog))
            {
                _aggregateCatalog.Catalogs.Remove(catalog);
                _catalogs.Remove(uri);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions