简体   繁体   English

如何处理Windows Phone中所选枢轴项的事件

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

I have an app with 3 PivotItem s and an ApplicationBar . 我有一个包含3个PivotItem和一个ApplicationBar I want to hide the ApplicationBar when the PivotItem s 2 and 3 are selected, and show the ApplicationBar when the first PivotItem is selected. 我想在选择PivotItem s 2和3时隐藏ApplicationBar ,并在选择第一个PivotItem时显示ApplicationBar

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. 我为你找到了解决方案@ 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. 您可以通过Pivot中的Selection_Changed事件来实现此目的。 Hope it helps. 希望能帮助到你。

First give names for your pivot items then you need to create a PivotCallbacks class 首先为您的透视项目命名,然后您需要创建一个PivotCallbacks类

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) (此处ShowAppbar和HideAppbar是具有显示/隐藏应用栏的逻辑的方法)

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. 因此,当调用onActivated时,在相应的pivot项中调用与该Action关联的方法。 You can do the same to the other actions (eg. OnBackKeyPress) as well. 您也可以对其他操作(例如OnBackKeyPress)执行相同操作。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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