【发布时间】: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 的堆栈跟踪?
-
如果您将 BitmapImage 直接放入 Application.Resources,而不使用 MergedDictionaries,则它可以工作。这是绝对必要的吗?
-
@Clemens,我刚刚更新了问题以包含我的完整 App.xaml。因为我使用的是 MahApps,所以我认为我必须使用合并字典。我对 WPF 中的资源还不是很熟悉,有什么我可以更改的,以便解决这个问题吗?
-
只需将其移至 MainWindow 的 XAML 中的 Window.Resources 即可。