Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a resource to WPF application causes build error

Tags:

c#

wpf

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:

  1. 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; }
        }
    }
    
  2. 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>
    
  3. Attempt to build. Error "The tag 'MyDataClass' does not exist in XML namespace 'clr-namespace:XAMLBuildErrorExample'. Line 11 Position 10.":

    enter image description here

  4. Comment out the resource. Build succeeds:

    enter image description here

  5. Uncomment resource. Build succeeds whereas it failed before:

    enter image description here

  6. Any subsequent cleaning of the solution makes building impossible because of the error in the first image.

like image 228
IvenBach Avatar asked Apr 02 '19 20:04

IvenBach


1 Answers

It appears the problem is tied to initial computer.

Tested on another work station VS Community 2017 version 15.9.11 and build was successful without any issues. Build>Clean>Build without issues.

like image 126
IvenBach Avatar answered Oct 15 '22 21:10

IvenBach