Xamarin中的可绑定选择器

时间:2017-01-21 12:07:18

标签: xamarin xamarin.forms picker

Xamarin中的Picker Control是否可用于绑定?如果是这样,有人可以帮助如何使用它?我想将Picker控件与来自DB的数据(XAML方法)绑定。

5 个答案:

答案 0 :(得分:4)

可绑定的Picker自2017年1月13日起可用。目前,它包含在2.3.4.184-pre1版本中。 如果要使用它,则必须使用-Pre标志通过nuget安装Xamarin.Forms。或者查看nuget UI中的Prerelease复选框。

  

Install-Package Xamarin.Forms -Pre

然后,您可以将您的收藏绑定到ItemsSource

<Picker
    Title="Select a Color"
    ItemsSource="{Binding Colors}" />

公告:https://blog.xamarin.com/new-xamarin-forms-pre-release-2-3-4-pre1-quality-improvements-bindable-picker

它将在2017年2月之前稳定发布(根据Roadmap

答案 1 :(得分:0)

是的,您可以使用Xamarin Forms中的Picker控件。 请在此链接中查看说明:https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/

谢谢。

答案 2 :(得分:0)

答案 3 :(得分:0)

以下是完整的解决方案:

https://stackoverflow.com/a/42422827/4748204

创建自定义选择器类。

xmlns:local=“clr–namespace:FDNet;assembly=FDNet“
  1. 将XAML引用变量添加到Page。

    <local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
    
  2. 添加控件并绑定属性。

    Outlets = new List<string> { “4G LTE“, “4G Broadband“, “Fiber connection“ };
    
    <local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
    
  3. 现在您可以看到我们的自定义可绑定属性ItemSource =“{Binding Outlets}”)

    1. 演示。

      String sentence = "Hello my name is Bob, and I'm testing the line length in this program.";
      String[] words = sentence.Split();
      
      //Assigning first word here to avoid begining with a space.
      String line = words[0];
      
                  //Starting at 1, as 0 has already been assigned
                  for (int i = 1; i < words.Length; i++ )
                  {
                      //Test for line length here
                      if ((line + words[i]).Length < 10)
                      {
                          line = line + " " + words[i];
                      }
                      else
                      {
                          Console.WriteLine(line);
                          line = words[i];
                      }
                  }
      
                  Console.WriteLine(line);
      
    2. https://hiranpeiris.com/2017/02/24/how-to-add-a-custom-bindable-property-to-xamarin-forms-control/

答案 4 :(得分:0)

(1)绑定选择器:

词典dicobj = new Dictionary();

 dicobj.Add(1, "abc");

dicobj.Add(2, "xyz");





 foreach (var item in dicobj.Values)

  {


 YorPickerName.Items.Add(item);

   } 

(2)然后在选择器的SelectedIndexChanged事件中获取所选值:

  var selval = Service_category.SelectedIndex;

  int value = dicobj.ElementAt(selval).Key;

  var data = Service_category.Items[selval];

  int id = dicval.FirstOrDefault(x => x.Value == data).Key;