71

I was working on creating some markup extensions and started to get very weird VS behaviours. I have extracted and pinpointed the issue in the separate solution. Problem is that VS can't create a CLR object in XAML.

Here it is:

View:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication4="clr-namespace:WpfApplication4">
    <Window.Resources>
        <wpfApplication4:Dog x:Key="doggy" />
    </Window.Resources>
    <Grid />
</Window>

Code behind:

using System.Windows;

namespace WpfApplication4
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

Dog class:

namespace WpfApplication4
{
    public class Dog
    {

    }
}

App.Xaml (no code in App.Xaml.cs):

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

Project settings

Exception I am getting:

Error   1   The name "Dog" does not exist in the namespace "clr-namespace:WpfApplication4". \\hopr1\folders$\vxk\Documents\Visual Studio 2012\Projects\WpfApplication4\MainWindow.xaml  6   9   WpfApplication4

I am able to run solution, but designer fails with "Invalid Markup" error Any ideas?

Edit

I am running VS 2012 Update 2 The same solution work in VS 2012 Update 1

Vitalij
  • 4,587
  • 9
  • 42
  • 65
  • 5
    This is going to sound extremely strange - I noticed you're running the app on a network share? \\hopr1\folders$\vxk\Documents Can you try the exact same thing on a local drive like C:\ - the reason I ask is that I've seen very irrational behavior in VS when storing my solution on a network location – GrahamMc Apr 25 '13 at 14:16
  • That works, well spotted! Add this as an answer I will mark it as a correct answer! – Vitalij Apr 25 '13 at 14:47
  • Glad it worked - if you manage to get it working on the network share somehow, please "share" your workaround (pun intended :) – GrahamMc Apr 25 '13 at 14:57
  • I'm seeing the same problem, but am not using a network share. I developed a ValueConvertor locally to my project and it all worked fine, then I moved this to our central library and attempted to use it from there, only to start getting the error described by the OP. The XAML designer insists that it can't find the class, yet the app compiles and builds. Really annoying, as I can not longer preview my XAML. – belugabob Jul 17 '13 at 09:24
  • 2
    OK - just fixed it. The DLL being referenced was marked as being 'untrusted', so I had to fix that (http://wpflocalizeextension.codeplex.com/discussions/399736), then I had to close the solution, remove the 'suo' file, then re-open the project. – belugabob Jul 17 '13 at 10:27
  • Thanks belugabob - I guess I should consider changing my answer to indicate that the issue happens because of "trust" issues, not directly because of running on a share; the issue just happens there because UNC shares are not fully trusted by default. – GrahamMc Jan 13 '14 at 17:51
  • 1
    Rebuilding the project/solution got rid of the error for me. – Zoomzoom Nov 08 '14 at 03:13

21 Answers21

121

For anyone coming across this now, BEFORE YOU DO ANYTHING ELSE... if you're certain your classes/namespaces are correct and rebuilding has not solved your problem:

Try restarting Visual Studio

That's it!

This appears to be a bug with Visual Studio 2012 (also appears to affect all other versions that support XAML development)


Update: If restarting Visual Studio doesn't work, reboot the entire PC.

Update: As mentioned in comments by @Dunk, if restarting Visual Studio doesn't work, try deleting the .suo file

Jordan
  • 5,085
  • 7
  • 34
  • 50
  • 4
    Thanks for this, you saved me a couple hours of searching for an answer. This happened to me in VS2013 Update 2 and restarting fixed it. – Apogee May 27 '14 at 07:29
  • This is happening to me now. I'm using VS2013 Update 2 as well. I tried restarting but nothing changed. PS: It's a Windows Phone 8 project. – wm1sr Aug 20 '14 at 13:30
  • Hey @Apogee and wm1sr should I add this to a comment on the accepted answer, or edit it? Do you have any idea what might be causing this in 2012/2013? – GrahamMc Aug 22 '14 at 12:24
  • 1
    @GrahamMc No idea at all I'm afraid. I've only seen this issue once, and this fixed it, never seen the problem since. This was while running locally btw, not on a network share. – Apogee Aug 22 '14 at 15:45
  • 11
    I exited Visual Studio, deleted the .suo files (per another user's suggestion) and restarted visual studio and that resolved this issue. I'm not sure if restarting or deleting the .suo files was the solution but it is simple enough to do both. – Dunk Sep 03 '14 at 13:10
  • 3
    Restarted VS few times. Didn't helped. Deletion of .suo file worked. – Branislav Lazic Mar 25 '15 at 23:54
  • 1
    Thanks. Also applies to VS2015RC. – Tedd Hansen May 16 '15 at 13:20
  • 3
    Also applies to VS2013 Update 4. Deletion of .suo file worked. – liang Jun 13 '15 at 06:28
  • VS 2013. Restart didn't fix it. Deleting the. suo file, then restarting did however. Thank you!!!! – Laki Politis Apr 14 '16 at 16:01
  • 1
    Restart didn't fix it. Deleting the .suo file didn't fix it. Restarting pc didn't fix it. It's been 3 years now - @Microsoft you must be kidding us. – a'' Jun 10 '16 at 14:41
  • @erem I agree it should have been fixed by now - but in the absence of a permanent solution .. I've found my suggestion to be the most common resolution, but it doesn't work 100% of the time depending on the nature of the underlying problem - might be worth checking out some of the other solutions here – Jordan Jun 10 '16 at 14:46
  • Make sure that your namespace is correct. It might happen that your main namespace is doubled when you type "Myproject.CustomNamespace" it leads to "Myproject.Myproject.CustomNamespace". If everything is correct for VSExpress2013 the issue got solved by BUILD->REBUILD. – Matthis Kohli Jul 29 '16 at 10:30
  • 1
    VS2015 Update 3; still there. VS restart; no. OS report; no. Can't find an `suo` file. – davidbitton Aug 30 '16 at 15:12
  • I am using VS 2015 and that tip still did the trick! I wonder whether Microsoft just does a copy / paste of the previsious versions source code if they make a new Visual Studio version... – Martin Oct 20 '16 at 11:20
  • Visual Studio 2015 Update 3. Restarting VS did not work, but rebooting the entire PC solved it for me. – steffan Nov 01 '16 at 11:18
  • @LukTar has suggested an edit that in VS2015, removing the hidden ".vs" directory should resolve this problem - please up-vote to confirm or add comments to provide additional information - at this point I am not going to approve this edit until additional verification is provided. – Jordan Jun 12 '17 at 11:09
  • I would hesitate to try what @LukTar suggested. The hidden .vs folder contains a .suo file you can delete. But mine also contains applicationhost.config, which has this comment in the IIS config section -- "Please make a backup of this file before making any changes to it." – mmesser314 Sep 23 '17 at 18:52
  • @mmesser314 are you sure that's the right place for that? I've been a bit disconnected from the .NET world for a little while now, but this strikes me as an odd place for your applicationhost.config file unless it is being copied from a folder outside of your .vs folder ... typically you wouldn't check these hidden files into source control, but maybe I'm missing something. – Jordan Sep 25 '17 at 21:58
  • I didn't put it there. I found it there. I guess the larger point is that I wouldn't blindly delete a folder without knowing more about what it contains. – mmesser314 Sep 26 '17 at 05:17
  • I also had this problem with VS 2017 and restarting Visual Studio solved the problem – Andy Braham Oct 06 '17 at 09:12
  • 1
    Running VS2017 (v15.3.4), and same problem, same solution. Seemed to happen when I changed Assembly name and/or Assembly Info. Restart of VS did the trick. – bobwki Oct 10 '17 at 16:50
  • Problem still present in Visual Studio Community 2019 v16.4.1 nothing except of restart of entire pc helped. – Berger Dec 13 '19 at 04:41
  • Also applies to VS 2019 (16.11.3). Restarting visual studio fixed it. Thank you! – Sinan Ceylan Sep 21 '21 at 00:02
49

Your solution is running on a network share. .Net (and Visual Studio) applications can run into permission / access issues when running on a network share.

Copy your solution to a local drive (with full trust) and you should be fine.

It is possible to get a network drive working with full trust - you can find answers for this on StackOverflow and other places - but in my experience I keep running into obstacles when I do this, so try to avoid it unless it's absolutely critical to the problem at hand.

E.g. this question gives instructions about how to do this:

Give FullTrust to UNC share for Visual Studio 2012 and .Net 4.0

I've only ever tried this with VS2010 so (as indicated in the link) you might have better joy with 2012.

Community
  • 1
  • 1
GrahamMc
  • 3,034
  • 2
  • 24
  • 29
  • 2
    Note that as per belugabobs comment on the question, this issue can happen in other places besides a network share - any location that doesn't have full trust. Someone might be able to shed some light on what exactly causes the problem, rather than just what the solution is – GrahamMc Jan 13 '14 at 17:53
  • 3
    Running Visual Studio in Parallels and storing your solution on your Mac's filesystem seems to cause this as well (Parallels uses network shares to access the OS X filesystem). Copying the solution into an absolute path on the virtual C drive solved it for me. – Mick MacCallum Nov 16 '15 at 23:05
32

I experienced the same issue, but my files are stored locally. My IValueConverter resides in a different assembly than the view using it. Even though VS2013 IntelliSense suggested the following, it wasn't working:

xmlns:conv="clr-namespace:MySharedAssembly.Converters"

After I explicitly added the assembly at the end, it worked:

xmlns:conv="clr-namespace:MySharedAssembly.Converters;assembly=MySharedAssembly"
Wouter
  • 760
  • 8
  • 13
  • 1
    Did everything, then did this last. I would try this first, then restart vs/delete .suo. then if that doesnt work, do the massive workaround. Thanks a ton @Wouter – TheNoob Jul 10 '15 at 17:20
12

I stuck on this error for hours. Assemblies and Namespaces were correct, Classes and references were correct too. Compile and run ok, only the designer had somehow problems with me. The only thing that worked

  1. for design purposes swtich from x64 to x86
  2. for run and debug switch back to x64

I'm using 3d party portable.library which I had only in x64 version.

Stefan Michev
  • 4,795
  • 3
  • 35
  • 30
  • Similar to what I did. I switched from x64 to Any CPU (and turned off Prefer 32-bit). I rebuilt, then was able to restore it to x64 profile and rebuilt without receiving that silly error! – Nicholas Miller Apr 14 '16 at 22:04
  • Tried restart but doesn't work. Switching to 32bit worked. – Syaiful Nizam Yahya Jul 14 '16 at 09:55
  • I had exactly the same issue. My project was referencing a 64bit DLL but was set to Any CPU. After setting the main project to x64 and recompiling, the problem was solved. – jettatore Sep 14 '17 at 10:57
11

Downloaded DLL solution

  1. Unblock the .dll file
    • Right click it and select properties
    • Unblock it in the "General" tab (see image)
  2. Ensure VS revalidates the XAML (perhaps by restarting it)

enter image description here

consideRatio
  • 1,091
  • 13
  • 19
  • 4
    Thanks, this worked for me when I also cleared the ShadowCache. %localappdata%\Microsoft\VisualStudio\14.0\Designer\ShadowCache – StevenGodin Feb 16 '16 at 18:42
  • Hell yes! You are the man! After spending almost an hour trying all other suggestions, yours finally did the trick. I'm on Windows 10, using VS2015. The control was inside a 3rd party dll. Had to close and re-open the solution for the fix to take effect. – Eternal21 Jan 19 '17 at 18:10
  • @DeadlyGhost1 Deleting everything in the ShadowCache folder finaly worked for me too! Consider turning your comment into an answer ;) – Tobi o' Bobi May 12 '17 at 15:44
  • VS2013 - I had to unblock the .xaml – SBF May 18 '17 at 13:08
6

It still happens in VS 2015. I took out the SomeConverter in the App.xaml:

<Application.Resources>
    <!--Value Converters-->
    <local:SomeConverter x:Key="mySomeConverter"/>

Strg-Shift-B
Put it back in - and it worked.

Andi Truman
  • 111
  • 1
  • 6
4

Before trying an extensive solution, try the following:

I had the exact same problem, I closed the Window/Form that was causing the error and then ran the project,

The error seemed to clear once the project had ran successfully and hasn't come back up.

Hopefully this helps anyone looking for a quick solution.

Connor Mcgrann
  • 322
  • 2
  • 15
  • I've just discovered this as well. I had some .xaml files open. In my case, that resulted in 23 errors. Once I closed the files and rebuilt the solution (and I made no changes at all), it built fine. There's something wrong with the Visual Studio 2019, still, about this. – Rod Aug 21 '19 at 16:02
3

Create a symbolic link to the network share on your local drive.

Go to commandline and type mklink /D C:\LOCALFOLDER \YOURNETWORKPATH

Then open the projects from your local folder and all problems go away. Now all files will still be on your network share.:)

