【问题标题】:Trying to add an image on the Window throws 'BitmapImage must have IsFrozen set to false to modify.'尝试在 Window 上添加图像会引发“BitmapImage 必须将 IsFrozen 设置为 false 才能修改。”
【发布时间】:2017-05-19 02:15:33
【问题描述】:

App.xaml

<Application ...
         StartupUri="Views\MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <Local:ViewModelLocator x:Key="ViewModelLocator" />
                <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
                <BitmapImage x:Key="Logo" UriSource="Media/Images/Logo.png" />
            </ResourceDictionary>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

MainWindow.xaml

<Image Source="{StaticResource Logo}" Style="{StaticResource LogoStyle}" />

投掷

“System.Windows.Media.Imaging.BitmapImage”的初始化引发了 例外。'

内部:

“System.Windows.Media.Imaging.BitmapImage”类型的指定值 必须将 IsFrozen 设置为 false 才能修改。

调试器指向UriSource

堆栈

 at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at XXX.Apps.UI.Wpf.App.InitializeComponent() in C:\Projects\XXX\Apps\UI\WPF\src\XXX.Apps.UI.Wpf\App.xaml:line 1
   at XXX.Apps.UI.Wpf.App.Main()
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

内部堆栈

   at System.Windows.Freezable.WritePreamble()
   at System.Windows.Media.Imaging.BitmapImage.EndInit()
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)

为什么?我如何解决它?我在 .NET 4.5 上

编辑:

显然我什至不需要将 Image 添加到 MainWindow 来重现该问题。这与我在 App.xaml 中的声明有关...

编辑 2/修复:

作为一种临时解决方法,我已将徽标资源从 App.xaml 移到 Window 资源中,它工作正常。不过,如果我可以从 App.xaml 中使用它,那就太好了。

【问题讨论】:

  • 添加了堆栈跟踪
  • 能否也添加 InnerException 的堆栈跟踪?
  • 如果您将 Bi​​tmapImage 直接放入 Application.Resources,而不使用 MergedDictionaries,则它可以工作。这是绝对必要的吗?
  • @Clemens,我刚刚更新了问题以包含我的完整 App.xaml。因为我使用的是 MahApps,所以我认为我必须使用合并字典。我对 WPF 中的资源还不是很熟悉,有什么我可以更改的,以便解决这个问题吗?
  • 只需将其移至 MainWindow 的 XAML 中的 Window.Resources 即可。

标签: wpf xaml


【解决方案1】:

这个问题已经 3 岁了,但仍然没有答案。就这样吧。

如果您仍想使用 App.xaml 中的图像,但由于您在 App.xaml 中合并了资源字典并出现“IsFrozen”错误而无法使用,只需将您的图像资源放入您自己的资源字典中并将其合并到您的 App.xaml 中。

对于这个例子,创建一个资源字典(我称之为 ImageDictionary.xaml)。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:tradeware.Resources">
    <BitmapImage x:Key="Logo" UriSource="Media/Images/Logo.png"/>
</ResourceDictionary>

然后将其合并到您的 App.xaml 中。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ImageDictionary.xaml"/>
            .
            .
            .
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • 对我来说,设计师仍在抱怨绿色波浪线,“BitmapImage 必须将 isFrozen 设置为 false 才能修改”。但是没有运行时错误。您也可以尝试使用图像作为静态资源并设置 x:Shared="False"。像 一样引用它
  • 不工作。还是BitmapImage must have isFrozen set to false to modify.
  • 刚刚用一个新项目对其进行了测试。工作正常。在此处创建示例项目:github.com/RadiatorTwo/RessourceDictionary
猜你喜欢
  • 2021-01-14
  • 2012-06-09
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多