试图在Window上添加一个图像抛出'BitmapImage必须将IsFrozen设置为false才能修改。'

时间:2017-01-04 17:22:08

标签: wpf xaml

的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中使用它会很棒。

1 个答案:

答案 0 :(得分:6)

这个问题现在已经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>