Tom
  • 31
  • 1
  • This never worked for me, I still had problems until I moved everything to a local disk. – Duncan Groenewald Sep 05 '14 at 03:46
  • This worked fine for me, where the project is located on my Mac's home folder and I am using it in a Parallels VM via PSF share. Excellent idea, thanks! – Michael Oct 14 '14 at 09:19
3

I was starting a new project and having this issue. None of the solutions listed here worked for me, including removing the suo file, unloading/reloading the project, restarting VS, etc.

What worked for me was, because this was a new project, I had not yet built it. I removed the Window.DataContext element to the clipboard, built the project once (shift-ctrl-b), then re-added the element and it functioned immediately.

Jim Yarbro
  • 2,063
  • 15
  • 21
2

I got the same error in Visual Studio 2015 and managed to get the designer working by unchecking the "Disable Project Code Button".

Screenshot to Designer Button

ChristianMurschall
  • 1,621
  • 15
  • 26
1

I also have a project on a network share and this error unexpectedly showed up. I tried all the suggestions above including copying the project to a local disk, cleaning, rebuilding and opening and closing VS. None of these solved the problem.

What worked for me was simply deleting the namespace reference to the viewmodels folder (xlmns:vm="clr-namespace:Myproj.ViewModel").

I added the type to my xaml (DataTemplate DataType="{x:Type vm:myviewmodel}"). Visual studio then detected the namespace was missing and I clicked on the prompt to add the namespace.

