C# Xamarin新型可绑定选择器控件

C# Xamarin新型可绑定选择器控件,c#,xamarin,C#,Xamarin,我对xamarin和c#还很陌生 不知怎么的,它对我不起作用 public class RegistrationPageViewModel : INotifyPropertyChanged { List<string> _countries = new List<string> { "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", };

我对xamarin和c#还很陌生 不知怎么的,它对我不起作用

 public class RegistrationPageViewModel : INotifyPropertyChanged
    {

        List<string> _countries = new List<string>
{
    "Afghanistan",
    "Albania",
    "Algeria",
    "Andorra",
    "Angola",

};
        public List<string> Countries => _countries;

        public event PropertyChangedEventHandler PropertyChanged;




    }
    public partial class RegistrationPage : ContentPage
    {
        RegistrationPageViewModel vm;

        public RegistrationPage()
        {
            InitializeComponent();
            this.BindingContext = vm = new RegistrationPageViewModel();
        }

        private void InitializeComponent()
        {
            throw new NotImplementedException();
        }
    }



    int countriesSelectedIndex;
            public int CountriesSelectedIndex
            {
                get
                {
                    return countriesSelectedIndex;
                }
                set
                {
                    if (countriesSelectedIndex != value)
                    {
                        countriesSelectedIndex = value;

                        // trigger some action to take such as updating other labels or fields
                        OnPropertyChanged(nameof(CountriesSelectedIndex));
                        SelectedCountry = Countries[countriesSelectedIndex];
                    }
                }
            }

        public string SelectedCountry { get; private set; }
    }
公共类注册PageViewModel:INotifyPropertyChanged
{
列表_国家=新列表
{
“阿富汗”,
“阿尔巴尼亚”,
“阿尔及利亚”,
“安道尔”,
“安哥拉”,
};
公开名单国家=>\u国家;
公共事件属性更改事件处理程序属性更改;
}
公共部分类注册页面:ContentPage
{
注册页面视图模型虚拟机;
公共注册页()
{
初始化组件();
this.BindingContext=vm=new RegistrationPageViewModel();
}
私有void InitializeComponent()
{
抛出新的NotImplementedException();
}
}
int Countries选定的索引;
公共整数国家选定索引
{
得到
{
返回国家/地区选择索引;
}
设置
{
if(countriesSelectedIndex!=值)
{
countriesSelectedIndex=数值;
//触发一些要执行的操作,例如更新其他标签或字段
已更改的不动产(国家名称已选定索引);

SelectedCountry=国家[国家/地区SelectedIndex]; } } } 公共字符串SelectedCountry{get;private set;} }
我收到一个错误:“国家”名称在当前上下文中不存在


我做错了什么?是否有人有一个有效的示例

以下是我将如何编写它(使用而不是麻烦于
INotifyPropertyChanged
):

公共类国家
{
公共字符串名称{get;}
公共国家(字符串名称)=>name=name;
公共静态国家阿富汗=新国家(“阿富汗”);
公共静态国家阿尔巴尼亚=新国家(“阿尔巴尼亚”);
公共静态国家阿尔及利亚=新国家(“阿尔及利亚”);
公共静态国家安道尔=新国家(“安道尔”);
公共静态国家安哥拉=新国家(“安哥拉”);
最不发达国家=新名单{阿富汗、阿尔巴尼亚、阿尔及利亚、安道尔、安道尔、安哥拉};
}
[实现属性更改]
公共类注册PageViewModel
{
公共国家/地区SelectedCountry{get;set;}
}
公共类注册页面:ContentPage
{
公共注册页()
{
var vm=new RegistrationPageViewModel();
BindingContext=vm;
var picker=new picker();
foreach(在country.Countries中的var country)picker.Items.Add(country.Name);
picker.SetBinding(picker.selectedIndex属性,x=>x.SelectedCountry,转换器:新的PickerItemsToTypeConverter(Country.Countries));
内容=选择器;
}
}
公共类PickerItemsToTypeConverter:IValueConverter
{
私有只读IList_pickerItemsList;
公共PickerItemsToTypeConverter(IList pickerItemsList)=>\u pickerItemsList=pickerItemsList;
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var itemType=(T)值;
返回_pickerItemsList.IndexOf(itemType);
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var selectedIndex=作为int?的值?;
如果(selectedIndex==null)抛出新异常(“无选择”);
返回_pickerItemsList[selectedIndex.Value];
}
}

哪一行抛出错误?您在RegistrationPageViewModel中将国家/地区定义为公共字段,如果没有可用VM实例,您不能简单地从另一个类引用它。还不清楚代码的第三部分位于何处-这是页面的一部分、VM还是另一个类的全部内容?SelectedCountry=Countries[countriesseselectedIndex];请尝试改用vm.Countries,因为vm是vm的一个实例。如何将其绑定到表单?我试着在文件里写,但我只是得到了一个空的选择器
public class Country
    {
        public string Name { get; }
        public Country(string name) => Name = name;
        public static Country Afghanistan = new Country("Afghanistan");
        public static Country Albania = new Country("Albania");
        public static Country Algeria = new Country("Algeria");
        public static Country Andorra = new Country("Andorra");
        public static Country Angola = new Country("Angola");

        public static IList<Country> Countries = new List<Country> { Afghanistan, Albania, Algeria, Andorra, Andorra, Angola };
    }

    [ImplementPropertyChanged]
    public class RegistrationPageViewModel
    {
        public Country SelectedCountry { get; set; }
    }

    public class RegistrationPage : ContentPage
    {
        public RegistrationPage()
        {
            var vm = new RegistrationPageViewModel();
            BindingContext = vm;
            var picker = new Picker();
            foreach (var country in Country.Countries) picker.Items.Add(country.Name);
            picker.SetBinding<RegistrationPageViewModel>(Picker.SelectedIndexProperty, x => x.SelectedCountry, converter: new PickerItemsToTypeConverter<Country>(Country.Countries));
            Content = picker;
        }
    }

    public class PickerItemsToTypeConverter<T> : IValueConverter
    {
        private readonly IList<T> _pickerItemsList;

        public PickerItemsToTypeConverter(IList<T> pickerItemsList) => _pickerItemsList = pickerItemsList;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var itemType = (T)value;
            return _pickerItemsList.IndexOf(itemType);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var selectedIndex = value as int?;
            if (selectedIndex == null) throw new Exception("No selection");
            return _pickerItemsList[selectedIndex.Value];
        }
    }