简体   繁体   中英

WPF window with user control throwing exception in XAML designer

I have a WPF window which contains a user control. The user control contains an image which is bound to a boolean property through the use of a value converter that looks like this:

class BooleanStatusToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value == true)
            {
                return new BitmapImage(new Uri("pack://application:,,,/Resources/green_orb_24x24.png", UriKind.Absolute));
            }
            else
            {
                return new BitmapImage(new Uri("pack://application:,,,/Resources/red_orb_24x24.png", UriKind.Absolute));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

When I run my application, everything seems to be working properly. However, when I view my window in the XAML designer, I get an IOException (from the user control) which indicates that it can't find resources/red_orb_24x24.png (the image URI returned from the value converter when the boolean property is false). The stack trace from the exception looks like this:

at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.get_ContentType()
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource, RequestCachePolicy uriCachePolicy)
at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource)
at MyTestApplcation.BooleanStatusToImageConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()

I'm guessing that this may be related to my URI and the fact that the user control is nested inside the window but that's just a guess on my part. Has anyone seen something like this before?

The designer is executing the code in your converter and that's failing because the path isn't valid at design time.

What you need to do is check whether you're running in design mode or not and not execute the code if you are:

// 'this' is your UI element
bool inDesign = DesignerProperties.GetIsInDesignMode(this);

if (!inDesign)
{
    if ((bool)value == true)
    {
        return new BitmapImage(new Uri("pack://application:,,,/Resources/green_orb_24x24.png", UriKind.Absolute));
    }
    else
    {
        return new BitmapImage(new Uri("pack://application:,,,/Resources/red_orb_24x24.png", UriKind.Absolute));
    }
}
else
{
     return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM