private void LoadPackage(Stream xapStream, ExtensionLink link, Action actionWhenExtensionLoaded = null)
        {
            string          xapFileName = String.Format("{0}.xap", link.Package);
            string          assemblyKey = System.IO.Path.ChangeExtension(xapFileName, null);
            List <Assembly> assemblies  = XapHelper.LoadPackageAssemblies(xapStream);

            foreach (var assembly in assemblies)
            {
                var assemblyName = assembly.FullName.Substring(0, assembly.FullName.IndexOf(','));
                if (assemblyName.Equals(assemblyKey))
                {
                    var obj = assembly.CreateInstance(String.Format("{0}.Extension", assemblyKey));
                    link.Extension = obj as Extension;
                    if (_extensionLoaded != null)
                    {
                        _extensionLoaded(this, new ExtensionEventArgs(obj as Extension));
                    }
                    if (actionWhenExtensionLoaded != null)
                    {
                        actionWhenExtensionLoaded();
                    }
                    break;
                }
            }
        }
 /// <summary>
 /// Loads an extension if not already loaded.
 /// </summary>
 /// <param name="link">The ExtensionLink object.</param>
 public void CheckExtension(ExtensionLink link, Action actionWhenExtensionLoaded = null)
 {
     if (link.Extension == null)
     {
         string xapFileName = String.Format("{0}.xap", link.Package);
         if (Application.Current.IsRunningOutOfBrowser)
         {
             // loads package from the isolated storage
             using (var store = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 // TODO : check for update on the server
                 using (IsolatedStorageFileStream xapStream =
                            new IsolatedStorageFileStream(
                                xapFileName, FileMode.Open, store))
                 {
                     LoadPackage(xapStream, link);
                 }
             }
         }
         else
         {
             // loads package from the server
             Uri       xapUri    = new Uri(xapFileName, UriKind.RelativeOrAbsolute);
             WebClient webClient = new WebClient();
             webClient.OpenReadCompleted +=
                 (sender, e) =>
             {
                 Stream xapStream = e.Result;
                 try
                 {
                     LoadPackage(xapStream, link, actionWhenExtensionLoaded);
                 }
                 finally
                 {
                     xapStream.Close();
                 }
             };
             webClient.OpenReadAsync(xapUri);
         }
     }
 }