locked
Windows store app tutorial - Type or namespace 'NavigationHelper' could not be found

    Question

  • I was following the tutorial when i reached the part where i needed to add code to the navigationHelper_SaveState method [To save session state] as shown -

    privatevoid navigationHelper_SaveState(object sender, SaveStateEventArgs e) { e.PageState["greetingOutputText"] = greetingOutput.Text; }

    But there isn't any method by that name in my file. When i add it manually i get the error described in the header.

    Am i missing something or is it just one of those Windows 8 VS 8.1 changes?

    Thanks in advance,

    Jay


    Sunday, May 04, 2014 5:06 PM

Answers

  • Are you writing a Windows 8 or a Windows 8.1 app? The navigation code in the templates changed between the ones generated in Visual Studio 2012 for Windows 8 and Visual Studio 2013 for Windows 8.1. See the note in the tutorial:

    Important  This tutorial is intended for use with  Microsoft Visual Studio 2013 and  Windows 8.1.  Parts of it will not work correctly with Microsoft Visual Studio 2012 and Windows 8.
    --Rob

    Monday, May 05, 2014 1:37 PM
    Owner

All replies

  • Hi,

    Define a NavigationHelper in your MainPage.cs and register the navigationHelper_SaveState event handle in the page:

    public sealed partial class MainPage : Page
        {
    
            private NavigationHelper navigationHelper;
                  public NavigationHelper NavigationHelper
            {
                get { return this.navigationHelper; }
            }
    
    
            public MainPage()
            {
                this.InitializeComponent();
                this.navigationHelper = new NavigationHelper(this);
                this.navigationHelper.LoadState += navigationHelper_LoadState;
                this.navigationHelper.SaveState += navigationHelper_SaveState;
            }
    ...
    }
    

    You can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/jj676795.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, May 05, 2014 3:07 AM
  • That's already in the file. Here's the full content - 

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

    // The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

    namespace HelloWorld
    {
        /// <summary>
        /// A basic page that provides characteristics common to most applications.
        /// </summary>
        public sealed partial class MainPage : Page
        {

            private NavigationHelper navigationHelper;
            private ObservableDictionary defaultViewModel = new ObservableDictionary();

            /// <summary>
            /// This can be changed to a strongly typed view model.
            /// </summary>
            public ObservableDictionary DefaultViewModel
            {
                get { return this.defaultViewModel; }
            }

            /// <summary>
            /// NavigationHelper is used on each page to aid in navigation and 
            /// process lifetime management
            /// </summary>
            public NavigationHelper NavigationHelper
            {
                get { return this.navigationHelper; }
            }


            public MainPage()
            {
                this.InitializeComponent();
                this.navigationHelper = new NavigationHelper(this);
                this.navigationHelper.LoadState += navigationHelper_LoadState;
                this.navigationHelper.SaveState += navigationHelper_SaveState;
            }

            /// <summary>
            /// Populates the page with content passed during navigation.  Any saved state is also
            /// provided when recreating a page from a prior session.
            /// </summary>
            /// <param name="navigationParameter">The parameter value passed to
            /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
            /// </param>
            /// <param name="pageState">A dictionary of state preserved by this page during an earlier
            /// session.  This will be null the first time a page is visited.</param>
            private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                // Restore values stored in session state.
                if (e.PageState != null && e.PageState.ContainsKey("greetingOutputText"))
                {
                    greetingOutput.Text = e.PageState["greetingOutputText"].ToString();
                }


                // Restore values stored in app data.
                Windows.Storage.ApplicationDataContainer roamingSettings =
                    Windows.Storage.ApplicationData.Current.RoamingSettings;
                if (roamingSettings.Values.ContainsKey("userName"))
                {
                    nameInput.Text = roamingSettings.Values["userName"].ToString();
                }
            }

            /// <summary>
            /// Preserves state associated with this page in case the application is suspended or the
            /// page is discarded from the navigation cache.  Values must conform to the serialization
            /// requirements of <see cref="SuspensionManager.SessionState"/>.
            /// </summary>
            /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
            private void SaveState(object sender, SaveStateEventArgs e)
            {
                e.PageState["greetingOutputText"] = greetingOutput.Text;

            }

            #region NavigationHelper registration

            /// The methods provided in this section are simply used to allow
            /// NavigationHelper to respond to the page's navigation methods.
            /// 
            /// Page specific logic should be placed in event handlers for the  
            /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
            /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
            /// The navigation parameter is available in the LoadState method 
            /// in addition to page state preserved during an earlier session.

            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                navigationHelper.OnNavigatedTo(e);
            }

            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                navigationHelper.OnNavigatedFrom(e);
            }

            #endregion

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                greetingOutput.Text = "Hello, " + nameInput.Text + "!";
            }

            private void NameInput_TextChanged(object sender, TextChangedEventArgs e)
            {
                Windows.Storage.ApplicationDataContainer roamingSettings =
                    Windows.Storage.ApplicationData.Current.RoamingSettings;
                roamingSettings.Values["userName"] = nameInput.Text;
            }
        }
    }

    =================================================

    I also get the following errors:

    Error 1 'HelloWorld.MainPage.NavigationHelper' is a 'property' but is used like a 'type'
    Error 2 The type or namespace name 'ObservableDictionary' could not be found (are you missing a using directive or an assembly reference?)

    Error 7 The name 'navigationHelper_SaveState' does not exist in the current context
    Error 8 The type or namespace name 'LoadStateEventArgs' could not be found (are you missing a using directive or an assembly reference?)
    Error 9 The type or namespace name 'SaveStateEventArgs' could not be found (are you missing a using directive or an assembly reference?)

    Monday, May 05, 2014 1:29 PM
  • Are you writing a Windows 8 or a Windows 8.1 app? The navigation code in the templates changed between the ones generated in Visual Studio 2012 for Windows 8 and Visual Studio 2013 for Windows 8.1. See the note in the tutorial:

    Important  This tutorial is intended for use with  Microsoft Visual Studio 2013 and  Windows 8.1.  Parts of it will not work correctly with Microsoft Visual Studio 2012 and Windows 8.
    --Rob

    Monday, May 05, 2014 1:37 PM
    Owner
  • Yeah that could be it. Gonna download 2013 version now and try.

    Thanks

    Monday, May 05, 2014 3:59 PM