Larry S
Larry S

Reputation: 457

Strange compile errors and Visual Studio crashes when creating WPF UserControls

I am trying to mimic some code I inherited, and to create a WPF UserControl that uses another UserControl in the same library.

I have created a brand new a project that is a UserControl Library. The library is called MyLib. It contains a simple UserControl in the file UserControl1.xaml

<UserControl x:Class="MyLib.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>
      <TextBlock>Hello</TextBlock>
    </Grid>
</UserControl>

I then created another UserControl (in the same Library/Project) that will use UserControl1 as part of it:

<UserControl x:Class="MyLib.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:lib="clr-namespace:MyLib"
             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>
    <lib:UserControl1 />
  </Grid>
</UserControl>

I get an error on the line:

xmlns:lib="clr-namespace:MyLib"

Saying "Error 2 Assembly must be specified for XAML files that are not part of a project. Reopen this XAML file after adding it to a project, close this file and reopen it using the project it is associated with, or modify the clr-namespace to include the name of the assembly."

But strangely I get auto-complete when I added the line:

<lib:UserControl1 />

I also occasionally get the odd error:

Error 1 Unknown build error, 'Object reference not set to an instance of an object.' MyLib

I am able to use the UserControl1 in a separate project (same solution, with a reference added to MyLib) as so:

<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lib="clr-namespace:MyLib;assembly=MyLib"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <lib:UserControl1 />
  </Grid>
</Window>

As a sanity-check, should my UserControl2 work? It's been a while since I worked with WPF UserControls, so I'm wondering if I'm doing something silly.

UPDATE Replicated on a clean install of both Windows and Studio on a new machine.

Upvotes: 0

Views: 393

Answers (3)

Larry S
Larry S

Reputation: 457

It took a lot of experimentation, but I tracked things down. My original example will work if you start a project from scratch, and type things in exactly as shown.

Where things went off the rails was when I introduced a bug in the UserControl that threw an exception that was being swallowed behind the scenes (a null string being used instead of an empty string). It appears that the compiling Visual Studio does for design-time display gets cranky and out-of-synch with the source code, generating spooky-looking errors that don't match up with source code.

The solution was to remove the error-causing code, save and close the solution. Then reload it. That seems to synch things up between display-time and the real Build process. As long as the (silent) error occurs during a session in Visual Studio, things remain spooky until reload of the project.

An important trick: protect your code-behind blocks for the properties on your UserControls with try-catch, especially when trying to diagnose an issue like this.

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54562

Usually in instances like this, I will make sure that I build the project first. Then you will get the UserControls available in your Toolbox, you can then drag and drop it on the UserControl you want to host the other UserControl and let Visual Studio figure out the NameSpace. I don't have your code, but using a Namespace of MyLib it came up with:

xmlns:my="clr-namespace:myLib"

Upvotes: 0

eran otzap
eran otzap

Reputation: 12533

try

xmlns:lib="clr-namespace:MyLib;assembly=MyLib"

Upvotes: 0

Related Questions