[英]winUserControl in VS2010 - properties are not visible in designer
我對(我想)我的 Visual Studio 2010 Express 環境有一個問題:當我設計自己的 UserControl 時,在屬性網格中我看不到該控件的公共屬性。 然而,它們在項目中是可見的,它們引用了這個控件。
因為它是 Express Edition,所以我創建了新的空項目,然后向其中添加了新的 UserControl。
然后,為了測試,我輸入了以下代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Project1
{
public partial class UserControl1 : UserControl
{
private int myNumber;
[Browsable(true)]
public int MyNumber
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
}
我記得在 VS 2008 中,即使沒有[Browsable(true)]
屬性,在屬性網格中顯示 MyNumber 屬性也應該足夠了。 然而,在 VS 2010 中,當我在解決方案資源管理器中雙擊 UserControl1.cs 並查看屬性時,我看不到 MyNumber。
當我在另一個項目中引用和使用此控件時,可以訪問它的屬性。
我試圖重新安裝 VS 2010 環境,包括 SP1,但沒有成功。 你知道有什么問題嗎?
順便說一句:這些屬性都不起作用,要么:
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Bindable(true)]
此致,
馬爾欽
我相信這是 VS2010 的正常行為,並假設它是設計使然。 它在 2010 Ultimate 中對我來說表現相同。 當您將UserControl1
放在窗體上時,您將看到它的自定義屬性。
我的猜測是這是因為當你設計控件時,還沒有你的控件實例(它甚至可能還沒有被編譯)。 您正在查看的是UserControl
的一個實例。 當您編譯控件然后將其添加到窗體時,設計器會創建控件的實例,因此可以查看和操作其屬性。
我以前沒有使用過 [Browsable] 標簽。 然而,下面是我在我的一個項目中使用的示例。
[Description("The length used to display the dimensions")]
[Category("Custom")]
public double DisplayLength { get; set; }
我猜你需要添加一個類別。
由於 VS 在 Designer 中處理 ascx'es 的方式,這將不起作用。 有關詳細信息,請參閱關於 SO 的出色答案。
如果答案不是您所期望的,您仍然可以將.ascx'es 遷移到我在博客中描述的用戶控件庫。
如果可以選擇,我會將我的所有 ascx 代碼作為Custom Web Server Controls重新開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.