How do I send variables to another page on Windows Phone 8?

Asked

Viewed 170 times

4

I tried to follow that method, but there is an error in Mainpage, saying that the NavigationService does not exist in the current context.

In Blankpage, the same error occurs in NavigationContext.

How can I fix this? Need to import some library, as it is on Android?

From what I understand, this method is to send strings. How to send type variables int and double?

Follows 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();
        }
    }
}

1 answer

2


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

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

For a "Windows Phone app"

Access the localSettings:

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

Example to add a bool:

localSettings.Values["saveLogin"] = true;

or

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

Read the information:

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

To check if it exists:

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

For a "Windows Phone Silverlight"

Example to add a 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 check if the information exists before accessing it:

IsolatedStorageSettings.ApplicationSettings.Contains("saveLogin")

Naturally, you can do the same with int and double. I’ve used this resource quite a lot in this small project, if you want to take a closer look.

  • Visual Studio presents this error: "The name "Isolatedstoragesettings" does not exist in the current context."

  • @Edeiltonso, it must be because your application is not marked as an application "Windows Phone Silverlight". I will edit my reply.

  • Thank you very much, the first method worked perfectly! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.