【问题标题】:How to use a string between two page in windowsphone如何在windowsphone的两个页面之间使用字符串
【发布时间】:2015-02-08 17:17:27
【问题描述】:

我有两页(主页/第二页)。我在 Mainpage.xaml.cs 中创建了一个字符串,但我想在第二页中使用该字符串。 我该怎么做?

【问题讨论】:

  • 这真的是 xpages 问题吗?
  • 我删除了 xpages 并添加了 xaml
  • this question中的示例代码

标签: xaml windows-phone-8


【解决方案1】:

您可以通过使用另一个静态类作为其成员的静态字符串来做到这一点,同样:

public static class stringWrapper{
 public static string message{get; set;}
}

然后在MainPage.xaml.cs中设置,然后在secondPage.xaml.cs中获取它的值,同理:

MainPage.xaml.cs:

stringWrapper.message = "Whatever your string is";

secondPage.xaml.cs:

string msg = stringWrapper.message;

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    你可以有不同的解决方案:

    1. 如@cybertronhac 所述,将字符串作为包装类中的公共静态属性。
    2. 使用Messenger 模式(例如来自 Mvvm Light)在请求时将字符串发送到第二页。
    3. 将SecondPage中需要绑定字符串对象的对象的DataContext改为MainPage.xaml.cs类

    【讨论】:

      【解决方案3】:

      在 App.xaml.cs 中,在构造函数“public App()”之外声明一个变量(假设为“a”)。将其设为公开静态。然后在 Mainpage.xaml.cs 中将您的字符串放入该变量“a”中。由于您可以从项目的任何页面访问“a”,因此您将获得所需的字符串。我希望这将有所帮助。下面给出一个示例。

      在 App.xaml.cs 页面中:

      公共静态字符串 a = "";

      在 MainPage.xaml.cs 页面中:

      App.a = "我叫内马尔。";

      现在您可以在第二页的控制台中显示您的字符串,也可以将其用于其他目的。您有价值的字符串在变量“a”中,可以通过编写“App.a”来访问:

      Debug.WriteLine("" + App.a);

      【讨论】:

        猜你喜欢
        • 2018-12-05
        • 2015-09-04
        • 1970-01-01
        • 2020-06-02
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 2012-09-17
        • 2019-10-26
        相关资源
        最近更新 更多