MainPano.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright © 2011-2012 Nokia Corporation. All rights reserved.
  3. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. * Other product and company names mentioned herein may be trademarks
  5. * or trade names of their respective owners.
  6. * See LICENSE.TXT for license information.
  7. */
  8. using System;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Navigation;
  12. using Microsoft.Phone.Controls;
  13. using RSSReader.Model;
  14. namespace RSSReader.Views
  15. {
  16. /// <summary>
  17. /// Main panorama page of the application
  18. /// </summary>
  19. public partial class MainPano : PhoneApplicationPage
  20. {
  21. /// <summary>
  22. /// True when this object instance has been just created, otherwise false
  23. /// </summary>
  24. private bool isNewInstance = true;
  25. /// <summary>
  26. /// Const for identifying panorama index in transient storage
  27. /// </summary>
  28. private static readonly string MAIN_PANORAMA_INDEX = "mainPanoramaIndex";
  29. /// <summary>
  30. /// Member variable for saving and restoring current panorama index
  31. /// </summary>
  32. private int panoramaIndex = -1;
  33. /// <summary>
  34. /// Constructor
  35. /// </summary>
  36. public MainPano()
  37. {
  38. InitializeComponent();
  39. RSSFeedsPanorama.Loaded += new RoutedEventHandler(RSSFeedsPanorama_Loaded);
  40. }
  41. /// <summary>
  42. /// Overridden OnNavigatedTo handler
  43. /// </summary>
  44. protected override void OnNavigatedTo(NavigationEventArgs e)
  45. {
  46. base.OnNavigatedTo(e);
  47. if (isNewInstance)
  48. {
  49. // Restoration of panorama index cannot be done here as panorama items are loaded from a collection
  50. // that might not be ready when OnNavigatedTo() is called. We need to do the restoration upon
  51. // Loaded event. We need to take the panorama index out of State here and store it to member variable
  52. // because State is guaranteed to be accessible only during OnNavigatedTo() and OnNavigatedFrom()
  53. if (State.ContainsKey(MAIN_PANORAMA_INDEX))
  54. {
  55. panoramaIndex = (int)State[MAIN_PANORAMA_INDEX];
  56. }
  57. else
  58. {
  59. panoramaIndex = -1;
  60. }
  61. isNewInstance = false;
  62. }
  63. }
  64. /// <summary>
  65. /// Overridden OnNavigatedFrom handler
  66. /// </summary>
  67. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  68. {
  69. base.OnNavigatedFrom(e);
  70. if (e.NavigationMode != NavigationMode.Back)
  71. {
  72. // Remember the PanoramaItem we were on
  73. State[MAIN_PANORAMA_INDEX] = RSSFeedsPanorama.SelectedIndex;
  74. }
  75. }
  76. /// <summary>
  77. /// Handler for restoring panorama state after tombstoning
  78. /// </summary>
  79. /// <param name="sender"></param>
  80. /// <param name="e"></param>
  81. void RSSFeedsPanorama_Loaded(object sender, RoutedEventArgs e)
  82. {
  83. if (App.WasTombstoned && panoramaIndex != -1 )
  84. {
  85. RSSFeedsPanorama.DefaultItem = RSSFeedsPanorama.Items[panoramaIndex];
  86. panoramaIndex = -1;
  87. }
  88. }
  89. /// <summary>
  90. /// Go to the Subscriptions edit page
  91. /// </summary>
  92. /// <param name="sender"></param>
  93. /// <param name="e"></param>
  94. private void SettingsButton_Click(object sender, RoutedEventArgs e)
  95. {
  96. NavigationService.Navigate(new Uri("/Views/SubscriptionsPage.xaml?pageId=" + RSSFeedsPanorama.SelectedIndex.ToString(), UriKind.Relative));
  97. }
  98. /// <summary>
  99. /// Handler called when user taps one of the RSS feed icons
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. private void Button_Click(object sender, RoutedEventArgs e)
  104. {
  105. Button button = sender as Button;
  106. int pageIndex = RSSFeedsPanorama.SelectedIndex;
  107. PanoramaItem item = RSSFeedsPanorama.SelectedItem as PanoramaItem;
  108. RSSPage page = item.DataContext as RSSPage;
  109. RSSFeed feed = button.DataContext as RSSFeed;
  110. int feedIndex = page.Feeds.IndexOf(feed);
  111. if (feedIndex >= 0) // Act only on buttons on current page
  112. {
  113. NavigationService.Navigate(new Uri("/Views/FeedPivot.xaml?id=" + feedIndex.ToString() + "&page=" + pageIndex.ToString(), UriKind.Relative));
  114. }
  115. }
  116. }
  117. }