コード例 #1
0
        /// <summary>
        /// Loads xaml content from a WPF package.
        /// </summary>
        /// <param name="stream">
        /// Stream that must be accessible for reading and structured as
        /// a WPF container: part XamlEntryPart is expected as one of
        /// its entry parts.
        /// </param>
        /// <returns>
        /// Returns a xaml element loaded from the entry part of the package.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Throws parsing exception when the xaml content does not comply with the xaml schema.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Throws validation exception when the package is not well structured.
        /// </exception>
        /// <exception cref="Exception">
        /// Throws uri exception when the pachageBaseUri is not correct absolute uri.
        /// </exception>
        /// <remarks>
        /// USED IN LEXICON VIA REFLECTION
        /// </remarks>
        internal static object LoadElement(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            object xamlObject;

            try
            {
                WpfPayload wpfPayload = WpfPayload.OpenWpfPayload(stream);

                // Now load the package
                using (wpfPayload.Package)
                {
                    // Validate WPF paypoad and get its entry part
                    PackagePart xamlEntryPart = wpfPayload.ValidatePayload();

                    // Define a unique uri for this instance of PWF payload.
                    // Uniqueness is required to make sure that cached images are not mixed up.
                    int newWpfPayoutCount = Interlocked.Increment(ref _wpfPayloadCount);
                    Uri payloadUri        = new Uri("payload://wpf" + newWpfPayoutCount, UriKind.Absolute);
                    Uri entryPartUri      = PackUriHelper.Create(payloadUri, xamlEntryPart.Uri); // gives an absolute uri of the entry part
                    Uri packageUri        = PackUriHelper.GetPackageUri(entryPartUri);           // extracts package uri from combined package+part uri
                    PackageStore.AddPackage(packageUri, wpfPayload.Package);                     // Register the package

                    // Set this temporary uri as a base uri for xaml parser
                    ParserContext parserContext = new ParserContext();
                    parserContext.BaseUri = entryPartUri;

                    // Call xaml parser
                    xamlObject = XamlReader.Load(xamlEntryPart.GetStream(), parserContext);

                    // Remove the temporary uri from the PackageStore
                    PackageStore.RemovePackage(packageUri);
                }
            }
            catch (XamlParseException e)
            {
                // Incase of xaml parsing or package structure failure
                // we return null.
                Invariant.Assert(e != null); //to make compiler happy about not using a variable e. This variable is useful in debugging process though - to see a reason of a parsing failure
                xamlObject = null;
            }
            catch (System.IO.FileFormatException)
            {
                xamlObject = null;
            }
            catch (System.IO.FileLoadException)
            {
                xamlObject = null;
            }
            catch (System.OutOfMemoryException)
            {
                xamlObject = null;
            }

            return(xamlObject);
        }
コード例 #2
0
        // Token: 0x06003F20 RID: 16160 RVA: 0x0012075C File Offset: 0x0011E95C
        internal static object LoadElement(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            object result;

            try
            {
                WpfPayload wpfPayload = WpfPayload.OpenWpfPayload(stream);
                using (wpfPayload.Package)
                {
                    PackagePart packagePart = wpfPayload.ValidatePayload();
                    int         num         = Interlocked.Increment(ref WpfPayload._wpfPayloadCount);
                    Uri         packageUri  = new Uri("payload://wpf" + num, UriKind.Absolute);
                    Uri         uri         = PackUriHelper.Create(packageUri, packagePart.Uri);
                    Uri         packageUri2 = PackUriHelper.GetPackageUri(uri);
                    PackageStore.AddPackage(packageUri2, wpfPayload.Package);
                    ParserContext parserContext = new ParserContext();
                    parserContext.BaseUri = uri;
                    bool useRestrictiveXamlReader = !Clipboard.UseLegacyDangerousClipboardDeserializationMode();
                    result = XamlReader.Load(packagePart.GetStream(), parserContext, useRestrictiveXamlReader);
                    PackageStore.RemovePackage(packageUri2);
                }
            }
            catch (XamlParseException ex)
            {
                Invariant.Assert(ex != null);
                result = null;
            }
            catch (FileFormatException)
            {
                result = null;
            }
            catch (FileLoadException)
            {
                result = null;
            }
            catch (OutOfMemoryException)
            {
                result = null;
            }
            return(result);
        }