从Stepper Xamarin.form向标签发送值

时间:2017-10-01 19:24:25

标签: c# xamarin xamarin.ios xamarin.android mobile-development

您好,这是第一次使用Xamarin。我想要做的是修改此应用程序以使用ListView,但首先我想学习如何从步进器获取值并将其打印到标签。

我知道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;
    }
}

}

2 个答案:

答案 0 :(得分:0)

这是接近它的一种方法 - 一种“强力”方法。或者,你可以使用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();
}; 

答案 1 :(得分:0)

确定。我会试着用它过期。我能用这行代码找到答案。我在stepper.ValueChanged + =(sender,e)=&gt;

之后输入
 valuelabel.Text = String.Format(" {0:F1}", e.NewValue);