Esempio n. 1
0
        /// <summary>
        /// Called by Silverlight host when it instantiates our application
        /// </summary>
        public DynamicApplication()
        {
            if (_Current != null) {
                throw new Exception("Only one instance of DynamicApplication can be created");
            }

            _Current = this;
            _UIThreadId = Thread.CurrentThread.ManagedThreadId;
            _HtmlPageUri = HtmlPageUri;

            Settings.ReportUnhandledErrors = true;

            AppManifest = new DynamicAppManifest();
            LanguagesConfig = DynamicLanguageConfig.Create(AppManifest.Assemblies);

            Startup += new StartupEventHandler(DynamicApplication_Startup);
        }
        /// <summary>
        /// Called by Silverlight host when it instantiates our application
        /// </summary>
        public DynamicApplication()
        {
            if (_Current != null)
            {
                throw new Exception("Only one instance of DynamicApplication can be created");
            }

            _Current     = this;
            _UIThreadId  = Thread.CurrentThread.ManagedThreadId;
            _HtmlPageUri = HtmlPageUri;

            Settings.ReportUnhandledErrors = true;

            AppManifest     = new DynamicAppManifest();
            LanguagesConfig = DynamicLanguageConfig.Create(AppManifest.Assemblies);

            Startup += new StartupEventHandler(DynamicApplication_Startup);
        }
        /// <summary>
        /// Downloads languages. If there is a missing language assembly in the
        /// XAP, then download the language's external package and load the
        /// package's assemblies, updating the AppManifest.Assemblies list.
        /// Calls the provided delegate when the downloadQueue is empty or if
        /// the language assemblies are all in the XAP.
        /// </summary>
        public void DownloadLanguages(DynamicAppManifest appManifest, Action onComplete)
        {
            var downloadQueue = new List <DynamicLanguageInfo>();

            foreach (var used in LanguagesUsed)
            {
                var lang = GetLanguageByName(used.Key);
                if (used.Value && lang != null && lang.External != null)
                {
                    downloadQueue.Add(lang);
                }
            }
            if (downloadQueue.Count == 0)
            {
                onComplete.Invoke();
                return;
            }
            foreach (var lang in downloadQueue)
            {
                var  xapvfs = (XapVirtualFilesystem)XapPAL.PAL.VirtualFilesystem;
                bool inXAP  = true;
                foreach (var assembly in lang.Assemblies)
                {
                    if (xapvfs.GetFile(assembly) != null)
                    {
                        BrowserPAL.PAL.LoadAssemblyFromPath(assembly);
                    }
                    else
                    {
                        inXAP = false;
                        break;
                    }
                }
                if (inXAP)
                {
                    onComplete.Invoke();
                }
                else
                {
                    WebClient wc  = new WebClient();
                    var       uri = new Uri(lang.External, UriKind.RelativeOrAbsolute);
                    wc.OpenReadCompleted += (sender, e) => {
                        // Make sure two handlers never step on eachother (could this even happen?)
                        lock (_lock) {
                            var  sri   = new StreamResourceInfo(e.Result, null);
                            var  alang = ((DynamicLanguageInfo)e.UserState);
                            bool first = true;
                            foreach (var assembly in alang.Assemblies)
                            {
                                if (xapvfs.GetFile(sri, assembly) != null)
                                {
                                    xapvfs.UsingStorageUnit(sri, () => {
                                        var asm = BrowserPAL.PAL.LoadAssemblyFromPath(assembly);
                                        if (first)
                                        {
                                            GetLanguageByName(alang.Names[0].ToLower()).LanguageContext =
                                                alang.LanguageContext.Split(',')[0] + ", " + asm.FullName;
                                            first = false;
                                        }
                                        appManifest.Assemblies.Add(asm);
                                    });
                                }
                            }
                            downloadQueue.Remove(alang);
                            if (downloadQueue.Count == 0)
                            {
                                onComplete.Invoke();
                            }
                        }
                    };
                    wc.OpenReadAsync(uri, lang);
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Downloads languages. If there is a missing language assembly in the
 /// XAP, then download the language's external package and load the
 /// package's assemblies, updating the AppManifest.Assemblies list. 
 /// Calls the provided delegate when the downloadQueue is empty or if 
 /// the language assemblies are all in the XAP.
 /// </summary>
 public void DownloadLanguages(DynamicAppManifest appManifest, Action onComplete)
 {
     var downloadQueue = new List<DynamicLanguageInfo>();
     foreach(var used in LanguagesUsed) {
         var lang = GetLanguageByName(used.Key);
         if (used.Value && lang != null && lang.External != null) {
             downloadQueue.Add(lang);
         }
     }
     if (downloadQueue.Count == 0) {
         onComplete.Invoke();
         return;
     }
     foreach (var lang in downloadQueue) {
         var xapvfs = (XapVirtualFilesystem)XapPAL.PAL.VirtualFilesystem;
         bool inXAP = true;
         foreach(var assembly in lang.Assemblies) {
             if(xapvfs.GetFile(assembly) != null) {
                 BrowserPAL.PAL.LoadAssemblyFromPath(assembly);
             } else {
                 inXAP = false;
                 break;
             }
         }
         if(inXAP) {
             onComplete.Invoke();
         } else {
             WebClient wc = new WebClient();
             var uri = new Uri(lang.External, UriKind.RelativeOrAbsolute);
             wc.OpenReadCompleted += (sender, e) => {
                 // Make sure two handlers never step on eachother (could this even happen?)
                 lock (_lock) {
                     var sri = new StreamResourceInfo(e.Result, null);
                     var alang = ((DynamicLanguageInfo) e.UserState);
                     bool first = true;
                     foreach (var assembly in alang.Assemblies) {
                         if (xapvfs.GetFile(sri, assembly) != null) {
                             xapvfs.UsingStorageUnit(sri, () => {
                                 var asm = BrowserPAL.PAL.LoadAssemblyFromPath(assembly);
                                 if (first) {
                                     GetLanguageByName(alang.Names[0].ToLower()).LanguageContext =
                                         alang.LanguageContext.Split(',')[0] + ", " + asm.FullName;
                                     first = false;
                                 }
                                 appManifest.Assemblies.Add(asm);
                             });
                         }
                     }
                     downloadQueue.Remove(alang);
                     if (downloadQueue.Count == 0) {
                         onComplete.Invoke();
                     }
                 }
             };
             wc.OpenReadAsync(uri, lang);
         }
     }
 }
Esempio n. 5
0
 void DynamicApplication_Startup(object sender, StartupEventArgs e) {
     Settings.Parse(InitParams = NormalizeInitParams(e.InitParams));
     Engine = new DynamicEngine();
     AppManifest = new DynamicAppManifest();
     AppManifest.LoadAssemblies(() => Engine.Start());
 }