How to send variables to another page in Windows Phone 8?

4

I tried to follow this method , but an error occurs in MainPage, saying NavigationService does not exist in the current context.

In BlankPage, the same error occurs in NavigationContext .

How can I fix this? Do I need to import some library, just like it is on Android?

As far as I understand, this method is for sending strings . How do I send variables of type int and double ?

Follow the code:

MainPage.xaml:     

<Grid>
    <TextBlock x:Name="helloMessage" Margin="10,132,10,0" TextWrapping="Wrap" Text="Hello" VerticalAlignment="Top" FontSize="26.667" FontFamily="Segoe UI Light"/>
    <Button x:Name="sayHelloButton" Content="Say Hello!" HorizontalAlignment="Stretch" Margin="10,64,10,0" VerticalAlignment="Top" Click="sayHelloButton_Click"/>
    <TextBox x:Name="nameField" Margin="10,10,10,0" TextWrapping="Wrap" Text="Enter your name..." VerticalAlignment="Top" Height="39"/>
    <Button x:Name="button_GoToPage" Content="Go to page" HorizontalAlignment="Right" Margin="0,159,10,0" VerticalAlignment="Top" Click="button_GoToPage_Click"/>
</Grid>

MainPage.xaml.cs:

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

namespace MyApp
{
    public sealed partial class MainPage : Page
    {

        string texto1 = "Hello World!";

        public MainPage()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void sayHelloButton_Click(object sender, RoutedEventArgs e)
        {
            helloMessage.Text = "Hello, "+nameField.Text+"!";
        }

        private void button_GoToPage_Click(object sender, RoutedEventArgs e)
        {

            NavigationService.Navigate(new Uri("/BlankPage.xaml?msg=" + nameField.Text, UriKind.Relative));
;       }
    }
}

BlankPage.xaml:

<Page
x:Class="MyApp.BlankPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <AppBarButton x:Name="appBarButton" HorizontalAlignment="Left" Icon="Back" Label="" Margin="-10,-2,0,0" VerticalAlignment="Top" Click="appBarButton_Click"/>
    <TextBlock x:Name="textName1" Margin="10,98,10,492" TextWrapping="Wrap" Text="(vazio)" FontSize="32" HorizontalAlignment="Center"/>

</Grid>

BlankPage.xaml.cs:

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

namespace MyApp
{
    public sealed partial class BlankPage : Page
    {

        public BlankPage()
        {
            this.InitializeComponent();
            /// Manter página armazenada em cache, mesmo utilizando o botão voltar
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string msg = "";

            if (NavigationContext.QueryString.TryGetValue("msg", out msg))

                textName1.Text = msg;
        }

        private void appBarButton_Click(object sender, RoutedEventArgs e)
        {
            Frame.GoBack();
        }
    }
}
    
asked by anonymous 17.08.2015 / 00:25

1 answer

2

You can use System.IO.IsolatedStorage (for a Windows Phone Silverlight application) or Windows.Storage.ApplicationData.Current.LocalSettings. (for a Windows Phone application).

Both can be used on any page. I know it seems strange that you include information that needs to be passed from one page to another in one of these Storage , which are global application access, but I have had this same question in the past and recommended this way.

For a "Windows Phone" application

Access localSettings :

var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

Example to add bool :

localSettings.Values["saveLogin"] = true;

or

localSettings.Values.Add("saveLogin", true);

Read the information:

var value = localSettings.Values["saveLogin"];

To verify that it exists:

if(localSettings.Values.ContainsKey("saveLogin"))

For a "Windows Phone Silverlight" application

Example to add bool :

IsolatedStorageSettings.ApplicationSettings.Add("saveLogin", false);
IsolatedStorageSettings.ApplicationSettings.Save();

And to get the information saved, already on another page:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
bool saveLogin = (bool)settings["saveLogin"];

To verify that the information exists before accessing it:

IsolatedStorageSettings.ApplicationSettings.Contains("saveLogin")

Of course, you can do the same with int and double . I used this feature a lot in this small project if you want to take a closer look.

    
17.08.2015 / 03:04