I have an assembly that defines a UserControl X. Within that assembly, X is composed of subcontrols Y and Z. I would like Y and Z to be marked internal, so that only X is available to users of my assembly.
I tried to accomplish this by changing the default public accessibility in the XAML and codebehind classes, writing:
<UserControl
x:Class="MyNameSpace.Y"
x:ClassModifier="internal"
...
and
namespace MyNameSpace
{
internal partial class Y : UserControl
{
...
When I do this, everything compiles fine, but then at runtime when initializing the top level user control X, I get the dreaded error AG_E_PARSER_BAD_TYPE
at the point in X.xaml where I say
<my:X Name="TheX"></my:X>
As I compile and edit, I also sometimes (but not always) see this line marked as an error in the XAML editor, with a message about X not being known.
Changing the accessibility back to 'public' immediately fixes the problem.
So it appears that my internal开发者_JAVA技巧 subcontrols cannot be hidden from the outside by marking them internal, because this also hides them from some part of the XAML mechanism. Is this correct? Is there any way to work around it and hide the pieces I don't actually want to share?
Yes, you need your controls to be accesible for them to be usable. One thing you can do if you don't want them to be sharable is to check the parent type to make sure it is a parent type that you have coded. Then if someone sticks your controls in their application without it being inside of your host control you can throw an exception.
精彩评论