Show applicationbar menu programmatically (wp7)

2019-02-14 23:40发布

问题:

I have a wp7 with some buttons in the application bar. When each button is pressed I change the menuItems of the application bar's menu. After this, I want to automatically open the menu when an application bar button is pressed.

But it seems that the SDK doesn't allow me to do that.

Do you know any work around?

I was thinking, if the above is not possible, to simulate a user finger click at the bottom right corner of the screen to open the menu. Any ideas on that?

Thanx in advance

回答1:

As far as I'm aware this capability has not been exposed as yet. It wasn't possible during beta and I've not noticed anything that's changed since that would allow it. You could always comment on the their suggestions forum or raise it on connect (vs/wpdt).



回答2:

It's possible to change the Application Bar Menu Items in response to an Icon Button click as demonstrated in the code below.

There isn't a way to forcibly open (or close) the application bar through code through.

It also isn't possible to simulate a finger click on the application bar as this isn't part of the actual page. Note that even if possible any click would need to be in the top right or bottom left if the device was in a landscape orientation.

Here's some code which demonstrates changing the menu items:

public partial class MainPage : PhoneApplicationPage
{
    private ApplicationBar appbar;
    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        appbar = new ApplicationBar();

        var ib1 = new ApplicationBarIconButton(new Uri("/images/one.png", UriKind.Relative)) { Text = "Option one" };
        ib1.Click += new EventHandler(ShowMenuOption1);

        var ib2 = new ApplicationBarIconButton(new Uri("/images/two.png", UriKind.Relative)) { Text = "Option two" };
        ib2.Click += new EventHandler(ShowMenuOption2);

        appbar.Buttons.Add(ib1);
        appbar.Buttons.Add(ib2);

        // Show menu option 1 as default
        DisplayMenuOption1();

        this.ApplicationBar = appbar;
    }

    private void DisplayMenuOption1()
    {
        appbar.MenuItems.Clear();

        var itemA = new ApplicationBarMenuItem("AAAA");
        var itemB = new ApplicationBarMenuItem("BBB");

        appbar.MenuItems.Add(itemA);
        appbar.MenuItems.Add(itemB);
    }

    private void DisplayMenuOption2()
    {
        appbar.MenuItems.Clear();

        var itemC = new ApplicationBarMenuItem("CCCC");
        var itemD = new ApplicationBarMenuItem("DDDD");

        appbar.MenuItems.Add(itemC);
        appbar.MenuItems.Add(itemD);
    }

    private void ShowMenuOption2(object sender, EventArgs e)
    {
        DisplayMenuOption2();
    }

    private void ShowMenuOption1(object sender, EventArgs e)
    {
        DisplayMenuOption1();
    }
}