![](/img/trans.png)
[英]How can I bind a ListBox's Visibility to an ApplicationBar button?
[英]How to bind ApplicationBar in CaliburnMicro?
我有使用Caliburn.Micro框架的Windows Phone 8應用程序,其中需要多個應用程序欄。 擁有一個應用程序欄很容易。 我只是將以下代碼添加到我的XAML中,它會自動綁定:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<cal:AppBarButton IconUri="/Images/appbar.edit.rest.png" Text="edit mode" Message="SwitchToEditMode"/>
<shell:ApplicationBar.MenuItems>
<cal:AppBarMenuItem Text="test" Message="Test"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
然后,我嘗試用另一個這樣的應用程序欄替換:
NewBar = new ApplicationBar();
var btn1 = new AppBarButton();
btn1.IconUri = new Uri("/Images/appbar.check.rest.png", UriKind.RelativeOrAbsolute);
btn1.Text = "get old bar";
btn1.Message = "SwitchToOldBar";
NewBar.Buttons.Add(btn1);
(GetView() as MyPage).ApplicationBar = EditBar;
此代碼更改了應用程序欄,但新欄不響應命令。 應用程序欄按鈕的單擊處理程序也為空。 我可以添加自己的處理程序並完成它,但這與MVVM精神背道而馳。 如何將新的應用程序欄綁定到Caliburn.Micro中的現有視圖模型?
更新這里是我的最終解決方案,從語法上看,它與初始代碼沒有什么不同。 對於XAML使用“消息”並且背后的代碼使用單擊處理程序,我感到有點不滿意,但是它可以工作。 這是更新的代碼:
NewBar = new ApplicationBar();
var btn1 = new AppBarButton();
btn1.IconUri = new Uri("/Images/appbar.check.rest.png", UriKind.RelativeOrAbsolute);
btn1.Text = "get old bar";
// Original line: btn1.Message = "SwitchToOldBar";
btn1.Click += (sender, e) => { this.SwitchToOldBar(); };
NewBar.Buttons.Add(btn1);
(GetView() as MyPage).ApplicationBar = EditBar;
您找不到Windows Phone Silveright中的應用程序欄,因為它實際上不是Silverlight元素。 有一些第三方解決方案,例如可綁定的應用程序欄 。
因為應用程序欄“不同”,所以僅在導航到頁面時才評估Caliburn AppBarButton上的Message屬性。 創建Caliburn AppBarButton是后面的代碼。
我建議關閉按鈕后使用事件處理程序,無論它是否可綁定,並使用DataContext將其調用到視圖模型中。
在“ MVVM精神”中,這仍然很好,只是您自己將視圖和視圖模型連接在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.