2

在我的方案中,我想爲我的所有Windows 8.1應用程序頁面編寫BasePage。 在此BasePage中應該創建TopAppBar按代碼添加AppBar(Windows 8.1,C#)

其實我有:

public CommandBar TopCommandBar 
{ 
    get 
    { 
     // Check if a TopAppBar exists 
     if (this.TopAppBar != null) return this.TopAppBar.Content as CommandBar; 

     var appBar = new AppBar(); 
     this.TopAppBar = appBar; 
     var top = this.TopAppBar.Content as CommandBar; 
     if (top == null) 
     { 
      topCommandBar = new CommandBar(); 
      this.TopAppBar.Content = topCommandBar; 
     } 
     return this.TopAppBar.Content as CommandBar; 
    } 
} 

此代碼工作得很好。但後來在我的BaseClass我想添加一個AppBarButton

if (ShowCloseButton) 
{ 
    var closeBtn = new AppBarButton(); 
    closeBtn.Icon = new SymbolIcon(Symbol.Clear); 
    closeBtn.Label = "Close"; 
    closeBtn.Click += closeBtn_Click; 
    this.TopCommandBar.PrimaryCommands.Insert(0, closeBtn); 
} 

的strage行爲是closeBtn不會在TopAppBar顯示,直到我點擊我的鼠標右鍵兩次。

意思是我第一次點擊右鍵 - >出現TopAppBar,但裏面沒有按鈕。

然後我再次單擊右鍵 - >TopAppBar保持打開狀態,並且該按鈕顯示其全部功能。

+1

似乎是一個錯誤。在WPF的美好時代,我會嘗試使父控制無效。作爲測試,您可以在添加新按鈕時切換appbar的可見性。或者,重新創建整個酒吧。 – WiredPrairie

回答

2

是的,我同意這看起來像一個錯誤。我看到了代碼生成路線的相同情況。經過調查,它看起來像AppBar.IsOpen在右鍵單擊或滑動過程中切換爲true,但CommandBar.IsOpen仍然爲false。此修補程序適用於我:

BottomAppBar.Opened + =(o,args)=> {(this.BottomAppBar.Content as CommandBar).IsOpen = true; };

+0

它給了我一個**'System.NullReferenceException'**!在調用之前,我應該測試什麼不應該爲空? – yalematta

0

您也可以直接使用AppBar爲命令欄:

CommandBar appBar = this.BottomAppBar as CommandBar; 
    if (appBar == null) 
    { 
     appBar = new CommandBar(); 
     this.BottomAppBar = appBar; 
    } 

    var btnAbout = new AppBarButton() { Icon = new SymbolIcon(Symbol.Help), Label = "About" }; 
    btnAbout.Click += btnAbout_Click; 
    appBar.SecondaryCommands.Add(btnAbout); 
相關問題