2017-10-01 27 views
0

こんにちは、これは初めてXamarinを使用しています。私がしようとしているのは、このアプリケーションをListViewを使用するように変更することですが、最初にステッパーから値を取得してラベルに印刷する方法を学びたいと思います。Stepper Xamarin.formからラベルに値を送信

私はmovieamountがステッパーから選択した値を送信してtotalLabelテキストに送信することは知っていますが、すでにラベル0にラベルを送信し、選択した値を変更する方法はわかりません。選択された正しい量が返されますが、決して画面に印刷されません。 "強引な" 方法 -

public static string movieamount; 
    public static string pickmovie; 
    public static string paymentSelected; 
    public static string dateSelected; 
    public static string timeSelected; 
    public static string totalLabel; 



    public MainPage() 
    { 

     Picker picker = new Picker 
     { 
      Title = "Movies", 

      VerticalOptions = LayoutOptions.CenterAndExpand 
     }; 


     var options = new List<string> { "Kill Bill", "Matrix", "Zombieland", "The Dark Knight", "Terminator", "Apocalypse Now", "Resouvoir dogs", "Horrible Bosses", "The Breakup", "Wedding Crashers", }; 
     picker.SelectedIndexChanged += (sender, e) => 
     { 
      pickmovie = picker.Items[picker.SelectedIndex]; 

     }; 
     foreach (string optionName in options) picker.Items.Add(optionName); 
     //listView.ItemTapped += async (sender, e) => { await DisplayAlert("Tapped", e.Item.ToString() + " was selected.", "OK"); ((ListView)sender).SelectedItem = null; }; 
     //this.Content = listView; 

     Label valuelabel = new Label 
     { 
      Text = "0", 
      FontAttributes = FontAttributes.Bold, 
      FontSize = 30, 
      HorizontalOptions = LayoutOptions.Center 
     }; 

     Stepper stepper = new Stepper 
     { 
      Minimum = 0, 
      Maximum = 10, 
      Increment = 1, 
      HorizontalOptions = LayoutOptions.Center, 
      VerticalOptions = LayoutOptions.CenterAndExpand 

     }; 


     stepper.ValueChanged += (sender, e)=> 
     { 

      movieamount = stepper.Value.ToString(); 

     }; 



     Picker payment = new Picker 
     { 
      Title = "Payment Method", 
      VerticalOptions = LayoutOptions.CenterAndExpand 

     }; 
     var options1 = new List<string> {"Visa", "MasterCard", "AmericanExpress", "Free",}; 

     foreach (string optionName in options1) payment.Items.Add(optionName); 
     payment.SelectedIndexChanged += (sender, e) => 
     { 
      paymentSelected = payment.Items[payment.SelectedIndex]; 
     }; 

     //TimePicker was here 



     Label totalLabel = new Label 

     { 

      HorizontalOptions = LayoutOptions.CenterAndExpand, 
      FontSize = 40, 
      FontAttributes = FontAttributes.Bold | FontAttributes.Italic 


     }; 

     DatePicker datePicker = new DatePicker 
     { 
      Format = "D", 
      VerticalOptions = LayoutOptions.CenterAndExpand, 

     }; 

     //---Handle Inline--- 

     datePicker.DateSelected += (object sender, DateChangedEventArgs e) => 
     { 
      //eventValue.Text = e.NewDate.ToString(); 
      dateSelected = e.NewDate.ToString(); 

     }; 

     TimePicker timePicker = new TimePicker 

     { 
      Format = "T", 
      VerticalOptions = LayoutOptions.CenterAndExpand 

     }; 
     // set inline handler 

     timePicker.PropertyChanged += (sender, e) => 
     { 
      if (e.PropertyName == TimePicker.TimeProperty.PropertyName) 

      { 
       timeSelected = timePicker.Time.ToString(); 
      }; 

     }; 

     Button button = new Button 

     { 
       Text = "Submit", 

       FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Button)), 
       HorizontalOptions = LayoutOptions.CenterAndExpand, 
       VerticalOptions = LayoutOptions.Fill 

     }; 

     button.Clicked += (sender, args) => 
     { 
      totalLabel.Text = "You have ordered " + movieamount + " " + 
      pickmovie + " \n You will be paying with " + paymentSelected + " " + 
      "Your delivery will be delivered at " + dateSelected + " " + timeSelected; 


     }; 





     StackLayout stackLayout = new StackLayout 
     { 
      Children = 

      { 
       picker, 
       payment, 
       valuelabel, 
       stepper, 
       datePicker, 
       totalLabel, 
       timePicker, 
       button, 


      } 

     }; 
     BackgroundColor = Color.Yellow; 
     this.Content = stackLayout; 
    } 
} 

}

答えて

0

これにアプローチする1つの方法です。オプションで、MVVMとdatabindingを使用することができ、それは、より高度であり、あなたが

// declare this method outside of the constructor 
private void UpdateLabel() { 

    totalLabel.Text = "You have ordered " + movieamount + " " + 
    pickmovie + " \n You will be paying with " + paymentSelected + " " + 
    "Your delivery will be delivered at " + dateSelected + " " + 
    timeSelected; 
} 

// then modify these existing handlers to call UpdateLabel 
stepper.ValueChanged += (sender, e)=> 
{ 
    movieamount = stepper.Value.ToString(); 
    UpdateLabel(); 
}; 

// you should also call UpdateLabel in the other handlers that update values 
button.Clicked += (sender, args) => 
{ 
    UpdateLabel(); 
}; 
0

OKこの時点でかみ切るしたいよりも、それ以上であってもよいです。私はそれを試し、満了するでしょう。このコード行で私の答えを見つけることができました。私がステッパーの直後に入力したもの.ValueChanged + =(送信者、e)=>

valuelabel.Text = String.Format(" {0:F1}", e.NewValue); 
関連する問題