具有用户控件的WPF窗口在XAML设计器中抛出异常

时间:2016-01-15 15:28:12

标签: c# wpf xaml exception user-controls

我有一个包含用户控件的WPF窗口。用户控件包含一个图像,该图像通过使用如下所示的值转换器绑定到布尔属性:

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();
        }
    }

当我运行我的应用程序时,一切似乎都正常工作。但是,当我在XAML设计器中查看我的窗口时,我得到一个IOException(来自用户控件),表示找不到资源/ red_orb_24x24.png(当布尔属性为false时,从值转换器返回的图像URI) )。异常的堆栈跟踪如下所示:

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()

我猜这可能与我的URI以及用户控件嵌套在窗口内的事实有关,但这只是我的猜测。有没有人见过这样的东西?

1 个答案:

答案 0 :(得分:2)

设计人员正在执行转换器中的代码并且失败,因为路径在设计时无效。

您需要做的是检查您是否在设计模式下运行,如果您是以下情况,则不执行代码:

// '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;
}