I have a UserControl, which I have set to Internal as such:
UserControl x:Class="ClassName"
x:ClassModifier="internal"
And it’s matching .cs file:
internal partial class ClassName : UserControl
{
public ClassName()
{
InitializeComponent();
}
}
Now, when I try to use the control on a window, like this:
uControls:ClassName x:Name="instanceName" Margin="0,0,8,0" Height="60" VerticalAlignment="Top" Width="60" HorizontalAlignment="Right" MouseLeftButtonUp="instanceName_MouseLeftButtonUp" Cursor="ScrollNW"
I get an error stating:
The type 'ClassName' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary. Line 12 Position 44.
If I change the access modifiers to public, all works as expected.
Why can I not have internal User Controls?
I believe that this is caused by the compilation model for WPF. I’m quoting this from memory and can’t find a resource to back it up, but I believe that WPF compilation happens in two passes: essentially the “codebehind” portion is compiled first to an intermediate assembly, then the XAML is compiled as referencing that intermediate, and the results are combined to the final assembly.
For this reason, the classes/members that the XAML references have to be declared publicly, because (as far as the compiler is concerned) they’re in a separate assembly to the XAML.
EDIT: found a Microsoft reference here that outlines the process. In particular, in terms of supporting what I said above, it says: