Namespace error when adding viewmodel - c#

I come to you with a very strange problem.
At the first look it's an easy thing but trust me it's make me crazy !
I just want to specify wich file I want to associate to xaml page in DataContext, I already did it on other xaml page so I don't understand why !
<UserControl x:Class="FinalMediaPlayer.Onglet.Musique.Home.HomeMusique"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home"
xmlns:tools="clr-namespace:FinalMediaPlayer.Tools"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<viewmodel:HomeViewModelMusique></viewmodel:HomeViewModelMusique>(error here)
</UserControl.DataContext>
The name "HomeViewModelMusique" doesn't exist "clr-namespace:FinalMediaPlayer.Onglet.Musique.Home".
namespace FinalMediaPlayer.Onglet.Musique.Home
{
public class HomeViewModelMusique : NotifyPropertyChanged
{
As you can see I put the path in viewmodel but apparently the file don't exist in the namespace ...
Thanks by advance guys, Im gonna suicide very soon...

Are all the namespaces in the same assembly? If they are not you will need to specify the assembly in the namespace declaration.
E.g:
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home;assembly=AssemblyName"

Check that the namespace is actually correct, then clean\rebuild your solution. Please consider also to use a tool like Resharper that will fix these problems automatically

Thanks for your fast answers guys !
I already try that :
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home;assembly=FinalMediaPlayer"
It doesn't work, "FinalMediaPlayer" is the name of my project, is there what is expected in the field assembly ?
I Get this on my AssemblyInfos.cs :
[assembly: AssemblyTitle("FinalMediaPlayer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FinalMediaPlayer")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
However Im gonna try to use Resharper but this is not a real solution :/.
If I find the solution I will post it.

Yes sorry guys I didn't answer that question, of course I did it.
Tryed tot clean/regenerate/generate. Whith no success.
Really I can't believe it, I have spend all my afternoon on this little stupid problem, maybe it's a bug coming from visual studio.
Im gonna try on my other computer later after work.
Again, Thanks to search with me.
namespace FinalMediaPlayer.Onglet.Musique.Home
{
public class HomeViewModelMusique : NotifyPropertyChanged
{

Related

"The name does not exist in the namespace error" in XAML

I know this is a recurring error but I can't seem to get around it.
Intellisense does recognize the name of my custom control and suggests to add the proper using: directives, but not only XAML designer doesn't find the name of the control but I can't get through compilation either.
The custom control is a public class defined as
namespace MyApp.CustomControls
{
public class CustomTextBox : TexBox
{
...
}
}
And in my MainPage.xaml
<Page ...
xmlns:customControls="using:MyApp.CustomControls">
...
<customControls:CustomTextBox/>
...
</Page>
This does not render in design nor compile.
This answer and the ones below are not working for me.
The error message:
Error XDG0008 The name "CustomTextBox" does not exist in the namespace "using:MyApp.CustomControls".
Your code should works well after you build the project, and it works well in my side using your above code. Try to clean your solution or delete the bin and obj folders in your project then rebuild your app again. Also try to restart your Visual Studio. If it still happens, you can provide a reproducible sample to help me look into this issue.
I've seen quite a lot solutions saying that you should rebuild the project, restart Visual Studio or restart the machine.
What worked for me was specifying the assembly in the namespace reference, that is:
xmlns:the_namespace="clr-namespace:the_namespace" - produces the above error.
xmlns:the_namespace="clr-namespace:the_namespace;assembly=the_assembly" - works well.
I got a version of this error in my embedded UserControl when I tried to use the Name property in my XAML instead of using x:Name. In other words, when my XAML code looked like this:
myUserControls="using:MyUserControls"
<myUserControls:GraphCanvas Name="GraphCanvas" />
I got an error that 'The name "GraphCanvas" does not exist in the namespace "using:MyUserControls"'. When I changed one line of code to this:
<myUserControls:GraphCanvas x:Name="GraphCanvas" />
Everything built just fine.
I'm dropping this solution here because it took me about a day and a half to figure out this problem and this was the only stackoverflow page I found when I searched the error string. Hopefully I will save someone else the hassle I went through.

How to use Microsoft converters

I have found reach set of converters in Microsoft.TeamFoundation.Controls.WPF.Converters, but i don't know how to use them in xaml. Particularly, i don't how to include this namespace into xaml. Maybe it is only allowed to use it in code?
List of converters
I tried
xmlns:conv="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls"
and then
<conv:NullToVisibleConverter x:Key="Null2VisConv"/>
but it can't find NullToVisibleConverter in conv.
First you need to target .NET 4.5, then add reference to Microsoft.TeamFoundation.Controls (should be in Assembilies -> Extensions) and then in XAML, as you did
<Window ...
xmlns:conv="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls">
<Window.Resources>
<conv:NullToVisibleConverter x:Key="Null2VisConv"/>
</Window.Resources>
<!-- ... -->
</Window>
Please make sure that have these pkgs installed.
You must add reference to dll in your project.
MSDN says:
You can find the assemblies in the client object model in Program Files\Microsoft Visual Studio 11.0\Common7\IDE under ReferenceAssemblies\v2.0, ReferenceAssemblies\v4.5, and PrivateAssemblies.
After thar you write in your xaml for example:
xmlns:converters="namespace for converters"
you can choose namespace for converters from the list of namespace you have on your pc

Using DesignData in WPF without including it into build

I have some Design time view models bound to d:DataContext like this:
<UserControl x:Class="MyProject.Views.CallRecordView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:MyProject.ViewModels.DesignTime"
d:DataContext="{d:DesignInstance Type=vm:MyDesignViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d vm">
I changed Build Action of every ViewModel from Compile to DesignDataWithDesignTimeCreatableTypes
and it's working on design time, but when I try to compile I get following errors:
Error 20519 The type or namespace name 'DesignTime' does not exist in the namespace 'MyProject.ViewModels' (are you missing an assembly reference?) ...\obj\Debug\Views\MyView.g.cs 13 35 MyProject
how can I prevent my sample data from being attached to an assembly? Or maybe this is not neccessary and the compiler wouldn't include those files into executable by default even on Compile setting?
// edit:
also - everything is working and compiling when I create an empty class into DesignTime namespace:
namespace MyProject.ViewModels.DesignTime {}
but can I do it without changing my namespace or providing an empty one for compile time?

Error when trying to use markup extension

I have a small window that I am trying to load when my application starts. Here is the (loose) XAML:
<ctrl:MainWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:Controls;assembly=Controls">
<Grid>
<ctrl:ConnectionStatusIndicator/>
<TextBlock Grid.Row="2" Text="{Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/>
</Grid>
</ctrl:MainWindow>
Notice the custom control called ConnectionStatusIndicator. The code for it is:
using System.Windows;
using System.Windows.Controls;
namespace Controls
{
public class ConnectionStatusIndicator : Control
{
public ConnectionStatusIndicator()
{
}
static ConnectionStatusIndicator()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ConnectionStatusIndicator),
new FrameworkPropertyMetadata(typeof(ConnectionStatusIndicator)));
IsConnectedProperty = DependencyProperty.Register("IsConnected", typeof(bool), typeof(ConnectionStatusIndicator), new FrameworkPropertyMetadata(false));
}
public bool IsConnected
{
set { SetValue(IsConnectedProperty, value); }
get { return (bool)GetValue(IsConnectedProperty); }
}
private static DependencyProperty IsConnectedProperty;
}
}
Now, here is where it gets weird (to me, at least). With the XAML as it appears above, my application will build and run just fine. However, if I remove the following line:
<ctrl:ConnectionStatusIndicator/>
or event move it one line down, I get the following error:
Additional information: 'Cannot create unknown type
'{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Resx'.'
Line number '13' and line position '33'.
What is really strange to me is that, if I replace ConnectionStatusIndicator with another custom control from the same assembly, I get the error. The other custom control is very similar, but has a few more properties.
Can anyone explain what is going on here?
The Resx markup extension belongs to the Infralution.Localization.Wpf namespace but also does something a bit hackish and attempts to register itself to the http://schemas.microsoft.com/winfx/2006/xaml/presentation xml namespace to allow developers to use it as {Resx ...} instead of having to declare the namespace in XAML and use the extension with a prefix {resxNs:Resx ...}.
I believe that if you clean up your solution and possible delete your *.sou file the project will build as expected, but a sure way to solve this will be to add an xmlns declaration for Infralution.Localization.Wpf and use the extension with the xmlns prefix:
<ctrl:MainWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:Controls;assembly=Controls"
xmlns:loc="clr-namespace:Infralution.Localization.Wpf;assembly=Infralution.Localization.Wpf">
<Grid>
<ctrl:ConnectionStatusIndicator/>
<TextBlock Grid.Row="2" Text="{loc:Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/>
</Grid>
</ctrl:MainWindow>
Also, for anyone interested, the "hack" is in these lines in the localization library:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Infralution.Localization.Wpf")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2007/xaml/presentation", "Infralution.Localization.Wpf")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2008/xaml/presentation", "Infralution.Localization.Wpf")]

WPF: XAML namespace

I have one assembly XAML registered in AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://schemas.mysite.es/wpf", "SWC.ViewModels")]
And In my namespace I hace these classes:
namespace SWC.ViewModels
{
public class MenuViewModel
: ObservableCollection<MenuViewModel>
{
public MenuViewModel()
: base()
{
}
}
}
If I use the namespace in a user control XAML,
<UserControl x:Class="SWC.UserControls.UserMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customMenu="http://schemas.mysite.es/wpf">
I can use the class in my XMLNS namespace,
<UserControl.Resources>
<customMenu:MenuViewModel x:Key="MenuItemsSource">
But, when I execute the application, the compiler said
The label 'MenuViewModel' dosen't exist in the namespace XML 'http://schemas.mysite.es/wpf'
All, can help me?? I'm crazy with this problem!!
Best regards,
I think you need to specify the assembly in which your customMenu controls exist. The assembly must also be referenced from the project (in the References section).
xmlns:customMenu="clr-namespace:customMenuNamespace;assembly=customMenuLibrary"
Otherwise I don't see how the compiler can find your implementation only through "http://schemas.mysite.es/wpf". What's at that address? The two microsoft schemas
http://schemas.microsoft.com/winfx/2006/xaml/presentation
http://http://schemas.microsoft.com/winfx/2006/xaml
work like some identifiers for the xaml compiler, there is nothing at those addresses.

Categories

Resources