ArdaZeytin
ArdaZeytin

Reputation: 1324

How to transfer textbox text data to another page ? Windows Phone 8.1

There are two pages in my app. First one is MainPage and second one is SettingsPage There is one textbox in my settings page. I want to save this textbox text and send to MainPage.

Here the new example. Now its working but I can't save the textBox1.text which is in the SettingsPage . It's cleaning when I navigate other page.

public sealed partial class MainPage : Page
{

    private NavigationHelper navigationHelper;

    public MainPage()
    {
        this.InitializeComponent();
        progRing.IsActive = true;
        Window.Current.SizeChanged += Current_SizeChanged;
        this.NavigationCacheMode = NavigationCacheMode.Required;
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
    }

    public NavigationHelper NavigationHelper
    {

        get { return this.navigationHelper; }
    }

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        this.txtBoxNotification.Text = (string)e.NavigationParameter;
    }

    private void btnNotification_Click(object sender, RoutedEventArgs e)
    {
        webView.Navigate(new Uri("http://teknoseyir.com/u/" + txtBoxNotification ));
    }

Upvotes: 0

Views: 2925

Answers (4)

Kira
Kira

Reputation: 173

The best easiest way I used for this is to declare a public static class in the main page so you can use the SAME class methods anywhere in your app pages as like this :

public static class MyClass
{
    public static string MyString = null;
}

then you can give it a value from a text box from the settings page as like this :

PhoneApp.MainPage.MyClass.MyString = TextBoxInSettings.Text;

then give this value back to another text box in the main page as like this :

TextBoxInMainPage.Text = MyClass.MyString;

Upvotes: 0

Chris Shao
Chris Shao

Reputation: 8231

In Windows Phone 8.1, you can transfer values as parameter of Frame.Navigate method.

For example, you want to transfer yourValue of yourType:

Frame.Navigate(typeof(TargetPage), yourValue); 

and get it in target page:

protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
    var value = navigationParameter as yourType;
}

the value is what you want to get.

Upvotes: 1

Balasubramani M
Balasubramani M

Reputation: 8378

You could also do this by using PhoneApplicationService. Set a value like this,

string str = textBox.Text;
PhoneApplicationService.Current.State["TextBoxValue"] = str;

Now you can call that value whereever you want using it key value. And get a value like this,

textblock.Text = PhoneApplicationService.Current.State["TextBoxValue"] as String;

Upvotes: 1

JayDev
JayDev

Reputation: 1360

The standard way to send navigation parameters between pages in WP8 is to use

NavigationService.Navigate(new Uri("/MainPage.xaml?text=" + textBox1.Text, UriKind.Relative));

Then check for the parameter on the OnNavigatedTo() Method on the page you have navigated to.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string settingsText = NavigationContext.QueryString["text"];
}

For Windows Phone 8.1 you no longer navigate using a URI. The approach is to use:

this.Frame.Navigate(typeof(MainPage), textBox1.Text);

Then on the loadstate for the page you are navigating to you can get the data by using:

private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    string text = e.NavigationParameter as string;
}  

Hope this helps.

Upvotes: 4

Related Questions