简体   繁体   中英

How to bind ApplicationBar in CaliburnMicro?

I have Windows Phone 8 app that uses Caliburn.Micro framework where I need multiple application bars. Having one application bar is easy. I just add the following code to my XAML and it binds automagically:

<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>   

Then I try to replace application bar with another one like this:

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;

This code changes application bar but the new bar does not react to commands. Click handler of the application bar button is also empty. I could just add my own handler and be done with it but it is against MVVM spirit. How can I bind my new application bar to an existing view model in Caliburn.Micro?

Update Here is my final solution which syntactically does not look that different from the initial code. I am a bit unhappy that XAML uses "message" and code behind uses click handler but it works. Here is updated code:

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;

You can't find to an Application Bar in Windows Phone Silveright since it's not really a Silverlight element. There some third party solutions such as Bindable Application Bar .

Because the app bar being "different" the Message property on the Caliburn AppBarButton is only evaluated when the page is navigated to. Creating a Caliburn AppBarButton is code behind.

I'd suggest using an event handler off the button whether it's the bindable one or not and have that call into the view model using DataContext.

That's still well within "the MVVM spirit", it's just that you're wiring the view and view model together yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM