I'm working through the book Head First C# and consistently have issues when adding resources to a window. This is a 100% repeatable error on any new WPF application I create when adding a new resource. The only way around this is to comment out the resource, build, and uncomment, as detailed in MVCE below. Images are included as proof this isn't a what-if or theoretical scenario.
What are the proper steps to add a resource file and use it within a WPF project?
I'm using Visual Studio Community 2017: Version 15.9.9 Target framework: .NET Framework 4.6.1
MVCE:
Create a new WPF application. Add a class:
//MyDataClass.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XAMLBuildErrorExample { class MyDataClass { public string Foo { get; set; } } }Within MainWindow.xaml add a resource
<Window x:Class="XAMLBuildErrorExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:XAMLBuildErrorExample" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:MyDataClass x:Key="exampleResource" /> </Window.Resources> <Grid> </Grid> </Window>Attempt to build. Error "The tag 'MyDataClass' does not exist in XML namespace 'clr-namespace:XAMLBuildErrorExample'. Line 11 Position 10.":
Comment out the resource. Build succeeds:
Uncomment resource. Build succeeds whereas it failed before:
Any subsequent cleaning of the solution makes building impossible because of the error in the first image.



MyDataClassandMainWindoware both in same assembly?If not,add “;assembly=right assembly name” to the namespace declaration.;assembly=XAMLBuildErrorExamplebut still couldn't build.