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