使用app.cs将整数传递到另一个页面而不是work.on windows phone

时间:2016-04-14 22:01:28

标签: c# windows-phone-8.1 windows-phone windows-10

您好我正试图通过点击按钮将整数值传递给篮子。我已成功设法使用此方法传递字符串,但未能使用数字。当我点击按钮时,它应该显示数字" 1"当我从日志顺序导航到篮子时,在篮子页面上的文本框上。相反,文本框每次显示为0,因为我在设计中将其设置为0,否则在运行程序时会显示错误。

是的我已将文本框转换为整数并将文本框值设置为" 0"。

我已经初始化了变量" storevalue"在app.cs。

这是我的日志订单代码,它有一个按钮,用于初始化整数,以显示在下一页的文本框中。

$client->account->messages->create(array( 
    'To' => "+16518675309", 
    'From' => "+14158141829", 
    'Body' => "Check out this awesome image!", 
    'MediaUrl' => "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg",  
));

}

第二页的代码应该显示在第一个文本框中显示整数...

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 icecreamapp
{

public sealed partial class logorder : Page
{
    public logorder()
    {
        this.InitializeComponent();
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)

    {

    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        App app = Application.Current as App; 

        app.storeValue = 1; //initializing variable as 1
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(basket)); //button that navigates to next page
    }
}

3 个答案:

答案 0 :(得分:1)

要正确执行此操作,您应该在singleton范围内拥有一个服务来捕获字符串的数据。您可以使用dependency injection轻松获取单一范围内的服务。假设以下服务属于单一范围,您可以将其注入您的页面或view model

class CartService
{
   int storeValue {get; set;}
}

然后在您的View或ViewModel中设置商店值

public sealed partial class basket : Page
{
    private CartService _cartService;

    public basket(CartService cartService)
    {
        this.InitializeComponent();
        _cartService = cartService;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        App app = Application.Current as App;
        var v = int.Parse(textBox.Text); //converting textbox to integer

        v = _cartService.storeValue; //variable should be displayed in this textbox     and show "1"
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(logorder));
    }

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {

    }
}

答案 1 :(得分:0)

protected override void OnNavigatedTo(NavigationEventArgs e)     {

App app = Application.Current as App;
var v = int.Parse(textBox.Text); //converting textbox to integer

v = app.storeValue; //variable should be displayed in this textbox     and show "1"

}

您在此处执行的操作是从文本框中检索值并将该值更改为存储值。由于您从文本框获取的值不是ReferenceType,因此在分配新值时不会更改。所以喜欢这样做

protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        App app = Application.Current as App;

    textBox.Text    = app.storeValue.ToString(); 
    }

答案 2 :(得分:0)

尝试使用参数导航: Frame.Navigate(typeof(logorder), yourParameter); 然后在页面导航上检查参数:

protected override void OnNavigatedTo(NavigationEventArgs e) { var yourThing = e.Parameter; }