The name <...> does not exist in the namespace <...> - c#

I found myself stuck in this little problem that seems to have no solution at all. I'm trying to set the DataContext to a Window in WPF Project that looks like this:
The XAML file:
<Window x:Class="CSB.Tasks.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:CSB.Tasks"
xmlns:vm="clr-namespace:CSB.Tasks.ViewModels.WPF" <!-- This is what i need -->
mc:Ignorable="d"
Title="MainWindow"
Height="350"
Width="525">
<WindowChrome.WindowChrome>
<WindowChrome ResizeBorderThickness="{Binding ResizeBorderThickness}"
GlassFrameThickness="0"
CornerRadius="{Binding CornerRadius}"/>
</WindowChrome.WindowChrome>
<StackPanel Margin="5">
</StackPanel>
I want to set WindowViewModel as the ViewModel of the Window, but VS doesn't seem to find the folders where the class is contained. So, when I try to add the Window.DataContext like:
<Window.DataContext>
<vm:WindowViewModel/>
</Window.DataContext>
VS obviously tells me that the class does not exist.
I've been searching for similar questions on SO and I found plenty of them, but no one actually helped me. I already tried restarting VS, cleaning and rebuilding the project, compiling on a specific target platform (now it's set to Any CPU), moving the ViewModel in the root folder and then moving it back, absolutely no changes.
Does anyone know what could the cause be?
Thank you in advance for the help.

I actually managed to add the DataContext to the MainWindow XAML. The namespace of the ViewModel was set to CSB.Tasks in order to access it globally, but even using the local xmlns I couldn't be able to reference it. I had to change the namespace of the ViewModel according to its actual path in the project folder, so:
namespace CSB.Tasks.ViewModels.WPF
{
public class WindowViewModel : BaseViewModel
{
...
}
}
In order to set the xmlns:vm and to use it in the DataContext declaration. Then I switched the ViewModel namespace back to CSB.Tasks and recompiled the project and for some reason in the XAML editor I could be able to access WindowViewModel from the xmlns:local.
It's not very clear to me if this is a bug or not.
Thank you all for the help!

Related

DataContext class not found in XAML Designer (while everything looks fine)

I am trying to set my datacontext in the XAML file via
<Window x:Class="LocationScout.SettingsDeleteWindow"
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:ViewModel="clr-namespace:LocationScout.ViewModel"
mc:Ignorable="d"
Title="Delete" Height="315" Width="350"
WindowStartupLocation="CenterScreen">
<Window.DataContext>
<ViewModel:SettingsDeleteDisplayItem/>
</Window.DataContext>
The XAML editor, however, shows the error "The name "SettingsDeleteDisplayItem" does not exist in the namespace "clr-namespace:LocationScout.ViewModel".
The view model class looks fine for me:
namespace LocationScout.ViewModel
{
public class SettingsDeleteDisplayItem : BaseObservableObject
{
private long _countryAreaCountToDelete;
public long CountryAreaCountToDelete
{
get => _countryAreaCountToDelete;
set
{
_countryAreaCountToDelete = value;
OnPropertyChanged();
}
}
}
}
Building the solution works fine without error. Any idea? Many thanks.
As advised by #RobertHarvey in a comment, I changed to lowercase and restarted Visual Studio:
try changing ViewModel to lower case, as in xmlns:viewModel.
The reason this might have been an issue is because ViewModel (in
upper case) appears in your namespace declarations.
Now it works.
Just rebuilding the solution did not help (I tried that earlier).

VS Designer not working when adding a user control from another project

