WPF prompts "an exception is thrown when the value is provided on System. Windows. Markup. StaticResourceHolder,
When writing a program, the system keeps reporting the error shown in the question, prompting that a defined static resource cannot be found. Baidu realized the order of resource definition only after a while.
App. xaml is defined as follows:
<Application x:Class="WpfApp2.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp2" StartupUri="FrmMain.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"> <Application.Resources> <ResourceDictionary> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:WpfApp2.ViewModel" /> <LinearGradientBrush x:Key="MainBrush" EndPoint="1,1" StartPoint="0,0"> <GradientStop Color="#242424" Offset="0"/> <GradientStop Color="#101010" Offset="1"/> </LinearGradientBrush> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/WpfApp2;component/Dictionary/Templete.xaml"/> <ResourceDictionary Source="/WpfApp2;component/Dictionary/Style.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources></Application>
As shown above, two resource dictionaries are defined: Templete. xaml and Style. xaml. The error occurs because Templete. xaml uses the Style defined in Style. xaml, but Style. xaml is defined after Templete, causing an exception. The solution is
Change the definition order of the resource dictionary file.
Conclusion: 1. The resource defined after the resource is used will cause an exception.
2. generally, it is best to place the paint brush, color, and defined local class at the beginning of the resource file, such as the MainBrush gradient Paint Brush defined in this example. If it is defined after the resource file is used, the exception shown in the title is also thrown.