How to assemble a Namespace in a WPF project - wpf

I'm learning WPF and trying to reproduce this DataTemplate example related to this article, writing the code-behind in VB (in the example is in C#). The first issue I'm facing is that I don't know how to assemble a Namespace created in the MainWindow.xaml.vb file. I read a lot of questions related here but I haven't been able to find a solution. I recompile the project and it run properly but the error is still there. I also read that could be a VS bug related with the architecture setting of the app (x64 x86).
MainWindow.xaml file:
<Window x:Class="DataTemplatingIntro.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:SDKSample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
MainWindow.xaml.vb file:
Imports System.Windows
Namespace DataTemplatingIntro
Public Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace
Severity Code Description Project File Line Status deleted
Error XLS0414 The type 'DataTemplatingIntro.MainWindow' was not found. Verify that a reference to an assembly is not missing and that all referenced assemblies have been compiled. SDKSample MainWindow.xaml
I tried to add the name of the assembly:
xmlns:myns="clr-namespace:DataTemplatingIntro;assembly=MyLibAssembly
I tried to add as local too:
xmlns:local="clr-namespace:SDKSample:DataTemplatingIntro"
But nothing works. How can I solve this annoying VS issue?
Thanks in advance

Related

Why does ServiceLocator.Current.GetInstance cause UserControls to not work at design-time?

I'm having an issue where if I call ServiceLocator.Current.GetInstance(type) anywhere in my project, it causes UserControls throughout my project to stop instantiating within other controls at design-time in Visual Studio. These controls work fine at run-time however.
It is reproducible by creating a simple WpfApplication with the following 3 classes. The first build will succeed, and the designer will show fine. If you rebuild, however, it throws the error and the designer stops instantiating the UserControls within other controls. Because of this behavior, it seems like a Visual Studio bug, but I'm not sure.
When uncommented, the #GetInstance(Type type) function of ServiceLocator causes an Error in the error list Cannot locate resource 'controls/test.xaml and the designer shows an error Cannot create an instance of "Test" where the Test UserControl should be. When commented, there are no errors.
Test xaml class.
<UserControl x:Class="XamlTest.Controls.Test"
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"
mc:Ignorable="d">
<Grid>
<Label Content="Test"/>
</Grid>
</UserControl>
MyView xaml class. (uses Test)
<UserControl x:Class="XamlTest.Views.MyView"
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:mycontrols="clr-namespace:XamlTest.Controls"
mc:Ignorable="d">
<Grid>
<mycontrols:Test/>
</Grid>
</UserControl>
A class unrelated to the UserControls that uses ServiceLocator.Current.GetInstance(type);.
using Microsoft.Practices.ServiceLocation;
namespace XamlTest.SomeClasses
{
class SomeClass
{
public void SomeFunction()
{
// Causes Test xaml class to not instantiate at design time within MyView.
// Does not matter what parameters you give the function.
// Notice this class isn't even associated with any other class,
// and is never called, but it still causes problems.
ServiceLocator.Current.GetInstance(null);
}
}
}

DesignTime only error when trying to load an xml to a dataset

I've created a simple WPFApplication project that produce this error
This is the exact error:
Could not find file
'C:\Users\Andrei\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\dj2szrx3.5nm\e412xqna.uj1\Locality.xml'. E:\BugDemo2\BugDemo2\MainWindow.xaml 7 9 BugDemo2
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--This is the line that Visual Studio says it produce the error -->
<local:MainWindowViewModel x:Key="MainWindowViewModelDataSource" d:IsDataSource="True"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MainWindowViewModelDataSource}}"/>
</Window>
MainWindowViewModel.vb
Imports System.Data
Imports System.IO
Public Class MainWindowViewModel
Shared ds As New DataSet("Locality")
Public Sub New()
MySub()
End Sub
Sub MySub()
'this is the line that actually gives the error
ds.ReadXml(
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location) &
"\Locality.xml")
MsgBox(ds.Tables.Count)
End Sub
End Class
I have nothing in the code behind MainWindow.xaml.vb
Class MainWindow
End Class
The program does exactly what it is supposed to do, it loads Locality.xml to dataset correctly and shows how many tables are in it.
If I move the ds variable in another module, or in another class, as a shared property I get
Object reference not set to an instance of an object
also as a Designtime only error.
I understand that Visual Studio doesn't know if Locality.xml, but why does it care and how do I make this error go away?
Use protective code, and more debuggable since you can check the variable before executing ReadXml:
Sub MySub()
Dim path As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location) &
"\Locality.xml"
If IO.File.Exists(path) Then
ds.ReadXml(path)
MsgBox(ds.Tables.Count)
End If
End Sub

Inconsistency between XAML intellisense and compiler for a control

