コード例 #1
0
ファイル: TypeConverterHelper.cs プロジェクト: JianwenSun/cc
        internal static UriHolder GetUriFromUriContext(ITypeDescriptorContext context, object inputString)
        {
            UriHolder uriHolder = new UriHolder();

            if (inputString is string)
            {
                uriHolder.OriginalUri = new Uri((string)inputString, UriKind.RelativeOrAbsolute);
            }
            else
            {
                Debug.Assert(inputString is Uri);
                uriHolder.OriginalUri = (Uri)inputString;
            }

            if (uriHolder.OriginalUri.IsAbsoluteUri == false)
            {
                //Debug.Assert (context != null, "Context should not be null");
                if (context != null)
                {
                    IUriContext iuc = (IUriContext)context.GetService(typeof(IUriContext));

                    //Debug.Assert (iuc != null, "IUriContext should not be null here");
                    if (iuc != null)
                    {
                        // the base uri is NOT ""
                        if (iuc.BaseUri != null)
                        {

                            uriHolder.BaseUri = iuc.BaseUri;

                            if (!uriHolder.BaseUri.IsAbsoluteUri)
                            {
                                uriHolder.BaseUri = new Uri(BaseUriHelper.BaseUri, uriHolder.BaseUri);
                            }
                        } // uriHolder.BaseUriString != ""
                        else
                        {
                            // if we reach here, the base uri we got from IUriContext is ""
                            // and the inputString is a relative uri.  Here we resolve it to
                            // application's base
                            uriHolder.BaseUri = BaseUriHelper.BaseUri;
                        }
                    } // iuc != null
                } // context!= null
            } // uriHolder.OriginalUri.IsAbsoluteUri == false

            return uriHolder;
        }
コード例 #2
0
 /// <summary>
 /// Converts a string into a transform.
 /// </summary>
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (!string.IsNullOrEmpty(value))
     {
         UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context, value);
         return(BitmapFrame.CreateFromUriOrStream(
                    uriHolder.BaseUri,
                    uriHolder.OriginalUri,
                    null,
                    BitmapCreateOptions.None,
                    BitmapCacheOption.Default,
                    null
                    ));
     }
     return(base.ConvertFromString(value, context));
 }
コード例 #3
0
        internal static UriHolder GetUriFromUriContext(ITypeDescriptorContext context, object inputString)
        {
            UriHolder uriHolder = new UriHolder();

            if (inputString is string)
            {
                uriHolder.OriginalUri = new Uri((string)inputString, UriKind.RelativeOrAbsolute);
            }
            else
            {
                Debug.Assert(inputString is Uri);
                uriHolder.OriginalUri = (Uri)inputString;
            }

            if (uriHolder.OriginalUri.IsAbsoluteUri == false)
            {
                //Debug.Assert (context != null, "Context should not be null");
                if (context != null)
                {
                    IUriContext iuc = (IUriContext)context.GetService(typeof(IUriContext));

                    //Debug.Assert (iuc != null, "IUriContext should not be null here");
                    if (iuc != null)
                    {
                        // the base uri is NOT ""
                        if (iuc.BaseUri != null)
                        {
                            uriHolder.BaseUri = iuc.BaseUri;

                            if (!uriHolder.BaseUri.IsAbsoluteUri)
                            {
                                uriHolder.BaseUri = new Uri(BaseUriHelper.BaseUri, uriHolder.BaseUri);
                            }
                        } // uriHolder.BaseUriString != ""
                        else
                        {
                            // if we reach here, the base uri we got from IUriContext is ""
                            // and the inputString is a relative uri.  Here we resolve it to
                            // application's base
                            uriHolder.BaseUri = BaseUriHelper.BaseUri;
                        }
                    } // iuc != null
                }     // context!= null
            }         // uriHolder.OriginalUri.IsAbsoluteUri == false

            return(uriHolder);
        }
コード例 #4
0
ファイル: ImageSourceConverter.cs プロジェクト: yk2012985/wpf
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            try
            {
                if (value == null)
                {
                    throw GetConvertFromException(value);
                }

                if (((value is string) && (!string.IsNullOrEmpty((string)value))) || (value is Uri))
                {
                    UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context, value);
                    return(BitmapFrame.CreateFromUriOrStream(
                               uriHolder.BaseUri,
                               uriHolder.OriginalUri,
                               null,
                               BitmapCreateOptions.None,
                               BitmapCacheOption.Default,
                               null
                               ));
                }
                else if (value is byte[])
                {
                    byte[] bytes = (byte[])value;

                    if (bytes != null)
                    {
                        Stream memStream = null;

                        //
                        // this might be a magical OLE thing, try that first.
                        //
                        memStream = GetBitmapStream(bytes);

                        if (memStream == null)
                        {
                            //
                            // guess not.  Try plain memory.
                            //
                            memStream = new MemoryStream(bytes);
                        }

                        return(BitmapFrame.Create(
                                   memStream,
                                   BitmapCreateOptions.None,
                                   BitmapCacheOption.Default
                                   ));
                    }
                }
                else if (value is Stream)
                {
                    Stream stream = (Stream)value;

                    return(BitmapFrame.Create(
                               stream,
                               BitmapCreateOptions.None,
                               BitmapCacheOption.Default
                               ));
                }

                return(base.ConvertFrom(context, culture, value));
            }
            catch (Exception e)
            {
                if (!CriticalExceptions.IsCriticalException(e))
                {
                    if (context == null && CoreAppContextSwitches.OverrideExceptionWithNullReferenceException)
                    {
                        throw new NullReferenceException();
                    }

                    IProvideValueTarget ipvt = context?.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                    if (ipvt != null)
                    {
                        IProvidePropertyFallback ippf = ipvt.TargetObject as IProvidePropertyFallback;
                        DependencyProperty       dp   = ipvt.TargetProperty as DependencyProperty;
                        // We only want to call IPPF.SetValue if the target can handle it.
                        // We need to check for non DP scenarios (This is currently an internal interface used
                        //             only by Image so it's okay for now)

                        if (ippf != null && dp != null && ippf.CanProvidePropertyFallback(dp.Name))
                        {
                            return(ippf.ProvidePropertyFallback(dp.Name, e));
                        }
                    }
                }

                // We want to rethrow the exception in the case we can't handle it.
                throw;
            }
        }