Haim Katz
  • 455
  • 3
  • 15
0

This has been annoying me for years 2008, 10, 12, 13.

Whenever this happens (and yes, I am working on a network share - I can't avoid it), I close VS, rename the FOLDER and reopen the project. 9 times out of 10, this works. For a while.

Muckers Mate
  • 399
  • 8
  • 23
  • I had this happen to me, but *NOT* on a network share, just a local folder and found that the only solution was to close VS (2013) and rename the solution folder to something else. Alternatively, I could have just relocated the dll that was not loading properly and update the reference. – pjs May 28 '15 at 18:17
0

For anyone else stuck.

What worked for me was changing the namespace alias from local, to anything else.

xmlns:local="clr-namespace:ExampleNameSpace.Folder" />

<Grid>
    <StackPanel>
        <local:ReferencedUserControl />
    </StackPanel>
</Grid>

to

xmlns:blah="clr-namespace:ExampleNameSpace.Folder" />

<Grid>
    <StackPanel>
        <blah:ReferencedUserControl />
    </StackPanel>
</Grid>

Hope this helps!

Ben Winding
  • 10,208
  • 4
  • 80
  • 67
0

Had the same issue. What fixed it for me was when I realized the class in question was marked as internal:

internal class MyClass
{
}

After changing it to public, the designer was able to compile the XAML properly.

James Ko
  • 32,215
  • 30
  • 128
  • 239
0

What worked for me is to change the MOVE to XCOPY in Post Build in the project properties and then re-build the project. The designer might want the dll in the project output folder. I'm using vs 2015

Sony
  • 7,136
  • 5
  • 45
  • 68
  • This could make sense.. Do you have any further info on this? – Jordan Apr 08 '16 at 12:16
  • @Martin, no man :-( The above approach is one and the only one that worked for me out of the many approaches I've tried. – Sony Apr 08 '16 at 12:56
0

After restarting Visual Studio I got an IntelliSense error that pointed me in the right direction.

Because 'Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.Metadata.ReflectionTypeNode' is implemented in the same assembly, you must set the x:Name attribute rather than the Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.Metadata.ReflectionPropertyNode attribute.

So I changed this:

<local:MyView Name="test"/>

To this:

<local:MyView x:Name="test"/>

And then it worked. So, that gives us what? 42 possible causes? Unreal...

l33t
  • 18,692
  • 16
  • 103
  • 180
0

Declaring it in a Resource Dictionary worked for me.

<Window.Resources>
    <c:IsNullConverter x:Key="IsNullConverter" />
</Window.Resources>

The problem appears to be that the parser shits itself when it sees a markup extension like Converter={c:IsNullConverter}}", but is fine with: Converter={StaticResource IsNullConverter}. I think maybe that's where the problem is.

Windows 10, VS 2013

Cool Blue
  • 6,438
  • 6
  • 29
  • 68
0

I had 3 errors in the project and was focused on this error concerning the ObjectDataProvider. I found out that this error cannot be resolved if the project can't build due to other errors. I had a couple other event handlers whose code had been deleted. I needed to also delete the code that tried to link the handlers to the controls. Then the project was able to build and see that the class I was trying to reference from ObjectDataProvider was available.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
0

The name “XYZ” does not exist in the namespace “clr-namespace:ABC”

SOLVED!!

check whether the function you want to invoke / the class is present in the namespace

Correct Example :

.XAML file

          <Window x:Class="objectbinding.MainWindow"
           <!--your xmlns:x .......
           namespace1 is the project name-->
           xmlns:m="clr-namespace:namespace1"
            Title="MainWindow" Height="350" Width="525">
    <Grid>
 <StackPanel Orientation="Vertical">
            <StackPanel.Resources>
                <ObjectDataProvider ObjectType="{x:Type m:StringData}"
                                    x:Key="anyname" MethodName="GetStrings"/>

            </StackPanel.Resources>

       ItemsSource="{Binding Source={StaticResource Runni}}" />
        </StackPanel>
    </Grid>

//StringData is the class name in xaml.cs and GetString is the Function

.XAML.CS file

 namespace namespace1
    {

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

        }
// the class StringData is defined in namespace namespace 1
        public class StringData
        {
            ObservableCollection<String> lst = new ObservableCollection<String>();

            public StringData()
            {
                lst.Add("Abhishek");
                lst.Add("Abhijit");
                lst.Add("Kunal");
                lst.Add("Sheo");
            }
            public ObservableCollection<String> GetStrings()
            {
                return lst;
            }
        }
    }

