【问题标题】:WPF window with user control throwing exception in XAML designerXAML 设计器中带有用户控件引发异常的 WPF 窗口
【发布时间】:2016-04-21 05:54:37
【问题描述】:

我有一个包含用户控件的 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 以及用户控件嵌套在窗口内的事实有关,但这只是我的猜测。有没有人见过这样的东西?

【问题讨论】:

    标签: c# wpf xaml exception user-controls


    【解决方案1】:

    设计器正在您的转换器中执行代码,但由于路径在设计时无效。

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

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

    【讨论】:

    • 非常感谢,克里斯。这正是正在发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多