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

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.

Related

How to assemble a Namespace in a WPF project

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

WPF:programmatically add RibbonBar from UserControl into a RibbonWindow RibbonTab

I have a WPF Ribbon based application and a DLL.
In the application I uses a third party source (Syncfusion) to create my RibbonWindow.
The DLL is a WPF class library that consist of a UserControl which is a RibbonBar.
I would like to add that UserControl to my RibbonWindow.
I'm not sure how to do that programmatically.
MainWindow.xaml.cs
Dynamically loaded the UserControl from the DLL
Assembly asm = Assembly.LoadFile( unitDllPath );
Type typ = asm.GetType( "WX" + ".UserControl1", true, true );
unitDll = Activator.CreateInstance( typ );
MainWindow.xaml
<syncfusion:RibbonTab Name="Tab1" IsChecked="True" >
</syncfusion:RibbonTab>
UserControl.xaml
<syncfusion:RibbonBar Header="Select" Name="Bar1" ></syncfusion:RibbonBar>
UserControl.xaml.cs
public void MainWindow()
{
InitializeComponent();
}
How can I get the RibbonBar from WX.UserControl (Bar1) into (Tab1)?
Thanks.
1)If we need to programmatically add UserControl which is a RibbonBar to your RibbonWindow, your UserControl should be like below
UserControl.xaml
<syncfusion:RibbonBar x:Class="WPFClassLibrary.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
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">
<syncfusion:RibbonButton SizeForm="Large" Label="New Email"/>
<syncfusion:RibbonButton SizeForm="Large" Label="Inbox"/>
<syncfusion:RibbonButton SizeForm="Large" Label="Drafts"/>
<syncfusion:RibbonButton SizeForm="Large" Label="OutBox"/>
2)Refer the assembly file in your application.
3)use that Assembly in code behind like below
using WPFClassLibrary;
4)now we can call that UserControl from code behind and add to the our RibbonWindow.
tab1.Items.Add(new UserControl1());
i have also attach simple sample for this
http://www.syncfusion.com/downloads/support/directtrac/general/ze/WPfRibbonSample-1343767783

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);
}
}
}

WPF Databinding to Custom Collection Objects

Public Sub New(ByVal log As Entities.LogSystem)
InitializeComponent()
Me.DataContext = log
End Sub
This is the the initializer for my custom control It passes in a single entity that has several property fields. This control is added to a parent control so that it appears in a stackpanel.
Anyway I am trying to get the specific data from this control into several different text boxes:
<UserControl x:Class="LogSystemPickerItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WavelengthIS.WISRED.UserControls"
Width="579" Height="122">
<UserControl.Resources>
<local:LogSystemPickerItem x:Key="Log"/>
</UserControl.Resources>
<Grid DataContext="{Binding Source={StaticResource Log}}">
<Label Height="30" Name="Label1" VerticalAlignment="Top" Content="{Binding deptDescription}"/>
</Grid>
</UserControl>
As you can see i havent really gotten too far. I have tried many different ways to do this including using dependency properties...I just really cant find a tutorial that shows this specific circumstance...can anyone point me in the right direction?
If you're setting the DataContext in the code behind as per your first code snippet, there's no need to also do it in the XAML, so you can remove the "Log" resource and the corresponding DataContext assignment on the Grid.
Once you've done that, it should work assuming there is a deptDescription property on your log class.
... and in XAML you may do it this way...
<UserControl.DataContext>
<local:LogSystemPickerItem/>
</UserControl.DataContext>

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