Wrong example:

.XAML File

  <Window x:Class="objectbinding.MainWindow"
              <!--your xmlns:x .......
           namespace1 is the project name-->
           xmlns:m="clr-namespace:namespace1"
            Title="MainWindow" Height="350" Width="525">
    <Grid>
 <StackPanel Orientation="Vertical">
            <StackPanel.Resources>
                <ObjectDataProvider ObjectType="{x:Type m:StringData}"
                                    x:Key="anyname" MethodName="GetStrings"/>

            </StackPanel.Resources>

       ItemsSource="{Binding Source={StaticResource Runni}}" />
        </StackPanel>
    </Grid>

.XAML.CS

namespace namespace1
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
//The StringData is defined in the class mainWindow not in namespace namespace1
        public class StringData
        {
            ObservableCollection<String> lst = new ObservableCollection<String>();

            public StringData()
            {
                lst.Add("Abhishek");
                lst.Add("Abhijit");
                lst.Add("Kunal");
                lst.Add("Sheo");
            }
            public ObservableCollection<String> GetStrings()
            {
                return lst;
            }
        }
    }

}
Runni Kumari
  • 398
  • 3
  • 6
0

In my case this error was caused by having signed assemblies with 'delay sign only' enabled. The solution was to run the command sn -Vr * from the developer console (as administrator). This registers the assembly for verification skipping. And then restart Visual Studio.

Scroog1
  • 3,539
  • 21
  • 26
-2

This may be due to the Release configuration. Designer needs to be used from Debug configuration.

Denis535
  • 3,407
  • 4
  • 25
  • 36