c# - xamarin 形成如何从代码隐藏填充选择器

标签 c# xamarin.forms picker

大家晚上好,

学习 Xamarin 表单..尝试添加带有数值的选择器...(使用 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource )

我已经使用此页面上的示例从 View 中填充选择器...效果很好...但是我想从后面的代码填充选择器...

<---XAML--->
      <Picker Grid.Column="4"  Grid.Row="2" ItemsSource="{Binding pickerSource}"/>

<---c#---->
         var pickerList = new List<string>();
                pickerList.Add("1");
                pickerList.Add("2");
                pickerList.Add("3");
                pickerList.Add("4");
                pickerList.Add("5");
                pickerList.Add("6");
                pickerList.Add("7");
                pickerList.Add("8");
                pickerList.Add("9");
                pickerList.Add("10");

                var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
                pickerSource.ItemsSource = pickerList;

选择器出现在应用程序上,但选择时,它没有填充任何值...为什么这个绑定(bind)不正确?

谢谢

另外...作为旁注,如果有人知道一个包含所有数值的工具,而不是我手动用 1,2,3 等填充它......

再次感谢


感谢@Jason 的回复...从这里我进行了以下操作:

---xaml--

<Picker Grid.Column="4"  Grid.Row="2" ItemsSource="{Binding pickerSource}"/>

---c#----

public List<string> pickerSource { get; set; }

 public void PopulateQuantityPicker()
        {
            var pickerList = new List<string>();
            pickerList.Add("1");
            pickerList.Add("2");
            pickerList.Add("3");
            pickerList.Add("4");
            pickerList.Add("5");
            pickerList.Add("6");
            pickerList.Add("7");
            pickerList.Add("8");
            pickerList.Add("9");
            pickerList.Add("10");

            pickerSource = pickerList;

            this.BindingContext = this;
}

选择器位于应用程序上,但未填充,它是空的。 当我点击它时,我得到以下信息:

(代码还调用了 PopulateQuantityPicker())

enter image description here

最佳答案

在这里,您将 ItemsSource 绑定(bind)到 pickerSource

<Picker Grid.Column="4"  Grid.Row="2" ItemsSource="{Binding pickerSource}"/>

在您的代码隐藏中,您需要一个名为 pickerSource公共(public)属性。您只能绑定(bind)到公共(public)属性

public List<string> pickerSource { get; set }

// assign the data to your ItemsSource
pickerSource = pickerList;

// also be sure to set the BindingContext
BindingContext = this;

// this is creating a new picker named pickerSource.  You have already done 
// this in your XAML.  This is NOT NEEDED
var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
pickerSource.ItemsSource = pickerList;

如果您想在不使用绑定(bind)的情况下从后面的代码执行此操作,您首先需要为您的控件分配一个x:name

<Picker x:Name="myPicker" Grid.Column="4"  Grid.Row="2" />

然后在后面的代码

myPicker.ItemsSource = pickerList;

关于c# - xamarin 形成如何从代码隐藏填充选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61014275/

相关文章:

c# - 通过 xamarin.forms 中的自定义 UI 将数据添加到用户帐户

ios - 如何区分didFinishPickingMediaWithInfo中的UIImagePickerController

xcode - 在钥匙串(keychain)中找不到有效的 iOS 代码签名 key 。您需要申请一个代码签名证书

c# - 如何等待或知道 Xamarin Forms 中的多个动画何时完成?

swift - 在 Swiftui Picker 滚动列表时触发的事件

ios - UIPickerView 中 UIVIew 的背景颜色设置不正确

c# - YASR - 另一个搜索和替换问题

c# - 如何在 asp.net web 应用程序 (C#) 中制作倒数计时器?

c# - 如何对列表进行排序,既不是升序也不是降序,而是自定义

c# - 在控制台应用程序中处理来自不同项目的依赖项