Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Stepper Xamarin.form向标签发送值_C#_Xamarin_Xamarin.ios_Xamarin.android_Mobile Development - Fatal编程技术网

C# 从Stepper Xamarin.form向标签发送值

C# 从Stepper Xamarin.form向标签发送值,c#,xamarin,xamarin.ios,xamarin.android,mobile-development,C#,Xamarin,Xamarin.ios,Xamarin.android,Mobile Development,你好,这是第一次使用Xamarin。我试图做的是修改此应用程序以使用ListView,但首先我想学习如何从步进器获取值并将其打印到标签上 我知道movieamount会将从步进器中选择的值发送到TotalAbel文本,但我似乎不知道如何将其发送到已为0的标签,并在选择时使其更改值。它确实返回所选的正确数量,但从不打印到屏幕上 public static string movieamount; public static string pickmovie; public s

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

我知道movieamount会将从步进器中选择的值发送到TotalAbel文本,但我似乎不知道如何将其发送到已为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;
    }
}
公共静态字符串movieamount;
公共静态字符串;
公共静态字符串paymentSelected;
选择公共静态字符串日期;
公共静态字符串;
公共静态字符串标签;
公共主页()
{
选择器选择器=新选择器
{
Title=“电影”,
垂直选项=LayoutOptions.CenterAndExpand
};
var options=新列表{“杀死比尔”、“矩阵”、“僵尸世界”、“黑暗骑士”、“终结者”、“现在的启示录”、“资源犬”、“恐怖老板”、“分手”、“婚礼破坏者”};
picker.SelectedIndexChanged+=(发件人,e)=>
{
pickmovie=picker.Items[picker.SelectedIndex];
};
foreach(选项中的字符串optionName)picker.Items.Add(optionName);
//listView.ItemTapped+=async(sender,e)=>{await DisplayAlert(“Tapped”,e.Item.ToString()+“已被选中。”,“确定”);((listView)sender)。SelectedItem=null;};
//this.Content=listView;
标签值标签=新标签
{
Text=“0”,
FontAttributes=FontAttributes.Bold,
FontSize=30,
水平选项=布局选项。居中
};
步进电机步进电机=新步进电机
{
最小值=0,
最大值=10,
增量=1,
水平选项=布局选项。中心,
垂直选项=LayoutOptions.CenterAndExpand
};
stepper.ValueChanged+=(发送器,e)=>
{
movieamount=stepper.Value.ToString();
};
取货人付款=新取货人
{
Title=“付款方式”,
垂直选项=LayoutOptions.CenterAndExpand
};
var选项1=新列表{“Visa”、“万事达卡”、“AmericanExpress”、“免费”、};
foreach(选项1中的字符串optionName)payment.Items.Add(optionName);
payment.SelectedIndexChanged+=(发件人,e)=>
{
paymentSelected=payment.Items[payment.SelectedIndex];
};
//时间选择器在这里
标签标签=新标签
{
水平选项=LayoutOptions.CenterAndExpand,
FontSize=40,
FontAttributes=FontAttributes.Bold | FontAttributes.Italic
};
DatePicker DatePicker=新的日期选择器
{
Format=“D”,
垂直选项=LayoutOptions.CenterAndExpand,
};
//---内联句柄---
datePicker.DateSelected+=(对象发送者,DateChangedEventArgs e)=>
{
//eventValue.Text=e.NewDate.ToString();
dateSelected=e.NewDate.ToString();
};
计时器选择器计时器选择器=新计时器选择器
{
Format=“T”,
垂直选项=LayoutOptions.CenterAndExpand
};
//设置内联处理程序
timePicker.PropertyChanged+=(发送方,e)=>
{
if(e.PropertyName==TimePicker.TimeProperty.PropertyName)
{
timeSelected=timePicker.Time.ToString();
};
};
按钮=新按钮
{
Text=“提交”,
FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Button)),
水平选项=LayoutOptions.CenterAndExpand,
垂直选项=布局选项。填充
};
按钮。单击+=(发件人,参数)=>
{
totalabel.Text=“您已订购”+movieamount+“”+
pickmovie+“\n您将使用“+paymentSelected+”付款+
“您的交货将在“+选定日期+”+选定时间交付;
}; 
StackLayout StackLayout=新的StackLayout
{
孩子们=
{
皮克,
付款
valuelabel,
步进机,
日期选择器,
阿贝尔,
时间选择器,
按钮
}
};
背景颜色=颜色。黄色;
this.Content=stackLayout;
}
}

}

这是一种方法——一种“暴力”方法。或者,您可以使用MVVM和,但这是更高级的,并且可能比您现在想要的要多

// 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();
}; 

这是一种方法——一种“暴力”方法。或者,您可以使用MVVM和,但这是更高级的,并且可能比您现在想要的要多

// 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();
}; 

嗯。我会试试看,然后用它来结束。我能用这行代码找到我的答案。我在stepper.ValueChanged+=(发件人,e)=>


嗯。我会试试看,然后用它来结束。我能用这行代码找到我的答案。我在stepper.ValueChanged+=(发件人,e)=>