簡體   English   中英

添加到項目后如何隱藏我在“創建的項目模板”中編寫的方法和屬性?

[英]How to hide Methods and Properties that i wrote in Created Item Template after adding to project?

我有問題。 我創建了一個自定義用戶控件。 我的CustomUserControl繼承自UserControl。 我在CustomUserControl中添加了一些自定義方法和屬性。 我想將我的CustomUserControl添加到項目的Visual Studio中的“添加新項”中。

為此,我使用了“項目模板”並創建了一個模板。 重新啟動Visual Studio之后,一切都很好,我可以通過在項目中使用“添加新項”來添加CustomUserControl。

當我將CustomUserControl添加到我的項目中時,只是出現一個問題,出現了添加到模板文件中的方法和屬性,我可以更改它們。 如何隱藏模板中的方法和屬性? 將CustomUserControl添加到項目后,我不想看到方法和屬性。

注意:添加我的CustomUserControl項目時,將創建“ CustomUserControl1”,它繼承自UserControl,而不是CustomUserControl。

我的模板是:

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;
        }
    }


}

添加到項目后:

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;
        }
    }


}

應該是這樣的:

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

謝謝您最好的問候,

您可以嘗試使用new關鍵字隱藏不想顯示的屬性。

例如,如果要隱藏屬性Text ,則可以添加以下內容:

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

編輯

如果要在其他項目中重用控件,而不是“項目模板”,則可能要創建一個庫或程序集並在項目中引用它。 這樣,您無需查看代碼就可以使用它並從中繼承。 使用項目模板時,它將僅基於模板上保存的代碼創建一個新的UserControl ,但您不會重用UserControl本身。 如果要處理版本控制等,也可以創建一個NuGet包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM