尝试在 Window 上添加图像会抛出“BitmapImage must have IsFrozen set to false to modify”。

hya*_*kov 5 wpf xaml

应用程序.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>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml

<Image Source="{StaticResource Logo}" Style="{StaticResource LogoStyle}" />
Run Code Online (Sandbox Code Playgroud)

投掷

“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()
Run Code Online (Sandbox Code Playgroud)

内栈

   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)
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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

编辑 2/修复:

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

小智 9

这个问题已经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>
Run Code Online (Sandbox Code Playgroud)

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

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ImageDictionary.xaml"/>
            .
            .
            .
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)