2016-04-14 4 views
-1

こんにちは私は、ボタンをクリックするとバスケットに整数値を渡そうとしています。私はこのメソッドを使って文字列を渡すことに成功しましたが、数値ではできませんでした。ボタンをクリックすると、ログオーダーからバスケットに移動すると、バスケットページのテキストボックスに数字「1」が表示されます。代わりに、私が必要としていたデザインで0に設定するたびに、テキストボックスに0が表示されます。そうしないと、プログラムを実行するときにエラーが表示されます。app.csを使用して別のページに整数を渡すことはできません。Windows phone

はいテキストボックスを整数に変換し、テキストボックスの値を「0」に設定しました。

私はapp.cs.で変数 "storevalue"を初期化しました。

これは、次のページのテキストボックスに表示される整数を初期化するボタンを持つログオーダーのコードです。最初のテキストボックスに表示に整数を示すべきである2ページ目の

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

}

コード...

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 basket : Page 
{ 
    App app = Application.Current as App; 
    public basket() 
    { 
     this.InitializeComponent(); 
    } 

    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" 
    } 

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

    private void textBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 

    } 
} 
} 
+1

ありませんあなたの希望する値に。あなたは何らかのバインディングをしていますか? –

答えて

1

適切にこれを行うには、あなたはシングルトンスコープでサービスを持っている必要があります文字列のデータを取得します。 dependency injectionを使用すると、シングルトンスコープで簡単にサービスを取得できます。次のサービスがシングルトンスコープ内にあると仮定すると、あなたのページまたはあなたのview modelにそれを注入することができます。

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

は、その後、あなたのビューまたは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) 
    { 

    } 
} 
0

保護されたオーバーライド無効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(); 
    } 
0

てみてください好きです: Frame.Navigate(typeof(logorder), yourParameter); 、その後、ページナビゲーションにパラメータを確認してください。あなたのコードでは、あなたがTextBox.Textを設定するのです

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

関連する問題