【发布时间】:2015-04-03 00:08:39
【问题描述】:
我有使用 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;
【问题讨论】:
标签: c# windows-phone-8 mvvm caliburn.micro