I am converting some Silverlight XAML into WPF. I currently have a user control (MyControl) that is trying to include a couple of other controls that are custom buttons (MyButton1) that are within the same assembly. I have the following XAML that compiles and works in SL:
MyButton1
<UserControl
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"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyButton1"
d:DesignWidth="640" d:DesignHeight="480">
...
</UserControl>
MyControl
<UserControl
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:somename="clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyControl"
d:DesignWidth="640" d:DesignHeight="480">
<somename:MyButton1 />
</UserControl>
When I try to compile this code in WPF, I get the following error:
The tag 'MyButton1' does not exist in XML namespace 'clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName.'
The weird thing is, if I comment out the <somename:MyButton1 /> line of code and compile and then type in <somename: IntelliSense gives me the option to autocomplete MyButton1. Which suggests that the control itself is in the assembly but for some reason it is not being seen when the MyControl XAML is being compiled.
For some context, I took the SL csproj file and made some modifications to it manually to make it a WPF csproj file. If there is a possibility that this caused this funkiness to happen, I'd be glad to share relevant portions of the project file.
Found the answer on an MSDN forum. Turns out that the assembly=MyCompany.MyAssemblyName line in my xmlns was screwing things up. Once I removed that line, I was able to reference the controls.
Related Link: http://social.msdn.microsoft.com/Forums/en/wpf/thread/807c9b80-81c7-4f75-aa2f-8427e17b1a90
To reference the current assembly you must not type its name but leave it blank, i.e. assembly=, the other option is of course to drop it completely. (MSDN -> Mapping to Current Assemblies)

User Control Test Container: Assembly doesn't contain any UserControls

This is killing me. I'm trying to test/debug a WPF UserControl library in Visual Studio 2010 with the User Control Test Container.exe. I keep getting the error "Assembly [...] doesn't contain any UserControls." This should be simple, and every msdn article and blog tutorial I've looked at suggests that it should all just work perfectly and magically.
Here's the code for a very simple test case which I cannot make work:
XAML:
<UserControl x:Class="UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="Hello!" Height="28" HorizontalAlignment="Left" Margin="86,106,0,0" Name="Label1" VerticalAlignment="Top" />
</Grid>
Code Behind:
Public Class UserControl1
Inherits UserControl
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Project File has debug options set to open UserControlTestContainer.exe, with ./MyTestLibrary.dll as a command-line arg.
Any suggestions on how to make this work?
Just to be sure, your User control is being hosted in a Window or something right? I am pretty certain you cannot display a usercontrol all by itself. What does your containing window look like?
The UserControl Test Container is only for libraries build using the Windows Control Library Project Template. Even then, it's a subset since Visual C++ controls can't be tested using it.

Overspecified Namespace in UserControl fails WPF Build

I have a very simple user control, and I'm trying to instantiate it in XAML. I find that when I go a bit overzealous with the namespacing, I run into problems with x:Name.
Here is my UserControl:
<UserControl x:Class="UserControlTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
<Grid>
<Label Name="Label1">Label</Label>
</Grid>
</UserControl>
Here is the code-behind for the UserControl:
Namespace UserControlTest
Partial Public Class UserControl1
End Class
End Namespace
Now, note that I have the root namespace of my VB.Net project set to "UserControlTest". Knowing that, have a look at my main window:
Here is my main window:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:control="clr-namespace:UserControlTest.UserControlTest"
Title="Window1" Height="300" Width="300">
<Grid>
<control:UserControl1 />
</Grid>
</Window>
See how the control alias needs to have "UserControlTest.UserControlTest"? That's because I have the root namespace of my project set to UserControlTest, and I have defined the namespace of the UserControl to be UserControlTest, also. If I don't use a namespace for the UserControl, I don't have any troubles.
However, because I have done this, the build fails should I try to apply an x:Name to the UserControl, as follows:
<control:UserControl1 x:Name="test"/>
That will fail the build, with this error:
Type 'UserControlTest.UserControlTest.UserControl1' is not defined.
Can anybody explain why? Do I need to avoid putting my UserControls into namespaces just so I can give them x:Name values? I'd like to manipulate my UserControls from code-behind, and without an x:Name, I'm up the creek. But I don't want to sacrifice namespace usage just to get it!
Thanks very much.
I had the same problem (after rebuilding the project, first it worked fine...). I put UserControl into separate namespace.
What is the namespace defined as in the code-behind of your user control?
If your project was called Foo and you had a folder called Controls inside that project, any new user control added to that folder would be given the namespace Foo.Controls.
Then in your XAML you can reference it like so:
xmlns:Controls="clr-namespace:Foo.Controls"
...
<Controls:UserControl1 x:Name="uc1"/>
It seems like you have a naming issue.
EDIT:
Here's how I'm doing it in a project of mine.
StatusBar.xaml.cs
namespace Client.Controls.UserControls
{
public partial class StatusBar : UserControl
{
...
}
}
StatusBar.xaml
<UserControl x:Class="Client.Controls.UserControls.StatusBar">
</UserControl>
Main.xaml.cs
using Client.Controls.UserControls;
namespace Client
{
public partial class Main : Window
{
...
}
}
Main.xaml
<Window x:Class="Client.Main"
xmlns:UserControls="clr-namespace:Client.Controls.UserControls">
<UserControls:StatusBar x:Name="mainStatusBar" />
</Window>
I encountered the same problem in a vb.net project, and despite trying the suggestions here and elsewhere, could not solve the issue. In fact, I was able to take the exact same code out of our project to a new project, and it worked just fine (as far as I could determine all the configuration in the new project was identical). In the solution provided by David, I notice he is using c# - I am wondering if this is some weirdness associated with vb.net.
In the end, the user control I needed to use was quite simple and I implemented it as a resource ControlTemplate to get around the issue. Sorry I don't have a better answer, I am really not happy wih my findings...

Resources