[英].NET winforms bug (savefiledialog)?
以下示例僅在ToolStripButton單擊事件上顯示和SaveFileDialog。 如果我預先制作SaveFileDialog,然后雙擊ToolStripButton,則應用程序堆棧會溢出。 好像Winforms中的一個bug給了我。 從MS獲得修復甚至回復都不樂觀(即使在幾年前他們剛剛回復“當我報告錯誤時沒有更多錯誤修復winforms”),所以我只是想看看這是否是一個錯誤的一些意見或者我做錯了什么。
using System;
using System.Windows.Forms;
namespace ToolStripDoubleClickSaveDialog
{
public partial class Form1 : Form
{
SaveFileDialog sfd = new SaveFileDialog();
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
sfd.ShowDialog(this);
}
private void InitializeComponent()
{
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(284, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(23, 22);
this.toolStripButton1.Text = "double click me";
this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.toolStrip1);
this.Name = "Form1";
this.Text = "Form1";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton1;
}
}
好的,所以當控件被Doubleclicked時會出現問題,因為它被設置為在單擊時打開對話框,並且兩個點擊事件都試圖同時打開對話框。 我的猜測是,當對話框加載時,應用程序在對話框打開之前短暫進入短暫的空閑狀態,足夠長以允許另一個事件被調用,當它調用ShowDialog()
兩次時會導致錯誤。
為了防止這種情況,您可以獲取窗口的System.Runtime.Remoting.Lifetime.Lease
並在顯示之前仔細檢查它是否處於活動狀態。
using System.Runtime.Remoting.Lifetime;
//.....
private SaveFileDialog sfd;
private ILease sfdLease;
public Form1()
{
InitializeComponent();
sfd = new SaveFileDialog();
sfdLease= (ILease)sfd.InitializeLifetimeService();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if(sfdLease.CurrentState != LeaseState.Active)
sfd.ShowDialog(this);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.