【问题标题】:Send value to label from Stepper Xamarin.form从 Stepper Xamarin.form 向标签发送值
【发布时间】:2018-03-12 23:10:22
【问题描述】:

您好,这是第一次使用 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;
    }
}

}

【问题讨论】:

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


    【解决方案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();
    }; 
    

    【讨论】:

      【解决方案2】:

      好的。我会尝试并过期。我能够用这行代码找到我的答案。我在 stepper.ValueChanged += (sender, e)=>

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2019-03-09
        • 1970-01-01
        • 2018-08-08
        • 1970-01-01
        • 2021-05-29
        相关资源
        最近更新 更多