I am creating a universal app in Visual Studio 2015. My universal app has a reference to a universal library called UIComponents.
In UIComponents I created a user control:
namespace MyProj.UIComponents {
public sealed partial class MyControl : UserControl
{
public MyControl()
{
this.InitializeComponent();
}
}
}
With the following xaml:
<UserControl
x:Class="MyProj.UIComponents.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProj.UIComponents"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Rectangle Fill="White" HorizontalAlignment="Left" Height="280" Stroke="Black" VerticalAlignment="Top" Width="380" Margin="10,10,0,0"/>
<TextBox x:Name="textBox" Margin="20,20,20,20" TextWrapping="Wrap" Text="TextBox"/>
</Grid>
</UserControl>
Inside my app project, which references UIComponents, I do this:
<Page
x:Class="MyProj.App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProj.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:MyProj.UIComponents"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ui:MyControl></ui:MyControl>
</Grid>
</Page>
But when I try to get the designer display the page I get:
The error list shows this:
The name "MyControl" does not exist in the namespace
"using:MyProj.UIComponents".
Funny thing is that the whole solution builds just fine, but the designer is not collaborating.
Attempt using clr-namespace
There are similar questions about this in WPF, so not strictly universal apps, and they are marked as solved on answers where the solution was to use:
xmlns:ui="clr-namespace:MyProj.UIComponents"
Bu that does not work:
Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace
'MyProj.UIComponents' that could not be found.
The error list shows this:
The name "MyControl" does not exist in the namespace "using:MyProj.UIComponents".
In my experience, this could probably be caused by
The UIComponents Library didn't got built.
After building the Library project, VS will add the following codes into YourProject.proj file, which will be detected by VS designer:
<ItemGroup>//The following lines will be added.
<Page Include="MyControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
So, please try building your library project and rereference it in your main project.After reloading, designer should load the contents correctly.
Notes: The architecture(x64,x86) when building your library should be identical to current architecture. (e.g. when you build your library with x86. Designer can't load correctly, when your current architure is x64).
The namespace is wrong, which seems not the main cause here.

The name "ImageViewModel" does not exist in the namespace "clr-namespace:AdminControlCenter.ViewModel"

As the titel say i have the problem that Xaml don´t now my clr namespace, I read the other posts and tried their solution without any success, I rebuild, Start as Admin tried the build conviguration etc.
My Project looks so:
My XAML:
<Controls:MetroWindow x:Class="AdminControlCenter.View.MainView"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:AdminControlCenter.Properties;assembly=AdminControlCenter"
xmlns:local="clr-namespace:AdminControlCenter;assembly=AdminControlCenter"
xmlns:vm="clr-namespace:AdminControlCenter.ViewModel;assembly=AdminControlCenter"
Title="MainWindow" Height="400" Width="600">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White">
<Grid.DataContext>
<vm:ImageViewModel x:Name="imageViewModel"/>
</Grid.DataContext>
And the Error:
And the ImageViedModel:
namespace AdminControlCenter.ViewModel
{
public class ImageViewModel : ViewModel
{
public ImageViewModel()
{
}
.....
The classes are public and show up at the autoformat, when I type for example
"vm:" It also go to the Definition of the Class by using F12.
When I use some MVVM tutorial tests, they are working. But when I try in my Project all ViewModels are not found.
Why doesen´t found the XAMl my ViewModels ?
Edit:
I tried every variation of the assemblies with and without, I also try to delete the suo and try every build configuration, and I restart VS as Admin etc. I also make a new Project with another name and rewrite all classes (Not Copied them) also without any success
I get this error too sometimes, but I can build/deploy anyway.. I think it's a Visual Studio bug.. Are you able to deploy your solution?
Maybe you can try to copy Model and ViewModel's content somewhere else , delete from the project these two files and recreate the files again.. Or.. Do you know how to use the ViewModelLocator class? Since I started use it I never seen that issue again
See here for details about ViewModelLocator: link

How to use view models for windows inside a DLL

I'm new to WPF. Here is xaml defining a window defined inside a DLL:
<Window x:Class="MyNamespace.MyClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d"
xmlns:attachedProperties="clr-namespace:MyNamespace.AttachedProperties"
xmlns:viewModels="clr-namespace:MyNamespace.ViewModels"
DataContext="{Binding Source={StaticResource VmLocator}}"
Title="{Binding MyVm.MyTitle, Mode=OneTime}" Height="300" Width="460">
<Window.Resources>
<viewModels:ViewModelLocatorTestSteps x:Key="VmLocator" d:IsDataSource="True" />
</Window.Resources>
When the client code constructs this window object, this exception is thrown:
Cannot find resource named 'VmLocator'
How do I define the resource earlier so that it exists when it is needed? I'd also prefer the solution enable Intellisense to work. This is my first attempt at a window defined inside a DLL.
Using Visual Studio 2013.
If you want the Window to create its own DataContext, you can just stick that in the constructor in the code-behind, and avoid the necessity of making your VmLocator a resource. The resources of a WPF control (including a Window) are available to children of that control.
just:
public MyNamespace()
{
InitializeComponents();
this.DataContext = new VmLocator();
}
If you really want to make your DataContext a resource, you could create an application-level resource and reference that.
Also - 'MyNamespace' is a very confusing name for a class :)

How could I access wpf window resoures

I have this xaml
<mui:ModernWindow x:Uid="mui:ModernWindow_1" x:Class="App1.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ContentSource="/Window1.xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized" MenuLinkGroups="{Binding menuLinkGroups}">
<mui:ModernWindow.Resources>
<sys:String x:Key="ApplicationName">Bla Bla</sys:String>
</mui:ModernWindow.Resources>
<Grid Style="{StaticResource ContentRoot}" Name="mainGrid">
</Grid>
</mui:ModernWindow>
I need to reference current window resources, so I used this:
object obj = this.Resources["ApplicationName"];
But this.Resources doesn't have any resource! so obj is always null. How could I reference this window resources?
Assume that this is a FrameworkElement, like a Window, a Grid, a Button or something like that.
object obj = this.TryFindResource("ApplicationName");
Assuming this is a control...
var parent = Window.GetWindow(this)
Will get the window the control is currently on, you should then be able to access the resources like you already did
parent.Resources["ApplicationName"];
you can use below mentioned code
var MainGrid=Application.Current.FindResource("strApp")
or
this.FindResource("ApplicationName")
Thanks all, I find the solution (I forget to update the localized dll).
I cleaned and rebuilt solution, used locbaml.exe again to generate new localized dll.
You should bind in the XAML and not in the code behind.
"res" is the namespace where the resources file is located.
In your example the namespace alias is "local":
xmlns:local="clr-namespace:Project.Resources"
So your code should look like:
<Page Title ="{x:Static local:localStrings.step1Description}" />
Where:
"local" is the namespace alias where the resources file is located.
"localStrings" is the name of the resource file.
"step1Description" is an entry in the resource file.

Categories