Reputation: 3111
In my MainPage.xaml
, I created a Pivot Control: <controls:Pivot Title="Powder God" Name="PivotControl">
.
My first pivot view is a HubTile
that summarize all other individual pages. So my application bar will be different between the first pivot view and all other ones.
That's why I put my application bar in App.xaml
's resource section, then load based on selected index of my pivot.
My question is:
In the application bar I will be using for all individual pages, I want to have a delete option, where I will remove that specific item (a view model) from my data context.
I know I can use PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
to access navigation services, but I don't know how can I reference to my pivot, so that I can get the selected index and proceed forward.
Thanks!
Upvotes: 1
Views: 1051
Reputation: 9604
On one level Microsoft's suggestion of putting the ApplicationBar
in App.xaml
is great as it can be referenced from everywhere and would appear to encourage code reuse: however this question highlights the limit to this approach. An application bar is typically used to provide actions which are specific to the current page (or pivot item) and just because the buttons are the same you may not want the exact same code to run in each case.
In this case I think it would better to create a factory method that creates your common ApplicationBar
with the click handlers you specify specific to your page/pivot item. For bonus points put the method in a new class (not App
) so it doesn't get lost in all the boilerplate code there. Call this factory method in your page constructor and remember your ApplicationBar
in your class. For multiple app bars, create them all up front and you can then easily switch between these app bars in your Pivot SelectionChanged code.
The alternative of creating the ApplicationBar in App.xaml
and then retrieving this from the App.xaml.cs
"Resources" ResourceDictionary
in code, modifying the click callbacks, is more complicated in my opinion.
I wish they'd done a better job of implementing the ApplicationBar
so people wouldn't want to do this. I've found that using the ApplicationBar forces you to add code to your Page.xaml.cs
even if you use a framework like MVVM Light. This is still OK in MVVM as it's UI specific code that belongs in the View, but it makes things inconsistent if you're using ICommand
everywhere else. Last time I decided it was better to create the entire ApplicationBar
in code rather than hack this kind of thing via App.xaml.cs
.
Update: There is a UserVoice request for a data bindable ApplicationBar.
Upvotes: 3
Reputation: 5325
Using MVVM you SHOULDN'T do this:
((PageType)Application.Current.RootVisual).PivotControl. //Blah
PageType is whatever type PhoneApplicationFrame is that contains your PivotControl. If this doesn't work you need a Property in the RootVisual
public Pivot MyPivot
{
get
{
return PivotControl;
}
}
((PageType)RootVisual).MyPivot. //Blah
Upvotes: 3