Example #1
0
        private static IEnumerable <AssemblyPart> GetParts(Stream stream, ModuleInfo moduleInfo)
        {
            List <AssemblyPart> assemblyParts = new List <AssemblyPart>();

            using (var streamReader = new StreamReader(Application.GetResourceStream(
                                                           new StreamResourceInfo(stream, null),
                                                           new Uri("AppManifest.xaml", UriKind.Relative)).Stream))
            {
                using (XmlReader xmlReader = XmlReader.Create(streamReader))
                {
                    xmlReader.MoveToContent();

                    if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "Deployment")
                    {
                        string value = string.Empty, entryPointType = string.Empty;


                        if (string.IsNullOrEmpty(value = xmlReader.GetAttribute("EntryPointType")))
                        {
                            throw new ArgumentNullException("EntryPointType", new Exception("The EntryPointType is not found in AppManifest.xaml!"));
                        }

                        entryPointType = value + ",";

                        if (string.IsNullOrEmpty(value = xmlReader.GetAttribute("EntryPointAssembly")))
                        {
                            throw new ArgumentNullException("EntryPointAssembly", new Exception("The EntryPointAssembly is not found in AppManifest.xaml!"));
                        }

                        entryPointType += value;
                        if (moduleInfo.EntryPointType == null)
                        {
                            moduleInfo.m_entryPointType = entryPointType;
                        }
                    }

                    while (xmlReader.Read())
                    {
                        if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "Deployment.Parts")
                        {
                            using (XmlReader xmlReaderAssemblyParts = xmlReader.ReadSubtree())
                            {
                                while (xmlReaderAssemblyParts.Read())
                                {
                                    if (xmlReaderAssemblyParts.NodeType == XmlNodeType.Element && xmlReaderAssemblyParts.Name == "AssemblyPart")
                                    {
                                        AssemblyPart assemblyPart = new AssemblyPart();
                                        assemblyPart.Source = xmlReaderAssemblyParts.GetAttribute("Source");
                                        assemblyParts.Add(assemblyPart);
                                    }
                                }
                            }

                            break;
                        }
                    }
                }
            }

            return(assemblyParts);
        }
Example #2
0
        private void OnLoadModule(object sender, LoadCompletedEventArgs e)
        {
            LoadModuleCallback callback = e.UserState as LoadModuleCallback;

            if (e.Error != null)
            {
                ModuleInfo module = e.Request.ModuleInfo as ModuleInfo;
                lock (module)
                {
                    module.Status = ModuleStatus.Preloading;
                }
                module.Raise(this, e);

                if (callback != null)
                {
                    callback(this, new LoadedMoudleEventArgs(e.Request, e.Error, false, LoadModuleStatus.End, e.UserState));
                }
                else
                {
                    throw new PageException(MessageResource.PageException_PageLoadFailure_Title,
                                            MessageResource.PageException_PageLoadFailure_Message, e.Error, e.Request);
                }
            }

            /*
             * 如果发生页面初始化失败的时候,会导致再次打开这个页面的时候一直处于Loading状态
             * 原因是EntryPointType可能在第一次加载的时候已经初始化,导致第二次不能进行加载;
             * (By Aaron.L.Zhou)
             */
            if (e.Request.ModuleInfo.Status == ModuleStatus.Loading && e.Request.ModuleInfo.EntryPointType == null)
            {
                //是否所有的Xap Dll都已经加载完成;
                bool m_isLoaded = false;
                try
                {
                    int loadedAssemblyCount = 0;

                    Stream stream = e.Result;
                    IEnumerable <AssemblyPart> parts = GetParts(stream, e.Request.ModuleInfo as ModuleInfo);
                    ModuleInfo module     = e.Request.ModuleInfo as ModuleInfo;
                    string     moduleFile = (module.m_entryPointType.Split(',')[1] + ".dll").ToLower();
                    module.m_stream = new StreamResourceInfo(stream, "xap");
                    LoadModuleConfig(module.m_stream, module.m_config);
                    foreach (AssemblyPart item in parts)
                    {
                        LoadAssembly(stream, item, (a) =>
                        {
                            Interlocked.Increment(ref loadedAssemblyCount);

                            if (item.Source.ToLower() == moduleFile)
                            {
                                module.m_entryPointType = string.Format("{0},Version={1}",
                                                                        module.m_entryPointType, Regex.Match(a.FullName, @"Version=(?<version>[\d.]+)").Groups["version"].Value);
                            }

                            if (parts.Count() == loadedAssemblyCount)
                            {
                                m_isLoaded = true;
                                loaded(e, callback);
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    ModuleInfo module = e.Request.ModuleInfo as ModuleInfo;
                    if (!m_isLoaded)
                    {
                        lock (module)
                        {
                            module.Status = ModuleStatus.Preloading;
                        }
                    }

                    module.Raise(this, new LoadCompletedEventArgs(e.Request, null, ex, e.Cancelled, e.UserState));

                    if (callback != null)
                    {
                        callback(this, new LoadedMoudleEventArgs(e.Request, ex, false, LoadModuleStatus.End, e.UserState));
                    }
                    else
                    {
                        throw new PageException(MessageResource.PageException_ModuleInitializeError_Ttitle,
                                                MessageResource.PageException_ModuleInitializeError_Message, ex, e.Request);
                    }
                }
            }
        }