I’m new to SL and must be missing something really fundamental here.
I have created a very simple user control like so:-
<UserControl x:Class="Company.UI.Common.Controls.TestControl"
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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="Yellow" Width="100" Height="20">
<TextBlock Text="foo"></TextBlock>
</Grid>
</UserControl>
Then in my view I’m referencing it as follows:-
xmlns:medControls="clr-namespace:Company.UI.Common.Controls;assembly=Company.UI.Common"
Then including it in the UI like this:-
<medControls:TestControl Width="100" Height="20" Visibility="Visible" />
However nothing appears when I run the app, just an empty space 100×20 pixels where the control should be. I’ve used Silverlight Spy and it shows the control being present, with all the correct details – type, assembly, visibility, etc.
I have put a breakpoint in the user control’s constructor, and can confirm that InitializeComponent() is being called.
Any suggestions as to what is happening would be greatly appreciated, as I’m tearing my hair out over what should be a very simple thing to do!
Thanks in advance
Andy
This turned out to be a known bug in VS2010, reported here: http://connect.microsoft.com/VisualStudio/feedback/details/683175/visual-studio-failing-to-embed-g-resources-file-in-dll-with-certain-silverlight-project-files
Basically, in some situations, the order of elements in the .csproj file can change, resulting in the XAML files not being included in the assembly, and this is what was happening with me. No runtime errors. Nothing. Just an empty space where the user control should have rendered.
The above link tells you what you need to do to resolve the problem, and involves manually editing the .csproj file and moving certain elements around.
Incidentally, this same bug was also the cause of another problem I’d been having where my user control’s code-behind was referencing a control (e.g. “this.txtForename”). The control reference, which is set up in InitializeComponent using FindName(), was always null.
Hope this helps someone else.
Andrew