user3847141
user3847141

Reputation: 253

How do i handle the event of a selected pivot item in Windows Phone

I have an app with 3 PivotItems and an ApplicationBar. I want to hide the ApplicationBar when the PivotItems 2 and 3 are selected, and show the ApplicationBar when the first PivotItem is selected.

Upvotes: 3

Views: 3204

Answers (2)

Balasubramani M
Balasubramani M

Reputation: 8338

I don't know why this question has been down voted. The question sense may be wrong and it can be edited. I got the solution for you @user3847141. Here you go.

PivotItem pivot = null;
    private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ApplicationBar appBar = new ApplicationBar();
        ApplicationBarIconButton appBarIconButton = new ApplicationBarIconButton();
        pivot = (PivotItem)(sender as Pivot).SelectedItem;
        switch(pivot.Header.ToString())
        {
            case "item1": 
                appBar.Mode = ApplicationBarMode.Default;
                appBarIconButton.IconUri = new Uri("/appbar.close.png", UriKind.RelativeOrAbsolute);
                appBarIconButton.Text = "Close";
                appBar.Buttons.Add(appBarIconButton);
                this.ApplicationBar = appBar;
                break;
            case "item2":
                appBar.Mode = ApplicationBarMode.Minimized; // To minimize AppBar
                appBar = null; // Delete Application Bar                     
                this.ApplicationBar = appBar;
                break;
            case "item3":
                appBar.Mode = ApplicationBarMode.Minimized;
                appBar = null;                   
                this.ApplicationBar = appBar;
                break;
        }
    }

You can achieve this through Selection_Changed event in Pivot. Hope it helps.

Upvotes: 5

Kasun Kodagoda
Kasun Kodagoda

Reputation: 4024

First give names for your pivot items then you need to create a PivotCallbacks class

class PivotCallbacks
{
     public Action Initiate { get; set; }
     public Action OnAvtivated { get; set; }
     public Action<CancelEventArgs> OnBackKeyPress { get; set; }
}

Then in your page constructor add the following.

public MainPage()
{
    InitializeComponent();

    _callbacks = new Dictionary<object, PivotCallbacks>();
    _callbacks[pivotItem1] = new PivotCallbacks
    {
        Initiate = ShowAppbar,
        OnAvtivated = ShowAppbar

    };
    _callbacks[pivotItem2] = new PivotCallbacks
    {
        OnAvtivated = HideAppbar
    };
    _callbacks[pivotItem3] = new PivotCallbacks
    {
        OnAvtivated = HideAppbar
    };

    foreach (var callbacks in _callbacks.Values)
    {
        if (callbacks.Initiate != null)
        {
            callbacks.Initiate();
        }
    }
}

(here ShowAppbar and HideAppbar are the methods that has the logic to show/hide the app bar)

Then when ever the selection is changed in the current pivot item you need to call the appropriate function attached to it. to do that in the Selection changed event of the pivot item add the following code.

private void pivotItemMomento_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    PivotCallbacks callbacks;
    if (_callbacks.TryGetValue(momentoPivot.SelectedItem, out callbacks) && (callbacks.OnAvtivated != null))
    {
           callbacks.OnAvtivated();
    }
}

So when onActivated is called the methods associated to that Action is called in the appropriate pivot item. You can do the same to the other actions (eg. OnBackKeyPress) as well. Hope this helps.

Upvotes: 1

Related Questions