![](/img/trans.png)
[英]VS2015 Community Edition Crashes when starting: “Component Cache out of date”
[英]VS2015 Community Crashes when using Invalidate() in custom Control
美好的一天,我正在基於Visual Studio中的標准控件制作一些自定義控件,但是為平樣式應用程序添加了更多功能,這是我通常所做的。 一切進展順利,因為我已經做了很多功能控制。
由於Visual Studio崩潰導致太多工作丟失后,我進行了調查,至少發現了這個問題的根源。
每當我初始化一個可以使用設計修改的變量時,例如說一個使邊框打開或關閉的布爾值(請參見下圖),我this.Invalidate()
在所述變量的Set事件期間調用this.Invalidate()
命令。 但是,每當我添加代碼行並單擊“生成解決方案”或進入“設計”視圖時,我的Visual Studio就會完全死機,並且我發現在任務管理器中運行的Windows錯誤報告過程中,這永遠不會返回我可以使用的錯誤。
每次我直接將Visual Studio重新啟動到項目中時,都會出現一個彈出窗口,告訴我Visual Studio由於錯誤而崩潰,並且沒有加載我打開過的任何文件。
請注意,即使我設置了DrawMode = DrawMode.OwnerDrawFixed
,並且還調整了所有SetStyle()
,這都會發生在我的任何自定義控件和變量上
編輯:Apologiez,代碼示例:
[Category("Appearance")]
public bool HasBorder
{
get
{
return HasBorder;
}
set
{
HasBorder = value;
this.Invalidate();
}
}
public vComboBoxList()
{
Items = new List<object>();
DataSource = new List<object>();
SelectedItemColor = SystemColors.Highlight;
HoverItemColor = SystemColors.HotTrack;
BorderColor = SystemColors.ActiveBorder;
HasBorder = false;
ItemHeight = 16;
BorderThickness = 2;
}
protected override void OnCreateControl()
{
base.OnCreateControl();
}
protected override void OnPaint(PaintEventArgs e)
{
Height = Items.Count * ItemHeight;
if (Items.Count > 0)
{
foreach (object Item in Items)
{
if (Items.IndexOf(Item) == ItemOver)
e.Graphics.FillRectangle(new SolidBrush(HoverItemColor), new Rectangle(0, Items.IndexOf(Item) * ItemHeight, Width, ItemHeight));
else if (Items.IndexOf(Item) != ItemOver)
e.Graphics.FillRectangle(new SolidBrush(BackColor), new Rectangle(0, Items.IndexOf(Item) * ItemHeight, Width, ItemHeight));
e.Graphics.DrawString(Items.IndexOf(Item).ToString(), Font, new SolidBrush(ForeColor), new Point(4, Items.IndexOf(Item) * ItemHeight));
}
}
if(HasBorder && BorderThickness != 0)
{
Rectangle _Top = new Rectangle(0, 0, Width, BorderThickness);
Rectangle _Right = new Rectangle(Width, 0, -BorderThickness, Height);
Rectangle _Left = new Rectangle(0, 0, BorderThickness, Height);
Rectangle _Bottom = new Rectangle(0, Height, Width, -BorderThickness);
e.Graphics.FillRectangles(new SolidBrush(BorderColor), new Rectangle[] { _Top, _Right, _Left, _Bottom });
}
base.OnPaint(e);
}
如果我從“ set {}”中刪除“ this.Invalidate()”,則效果很好。
此屬性設置器具有無限的遞歸:
[Category("Appearance")]
public bool HasBorder
{
get
{
return HasBorder;
}
set
{
HasBorder = value; // will call setter again, which will call setter, which ...
this.Invalidate();
}
}
將其更改為普通的完整屬性:
bool _hasBorder;
public bool HasBorder
{
get { return _hasBorder; }
set
{
_hasBorder = value;
Invalidate();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.