4

I am trying out a simple WPF application. The XAML code is:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApplication1;assembly=WpfApplication1"
        Title="My First WPF Demo" Height="350" Width="525">
    <Window.Resources>
        <sys:Int32 x:Key="i">10</sys:Int32>
        <local:Employee x:Key="emp2"></local:Employee> --> THIS LINE
    </Window.Resources>
    <StackPanel>
        <TextBox x:Name="txtName" FontSize="18" Margin="20"></TextBox>
        <Button x:Name="btnClickMe" FontSize="18" Margin="20" 
                Click="btnClickMe_Click">Click Me</Button>
        <TextBlock x:Name="lblName" FontSize="18" Margin="20"></TextBlock>
        <Label x:Name="lblEmpInfo" FontSize="18" Margin="20"></Label>
        <Label x:Name="lblEmpInfo2" FontSize="18" Margin="20"></Label>
    </StackPanel>
</Window>

I have a class Employee that is as follows in the same project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

When i try to build my project i get the error:

Error 1 The tag 'Employee' does not exist in XML namespace 'clr-namespace:WpfApplication1;assembly=WpfApplication1'. Line 9 Position 10.

Tharif
  • 13,794
  • 9
  • 55
  • 77
AnishaJain
  • 233
  • 1
  • 2
  • 13

5 Answers5

0

use this way in your case: xmlns:local="clr-namespace:WpfApplication"

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
0

Haven't really checked, but... Have you tried removing the assembly=WpfApplication1 part? Leaving only xmlns:local="clr-namespace:WpfApplication1"

almulo
  • 4,918
  • 1
  • 20
  • 29
  • When i removed the assembly value, i get the error `The name 'Employee' doesn't exist in the namespace 'clr-namespace:WpfApplication1'` – AnishaJain Jul 27 '15 at 11:34
  • 1
    Weird... The error makes no sense at all. Try removing that XAML line, re-building, and adding it again :/ – almulo Jul 27 '15 at 12:36
0

I tested your code (Windows 7, Visual Studio 2012 Update 4) and ran into the same situation. After trying some ideas (some months ago I think had the same issue) the following 'workaround' worked for me:

  1. Remove the 'error line' and the namespace from your MainWindow.xaml (but keep your Employee.cs in the Project)
  2. Build the solution! (I think Visual Studio needs to build your Employee.cs before you can instantiate the object in XAML)
  3. Add your Employee and the namespace without the explicit assembly

I'm looking forward hearing whether it fixed the problem

SSchuette
  • 609
  • 7
  • 15
  • Found a possible Duplicate at http://stackoverflow.com/questions/16080680/the-name-initializecomponent-does-not-exist-in-the-current-context-strange-b – SSchuette Jul 28 '15 at 10:54
0

This is a old question but what worked what this:

I change the tag from

<local:MyClass Name="TagName"></local:MyClass>

To:

<local:MyClass Name="TagName"/>

But the following error was thrown:

Because 'MyClass' is implemented in the same assembly, you must set the x:Name attribute rather than the Name attribute. 

So i changed to and worked:

<local:MyClass x:Name="TagName"/>

And then tried and worked too:

 <local:MyClass x:Name="TagName"></local:MyClass>
Misters
  • 1,337
  • 2
  • 16
  • 29
0

I had a similar issue. Tried to rebuild, clean, restart Visual Studio, rebuild again, but nothing solved the issue. I also removed the assembly=... part, it didn't help.

Then I tried moving the project to my hard disk (it was originally in my documents folder, which is located on a mapped network drive), and suddenly the problem was gone! Don't know why, but could be worth a try if anyone else is still having this problem.

Henrik Berg
  • 519
  • 5
  • 21