如何将文本框文本数据传输到另一个页面? Windows Phone 8.1

时间:2014-07-11 14:20:18

标签: c# windows-phone-8.1

我的应用中有两个页面。第一个是MainPage,第二个是SettingsPage。我的设置页面中有一个文本框。我想保存此文本框文本并发送到MainPage。

这里是新的例子。现在它正在工作,但我无法保存SettingsPage中的textBox1.text。当我浏览其他页面时,它会进行清理。

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

4 个答案:

答案 0 :(得分:4)

在WP8中页面之间发送导航参数的标准方法是使用

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

然后在导航到的页面上检查OnNavigatedTo()方法上的参数。

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

对于Windows Phone 8.1,您不再使用URI进行导航。方法是使用:

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

然后在您导航到的页面的loadstate上,您可以使用以下方式获取数据:

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

希望这有帮助。

答案 1 :(得分:1)

您也可以使用PhoneApplicationService执行此操作。设置这样的值,

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

现在,您可以使用键值来调用该值。得到这样的值,

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

答案 2 :(得分:1)

在Windows Phone 8.1中,您可以将值作为Frame.Navigate方法的参数传输。

例如,您想传输yourType的值:

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

并在目标页面中获取它:

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

价值就是你想得到的。

答案 3 :(得分:0)

我用于此目的的最简单方法是在主页面中声明一个公共静态类,以便您可以在应用程序页面的任何位置使用SAME类方法,如下所示:

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

然后你可以从设置页面的文本框中给它一个值,如下所示:

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

然后将此值返回到主页面中的另一个文本框,如下所示:

TextBoxInMainPage.Text = MyClass.MyString;