WPF: Can't add Name or EventHandlers to instance of a custom control defined in different assembly

StackOverflow https://stackoverflow.com/questions/17373423

سؤال

I can successfully add controls defined in my Assets assembly in my main project.

For example, a control like this:

using System.Windows.Controls;
namespace AdvancedAudioGui.Assets.Controls
{
    public class MyControl: Control
    {
        public string MyProperty { get; set; }
    }
}

works like this:

<UserControl x:Class="MyCompanyName.AdvancedAudioGui.Parts.MyPart"
         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:controls="clr-namespace:AdvancedAudioGui.Assets.Controls;assembly=AdvancedAudioGui.Assets"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
        <controls:MyControl />
</Grid>

but not like this (Note: the same thing happens with Name or event handlers like MouseDown):

<UserControl x:Class="MyCompanyName.AdvancedAudioGui.Parts.MyPart"
         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:controls="clr-namespace:AdvancedAudioGui.Assets.Controls;assembly=AdvancedAudioGui.Assets"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
        <controls:MyControl x:Name="MyInstance"/>
</Grid>

I get the error:

The type or namespace name 'Assets' does not exist in the namespace 'OurCompanyName.AdvancedAudioGui' (are you missing an assembly reference?)

If I create them in code behind I can add a name so it must be a xmlns issue, but I can't see it. Any ideas?

Thanks!

هل كانت مفيدة؟

المحلول

Wow, so I finally figured this out. Hopefully it will help someone else who is unlucky enough to have the same problem.

OLD namespaces WERE as follows:

main project namespace: CompanyName.AdvancedAudioGui

assets project namespace: AdvancedAudioGui.Assets

When I added the x:Name attribute on an instance of a control in the main project that was defined in assets project, it would fail with the error:

The type or namespace name 'Assets' does not exist in the namespace 'OurCompanyName.AdvancedAudioGui' (are you missing an assembly reference?)

When I changed my namespaces in the assets project to more closely match the main project, the error went away.

New namespaces are as follows:

main project namespace: CompanyName.AdvancedAudioGui (same as before)

assets project: CompanyName.AdvancedAudioGui.Assets

The latter way is preferable anyway, but it appears that the assets namespace being a subset on the main project's namespace somehow confused the compiler when it was trying to parse the xaml namespaces.

Maybe someone knows why that was the case...?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top