CACuzcatlan
CACuzcatlan

Reputation: 5497

How to programmatically set selected Panorama item in WP7

I'm using a panorama control in a WP7 app. One of the PanoramaItems takes you to another page, which then allows you send an email through the EmailComposeTask. If you don't select to send the email and press the back button, the Panorama returns to the item you last selected. However, if you do select to send an email (and therefore leave the app), it does not return to the previously selected PanoramaItem. Instead, it returns to the first item in the Panorama. I tried keeping track of the selected index and setting it, but I got an error saying the SelectedIndex is not settable. This is confirmed on MSDN documentation http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.panorama.selectedindex%28VS.92%29.aspx

Is there any way to manually set the selected index/item on a panorama? If not, is there a way for it to remember what was selected, even if the user leaves the app to compose an email?

Upvotes: 13

Views: 23082

Answers (6)

gjmwolmarans
gjmwolmarans

Reputation: 286

I'm using this model to change to a pivot when the device goes into landscape view, I'll probably end up extracting the current item to the application state. The panorama is a no-go in landscape orientation.

private int hub_page_index;

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
    base.OnOrientationChanged(e);

    if (panorama.Visibility == Visibility.Visible)
    {
        hub_page_index = panorama.SelectedIndex;
    }
    else if (pivot.Visibility == Visibility.Visible)
    {
        hub_page_index = pivot.SelectedIndex;
    }

    if (e.Orientation == PageOrientation.Landscape
     || e.Orientation == PageOrientation.LandscapeLeft
     || e.Orientation == PageOrientation.LandscapeRight)
    {
    // Display Pivot in Landscape orientation
        pivot.SetValue(Pivot.SelectedItemProperty, pivot.Items[panorama.SelectedIndex]);
        panorama.Visibility = Visibility.Collapsed;
        pivot.Visibility = Visibility.Visible;
    }
    else
    {
        // Display Panorama in Portrait orientation
        panorama.SetValue(Panorama.SelectedItemProperty, panorama.Items[pivot.SelectedIndex]);
        pivot.Visibility = Visibility.Collapsed;
        panorama.Visibility = Visibility.Visible;
    }
}

Upvotes: 1

Elw00t
Elw00t

Reputation: 51

Here is a solution. It does work as expected and does not rearrange your panorama, so your user interface is consistent.

pan.SetValue(Panorama.SelectedItemProperty, panoramaItem);
Panorama temp = pan;
LayoutRoot.Children.Remove(pan);
LayoutRoot.Children.Add(temp);
LayoutRoot.UpdateLayout();

this is not a perfect solution in that it does not slide nicely like panorama should, and it is probably not very efficient, but on the other hand you are not changing the default item so your user interface stays consistent.

Upvotes: 5

Phil
Phil

Reputation: 31

I tested solutions listed here without success. Here is what I did that works like a charm!

PanoramaItem panItem = (PanoramaItem)panorama.Items[1];

panorama.Items.Remove(panItem);

panorama.Items.Insert(0, panItem);

You need to remove the panel from the list and re-inserting it at the desired position!

Upvotes: 3

Mick N
Mick N

Reputation: 14882

You could try the solution posted by Silicon Shark in this thread. It's noted to work, but only on the initial display - which shouldn't be a problem for your requirements of restoring state after tombstoning.

How to programmatically set the visible item in a Panorama control?

You can get the currently active page from the panorama's SelectedIndex property.

Unfortunately setting DefualtItem is only an approximation to solving this problem, which you may have discovered already.

Edit: Be aware that setting DefaultItem, changes which page of the panorama is the first page. It's a subtle difference, but you will see how it matters looking at the positioning of the heading and the wrap around of the background image.

Upvotes: 7

Truong Hua
Truong Hua

Reputation: 812

Set new selected item by

pan.SetValue(Panorama.SelectedItemProperty, pan.Items[newSelectedItem]);

However, it work only on the initial so my idea is let the panorama control re-init when we change the selected item. This is my code, just add this after Panorama.SelectedItem changing.

(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Collapsed;
pan.SetValue(Panorama.SelectedItemProperty, pan.Items[(curIndex + 1) % pan.Items.Count]);
pan.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
(pan.Items[curIndex] as PanoramaItem).Visibility = Visibility.Visible;

But there is not transition effect now! Although, you can create your self.

It work great for me, this page also create a effect for sliding right http://xme.im/slide-or-change-panorama-selected-item-programatically

Upvotes: 2

Alastair Pitts
Alastair Pitts

Reputation: 19601

I'm not sure if you can programmatically force an animation to another PanoramaItem, but you can change the Panorama.DefaultItem.

So you might have 3 PanoramaItem's and on the OnNavigatedTo() handler, change the default item via:

panoramaControl.DefaultItem = panoramaControl.Items[indexToSet];

This should help when you recover from a tombstone.

Upvotes: 34

Related Questions