在WPF用户控制库中隐藏控件

时间:2022-11-04 13:21:19

I've a project made from the "WPF User Control Library" Template in Visual Studio. This project contains one main usercontrol plus additional Windows/Usercontrols.

我有一个项目是用Visual Studio中的“WPF用户控制库”模板做的。这个项目包含一个主用户控件和附加的Windows/Usercontrols。

How can I "hide" these additional Windows/Usercontrols, so that the user can only import the main usercontrol from the assembly (I wanted to put a screen-shot to illustrate my question but unfortunately, my "reputation" is too low!).

如何“隐藏”这些附加的Windows/Usercontrols,以便用户只能从程序集中导入主usercontrol(我想放一个屏幕截图来说明我的问题,但不幸的是,我的“声誉”太低了!)

Thx All
Fred

谢谢所有的弗雷德

3 个解决方案

#1


23  

Make those controls internal. If you have classic UserControls with XAML and codebehind you will need to add x:ClassModifier="internal" to the root element in the XAML:

确保这些控件内部。如果在XAML和codebehind下有经典的UserControls,则需要在XAML中的根元素中添加x:ClassModifier="internal":

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyNameSpace.MyUserControl"
    x:ClassModifier="internal">
       <!-- bla -->
</UserControl>

#2


0  

Maybe the usage of attributes will solve your problem. There is one attribute "DesignTimeVisible" inside the ComponentModel namespace. If you put such an attribute right above your class implementation and set it to false, the corresponding control should not be visible in the designers toolbox.

也许属性的使用可以解决您的问题。在ComponentModel名称空间中有一个属性“DesignTimeVisible”。如果您将这样的属性放在类实现的正上方并将其设置为false,则相应的控件在设计器工具箱中不应该是可见的。

#3


0  

I believe that x:ClassModifier="internal" will make the entire user control internal. This may not be desirable.

我相信x:ClassModifier="internal"将使整个用户控件成为internal。这可能不可取。

Instead if you add x:FieldModifier="private" to those controls within the user control that you don't wish to be accessible to the UserControl consumer, the generated C# will have those controls as private. Note the use of lower case which is correct for a C# field modifier.

相反,如果您将x:FieldModifier="private"添加到用户控件中您不希望对UserControl consumer进行访问的控件中,那么生成的c#将把这些控件设置为private。注意使用小写的c#字段修饰符是正确的。

#1


23  

Make those controls internal. If you have classic UserControls with XAML and codebehind you will need to add x:ClassModifier="internal" to the root element in the XAML:

确保这些控件内部。如果在XAML和codebehind下有经典的UserControls,则需要在XAML中的根元素中添加x:ClassModifier="internal":

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyNameSpace.MyUserControl"
    x:ClassModifier="internal">
       <!-- bla -->
</UserControl>

#2


0  

Maybe the usage of attributes will solve your problem. There is one attribute "DesignTimeVisible" inside the ComponentModel namespace. If you put such an attribute right above your class implementation and set it to false, the corresponding control should not be visible in the designers toolbox.

也许属性的使用可以解决您的问题。在ComponentModel名称空间中有一个属性“DesignTimeVisible”。如果您将这样的属性放在类实现的正上方并将其设置为false,则相应的控件在设计器工具箱中不应该是可见的。

#3


0  

I believe that x:ClassModifier="internal" will make the entire user control internal. This may not be desirable.

我相信x:ClassModifier="internal"将使整个用户控件成为internal。这可能不可取。

Instead if you add x:FieldModifier="private" to those controls within the user control that you don't wish to be accessible to the UserControl consumer, the generated C# will have those controls as private. Note the use of lower case which is correct for a C# field modifier.

相反,如果您将x:FieldModifier="private"添加到用户控件中您不希望对UserControl consumer进行访问的控件中,那么生成的c#将把这些控件设置为private。注意使用小写的c#字段修饰符是正确的。