【问题标题】:How to transfer textbox text data to another page ? Windows Phone 8.1如何将文本框文本数据传输到另一个页面?视窗电话 8.1
【发布时间】:2015-03-16 15:20:27
【问题描述】:

我的应用中有两个页面。第一个是 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 ));
    }

【问题讨论】:

  • 每当您向前导航到一个新页面时,它都会创建一个新的页面实例,因此它不会存储它。如果您需要永久存储值,则需要考虑使用静态值,或创建视图模型来存储数据

标签: c# windows-phone-8.1


【解决方案1】:

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

然后在您正在导航到的页面的加载状态上,您可以使用以下方法获取数据:

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

希望这会有所帮助。

【讨论】:

  • 它在 NavigationService 给出了红线。错误 = 当前上下文中不存在。
  • 您是为 WP8 还是 WP8.1 创作?
  • 我正在为 WP8.1 创建。我非常感谢你。现在 LoadStateEventArgs 给出红线。我会发布代码。它适用于其他页面,但我在 MainPage 上遗漏了一些东西(我的应用程序的第一页,你知道标准的第一页是自动创建的。)。
  • LoadStateEventArgs 位于 YOURAPPNAME.Common 命名空间中。如果您还没有,请尝试添加“使用 YOURAPPNAME.Common;”在页面顶部供您使用。
【解决方案2】:

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

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

现在您可以使用键值在任何您想要的地方调用该值。并得到这样的值,

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

【讨论】:

    【解决方案3】:

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

    比如你想传递yourType的yourValue:

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

    并在目标页面中获取它:

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

    价值就是你想要得到的。

    【讨论】:

      【解决方案4】:

      我为此使用的最简单的方法是在主页中声明一个公共静态类,这样您就可以在应用页面的任何地方使用相同的类方法,如下所示:

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

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

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

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

      TextBoxInMainPage.Text = MyClass.MyString;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 2018-10-09
        • 2011-11-01
        • 1970-01-01
        • 2017-05-19
        相关资源
        最近更新 更多