Can't set x:Name=“Root” on UserControl

2019-03-06 04:23发布

问题:

I am trying to set the x:Name attribute to the definition of a UserControl to use it for internal bindings later on:

<UserControl 
    x:Name="root"
    x:Class="ElementManager.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">
</UserControl>

It doesn't compile saying:

The type name 'UserControl1' does not exist in the type 'ElementManager.ElementManager'

If I remove the x:Name attribute, it compiles and everything works fine. Is this meant to be or am I missing something trivial?

回答1:

The XAML-MSBuild task is confusing your type ElementManager with the namespace of the same name. Give your type ElementManager another name than its surrounding namespace.