2013-07-18 23 views
4

我注意到我的任務對話框(命令鏈接的標題和指令文本之間的空間)有一個很大的垂直空間,看起來非常糟糕。它在我將WindowsAPICodePack升級到版本1.1後開始出現。從v1.1開始,TaskDialog中命令鏈接的垂直空間

下面的代碼:

TaskDialog td = new TaskDialog(); 
var b1 = new TaskDialogCommandLink("b1", "foo", "bar"); 
var b2 = new TaskDialogCommandLink("b2", "one", "two"); 
td.Controls.Add(b1); 
td.Controls.Add(b2); 
td.Caption = "Caption"; 
td.InstructionText = "InstructionText"; 
td.Text = "Text"; 
td.Show(); 

這裏的結果:

Ugly task dialog with vertical space in command links

,「酒吧」將出現右下面的「富」,但現在看起來好像有一個空兩者之間的界限。這是否是我的問題(有人會知道它會是什麼)或者你們是否也遇到過這個問題?

回答

5

我在1.1版本中遇到了同樣的錯誤。這似乎是由於TaskDialogCommandLink類的toString方法string.FormatEnvironment.NewLine,當傳遞給TaskDialog本身時不會完全映射。

public override string ToString() 
{ 
    return string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", 
    Text ?? string.Empty, 
    (!string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(instruction)) ? 
     Environment.NewLine : string.Empty, 
    instruction ?? string.Empty); 
} 

我使用實現子類反正,使論點更容易,過騎着方法傳遞包含一個簡單的「\ n」字符串,雖然我並不需要我的國際化應用等能做更簡單的事情。

public override string ToString() 
{ 
    string str; 

    bool noLabel = string.IsNullOrEmpty(this.Text); 
    bool noInstruction = string.IsNullOrEmpty(this.Instruction); 

    if (noLabel & noInstruction) 
    { 
     str = string.Empty; 
    } 
    else if (!noLabel & noInstruction) 
    { 
     str = this.Text; 
    } 
    else if (noLabel & !noInstruction) 
    { 
     str = base.Instruction; 
    } 
    else 
    { 
     str = this.Text + "\n" + this.Instruction; 
    } 
    return str; 
} 
2

我注意到在Windows 8與API代碼包1.1版相同的間距問題DougM是正確的,他的ToString()覆蓋會解決這個問題。

這裏是一個更新版本,簡單地丟棄這個類到你的項目,而不是使用TaskDialogCommandLink,使用TaskDialogCommandLinkEx來代替。

using Microsoft.WindowsAPICodePack.Dialogs; 

internal class TaskDialogCommandLinkEx : TaskDialogCommandLink 
{ 
    public override string ToString() 
    { 
     string str; 

     var noLabel = string.IsNullOrEmpty(Text); 
     var noInstruction = string.IsNullOrEmpty(Instruction); 

     if (noLabel & noInstruction) 
     { 
      str = string.Empty; 
     } 
     else if (!noLabel & noInstruction) 
     { 
      str = Text; 
     } 
     else if (noLabel & !noInstruction) 
     { 
      str = Instruction; 
     } 
     else 
     { 
      str = Text + "\n" + Instruction; 
     } 
     return str; 
    } 
}