Exemplo n.º 1
0
        private static void BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
        {
            if (result.Exception != null)
            {
                result.IsCompleted = true;
                userCallback(result);
                return;
            }

            try
            {
                string pagePathAndName = UriParsingHelper.InternalUriGetBaseValue(result.Uri);

                Type factoryType = GetXamlPageFactoryType(pagePathAndName);

                if (factoryType == null)
                {
                    result.Exception = new InvalidOperationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            Resource.PageResourceContentLoader_NoXAMLWasFound,
                            pagePathAndName));
                    return;
                }

                try
                {
                    result.Content = factoryType.GetMethod("Instantiate").Invoke(null, null);
                }
                catch (Exception ex)
                {
                    result.Exception = new InvalidOperationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            Resource.PageResourceContentLoader_XAMLWasUnloadable,
                            pagePathAndName),
                        ex);
                    return;
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            finally
            {
                result.IsCompleted = true;
                if (userCallback != null)
                {
                    userCallback(result);
                }
            }
        }
        /// <summary>
        /// Completes the asynchronous loading of content
        /// </summary>
        /// <param name="asyncResult">The result returned from <see cref="BeginLoad(Uri,AsyncCallback,object)"/>, and passed in to the callback function.</param>
        /// <returns>The content loaded, or null if content was not loaded</returns>
        public override object EndLoad(IAsyncResult asyncResult)
        {
            Guard.ArgumentNotNull(asyncResult, "asyncResult");
            PageResourceContentLoaderAsyncResult result = asyncResult as PageResourceContentLoaderAsyncResult;

            if (result == null)
            {
                throw new InvalidOperationException("Wrong kind of IAsyncResult passed in.  The IAsyncResult passed in should only come from PageResourceContentLoader.BeginLoad.");
            }
            if (result.Exception != null)
            {
                throw result.Exception;
            }

            return(result.Content);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Completes the asynchronous loading of content
        /// </summary>
        /// <param name="asyncResult">The result returned from <see cref="BeginLoad(Uri,Uri,AsyncCallback,object)"/>, and passed in to the callback function.</param>
        /// <returns>The content loaded, or null if content was not loaded</returns>
        public LoadResult EndLoad(IAsyncResult asyncResult)
        {
            Guard.ArgumentNotNull(asyncResult, "asyncResult");
            PageResourceContentLoaderAsyncResult result = asyncResult as PageResourceContentLoaderAsyncResult;

            if (result == null)
            {
                throw new InvalidOperationException(String.Format(Resource.PageResourceContentLoader_WrongIAsyncResult, "IAsyncResult", "PageResourceContentLoader.BeginLoad"));
            }
            if (result.Exception != null)
            {
                throw result.Exception;
            }

            return(new LoadResult(result.Content));
        }
        /// <summary>
        /// Begins asynchronous loading of the provided <paramref name="uri"/>.
        /// </summary>
        /// <param name="uri">A URI value to resolve and begin loading.</param>
        /// <param name="userCallback">A callback function that will be called when this asynchronous request is ready to have <see cref="EndLoad"/> called on it.</param>
        /// <param name="asyncState">A custom state object that will be returned in <see cref="IAsyncResult.AsyncState"/>, to correlate between multiple async calls.</param>
        /// <returns>An <see cref="IAsyncResult"/> that can be passed to <see cref="CancelLoad(IAsyncResult)"/> at any time, or <see cref="EndLoad(IAsyncResult)"/> after the <paramref name="userCallback"/> has been called.</returns>
        public override IAsyncResult BeginLoad(Uri uri, AsyncCallback userCallback, object asyncState)
        {
            PageResourceContentLoaderAsyncResult result = new PageResourceContentLoaderAsyncResult(uri, asyncState);

            if (uri == null)
            {
                result.Exception = new ArgumentNullException("uri");
            }

            if (SynchronizationContext.Current != null)
            {
                SynchronizationContext.Current.Post((args) => BeginLoad_OnUIThread(userCallback, result), null);
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => BeginLoad_OnUIThread(userCallback, result));
            }

            return(result);
        }
        private static void BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
        {
            if (result.Exception != null)
            {
                result.IsCompleted = true;
                userCallback(result);
                return;
            }

            try
            {
                string pagePathAndName = UriParsingHelper.InternalUriGetBaseValue(result.Uri);

                string xaml = GetLocalXaml(pagePathAndName);

                if (String.IsNullOrEmpty(xaml))
                {
                    result.Exception = new InvalidOperationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            Resource.PageResourceContentLoader_NoXAMLWasFound,
                            pagePathAndName));
                    return;
                }

                string classString = GetXClass(xaml);

                if (String.IsNullOrEmpty(classString))
                {
                    try
                    {
                        result.Content = XamlReader.Load(xaml);
                    }
                    catch (Exception ex)
                    {
                        result.Exception = new InvalidOperationException(
                            String.Format(
                                CultureInfo.CurrentCulture,
                                Resource.PageResourceContentLoader_XAMLWasUnloadable,
                                pagePathAndName),
                            ex);
                        return;
                    }
                }
                else
                {
                    // If it does have an x:Class attribute, then it has a
                    // code-behind, so get the CLR type of the XAML instead.
                    Type t = GetTypeFromAnyLoadedAssembly(classString);

                    if (t == null)
                    {
                        result.Exception = new InvalidOperationException(String.Format(
                                                                             CultureInfo.CurrentCulture,
                                                                             Resource.PageResourceContentLoader_TheTypeSpecifiedInTheXClassCouldNotBeFound,
                                                                             classString,
                                                                             pagePathAndName));
                        return;
                    }

                    result.Content = Activator.CreateInstance(t);
                    return;
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            finally
            {
                result.IsCompleted = true;
                if (userCallback != null)
                {
                    userCallback(result);
                }
            }
        }
        private static void BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
        {
            if (result.Exception != null)
            {
                result.IsCompleted = true;
                userCallback(result);
                return;
            }

            try
            {
                string pagePathAndName = UriParsingHelper.InternalUriGetBaseValue(result.Uri);

                string xaml = GetLocalXaml(pagePathAndName);

                if (String.IsNullOrEmpty(xaml))
                {
                    result.Exception = new InvalidOperationException(
                                        String.Format(
                                            CultureInfo.CurrentCulture,
                                            Resource.PageResourceContentLoader_NoXAMLWasFound,
                                            pagePathAndName));
                    return;
                }

                string classString = GetXClass(xaml);

                if (String.IsNullOrEmpty(classString))
                {
                    try
                    {
                        result.Content = XamlReader.Load(xaml);
                    }
                    catch (Exception ex)
                    {
                        result.Exception = new InvalidOperationException(
                                            String.Format(
                                                CultureInfo.CurrentCulture,
                                                Resource.PageResourceContentLoader_XAMLWasUnloadable,
                                                pagePathAndName),
                                            ex);
                        return;
                    }
                }
                else
                {
                    // If it does have an x:Class attribute, then it has a
                    // code-behind, so get the CLR type of the XAML instead.
                    Type t = GetTypeFromAnyLoadedAssembly(classString);

                    if (t == null)
                    {
                        result.Exception = new InvalidOperationException(String.Format(
                                            CultureInfo.CurrentCulture,
                                            Resource.PageResourceContentLoader_TheTypeSpecifiedInTheXClassCouldNotBeFound,
                                            classString,
                                            pagePathAndName));
                        return;
                    }

                    result.Content = Activator.CreateInstance(t);
                    return;
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            finally
            {
                result.IsCompleted = true;
                if (userCallback != null)
                {
                    userCallback(result);
                }
            }
        }
        /// <summary>
        /// Begins asynchronous loading of the provided <paramref name="targetUri"/>.
        /// </summary>
        /// <param name="targetUri">A URI value to resolve and begin loading.</param>
        /// <param name="currentUri">The URI that is currently loaded.</param>
        /// <param name="userCallback">A callback function that will be called when this asynchronous request is ready to have <see cref="EndLoad"/> called on it.</param>
        /// <param name="asyncState">A custom state object that will be returned in <see cref="IAsyncResult.AsyncState"/>, to correlate between multiple async calls.</param>
        /// <returns>An <see cref="IAsyncResult"/> that can be passed to <see cref="CancelLoad(IAsyncResult)"/> at any time, or <see cref="EndLoad(IAsyncResult)"/> after the <paramref name="userCallback"/> has been called.</returns>
        public IAsyncResult BeginLoad(Uri targetUri, Uri currentUri, AsyncCallback userCallback, object asyncState)
        {
            PageResourceContentLoaderAsyncResult result = new PageResourceContentLoaderAsyncResult(targetUri, asyncState);

            if (targetUri == null)
            {
                result.Exception = new ArgumentNullException("targetUri");
            }

            if (SynchronizationContext.Current != null)
            {
                SynchronizationContext.Current.Post((args) => BeginLoad_OnUIThread(userCallback, result), null);
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => BeginLoad_OnUIThread(userCallback, result));
            }

            return result;
        }