0

I have question. I created a custom user control. My CustomUserControl is inherited from UserControl. I add some custom methods and properties in my CustomUserControl. I wanted to add my CustomUserControl to "Add New Item" in Visual Studio for projects.

For this I used "Item Template" and created a template. After restarting Visual Studio every thing was fine and I could add my CustomUserControl by using "Add New Item" in my project.

Just i have a problem when I add CustomUserControl to my project, the methods and properties that I am added into template file appeare and i can change them. How can I Hide methods and property in template? I don't want to see methods and properties after add CustomUserControl to project.

Note : When i add my CustomUserControl project, "CustomUserControl1" is created and it inherit from the UserControl not my CustomUserControl.

My Template is :

public partial class CustomUserControl : UserControl
{

    private string _Version;

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    public string Version
    {
        get { return _Version; }
        private set { _Version = value; }
    }

    private void InitRequirements()
    {
        try
        {

            // ... My Code

        }
        catch (Exception exp)
        {

            throw exp;
        }
    }


}

After adding to project :

public partial class CustomUserControl1 : UserControl
{

    private string _Version;

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    public string Version
    {
        get { return _Version; }
        private set { _Version = value; }
    }

    private void InitRequirements()
    {
        try
        {

            // ... My Code

        }
        catch (Exception exp)
        {

            throw exp;
        }
    }


}

it should be like this :

public partial class CustomUserControl1 : CustomUserControl
{
    // Without showing methods and properties
}

Thank you Best regards,

4

1 回答 1

1

您可以尝试使用关键字隐藏不想显示的属性new

例如,如果要隐藏属性Text,可以添加以下内容:

[Bindable(false)]
[Browsable(false)]
public new string Text { get; set; }

编辑

如果您想在其他项目中重用您的控件,而不是“项目模板”,您可能希望创建一个库或程序集并在您的项目中引用它。像这样,您将能够使用它并从它继承而无需查看代码。当您使用项目模板时,它只会UserControl根据模板上保存的代码创建一个新的,但您不会重用它UserControl本身。如果你想处理版本控制等,你也可以创建一个 NuGet 包......

于 2018-12-05T09:09:54.317 回答