Xamarin:使用 FreshMvvM ViewModel 绑定选择器选择的项目

Xamarin: Bind picker selected item with FreshMvvM ViewModel

我正在创建一个表单,其中需要使用下拉菜单选择产品状态。 我为此创建了一个选择器。数据是从我的 ViewModel 中的列表加载的,但不会发回。 我已经尝试过使用输入字段进行相同的操作并且效果很好。我只是不知道如何 link 选择器与视图模型。

这是我的代码。

Xaml

    </Grid>
                    <Label Text="Current status" Style="{StaticResource MainLabel}"/>
                    <Label Style="{StaticResource MainLabel}" Text="{Binding ProductionStatus, Mode=TwoWay}"/>
                    <!-- gets send when saved-->
                    <Entry Style="{StaticResource MainEntry}" Text="{Binding ProductionStatus, Mode=TwoWay}" Keyboard="Text" /> 
                    <Label Text="Remark" Style="{StaticResource MainLabel} "/>
                    <!-- gets send when saved-->
                    <Entry Text="{Binding Remark}" Keyboard="Text" Style="{StaticResource MainEntry}"/>
                    <!-- Does not bind with anything.-->
                    <Picker x:Name="statusPicker" ItemsSource="{Binding ProductionOrderStatusName}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding ProductionStatusName}"/>
                   
    
                    <Button Style="{StaticResource PrimaryButton}" Text="Save"  Command="{Binding SaveCommand}"></Button>

ViewModel 代码隐藏

 public ICommand SaveCommand
        {
            get
            {
                return new Command(ExecuteSaveCommand);
            }
        }
        
        private async void ExecuteSaveCommand()
        {
            int statusId = FindProductionOrderStatusId(ProductionStatus); //the production status should be the result of the selected value in the picker
            Guid id = _CurrentProductionOrder.Id;
            string remark = Remark; // value from the remark entery in the xaml
          
            await __productionOrderService.UpdateAsync(id, remark,statusId);
        }

属性

 
        public ObservableCollection<ProductionOrderStatus> productionOrderStatusName;
        public ObservableCollection<ProductionOrderStatus> ProductionOrderStatusName
        {
            get { return productionOrderStatusName; }
            set
            {
                productionOrderStatusName = value;


            }
        }
        public int amount;
        public int Amount
        {
            get { return amount; }
            set
            {
                amount = value;

            }
        }

        public DateTime finishDate;
        public DateTime FinishDate
        {
            get { return finishDate; }
            set
            {
                finishDate = value;
               

            }
        }
        public int ordernumber;
        public int OrderNumber
        {
            get { return ordernumber; }
            set
            {
                ordernumber = value;
             

            }
        }

        public string remark;
        public string Remark
        {
            get { return remark; }
            set
            {
                remark = value;
              

            }
        }

        public string productionStatus;
        public string ProductionStatus
        {
            get
            {
                return productionStatus;
            }
            set
            {
                productionStatus = value;
            

            }
        }

        private string materialNumber;

        public string MaterialNumber
        {
            get { return materialNumber; }
            set
            {
                materialNumber = value;
               

            }
        }


        private string materialDescription;

        public string MaterialDescription
        {
            get { return materialDescription; }
            set
            {
                materialDescription = value;
               

            }
        }

加载我的数据背后的代码

 public OrderViewModel()
        {
            _productionOrderStepService = new MockingProductionOrderStepService();
            _materialService = new MockingMaterialService();
            __productionOrderService = new MockingProductionOrderService();
            __productionOrderStatusService = new MockingProductionOrderStatusService();
            _seederService = new Seeder(__productionOrderService, _productionOrderStepService, __productionOrderStatusService, _materialService);
            _seederService.EnsureSeeded();
           

        }

       

        public override void Init(object initData)
        {
            _CurrentProductionOrder = initData as ProductionOrder;
            LoadItemState();
            base.Init(initData);
        }

        private void LoadItemState()
        {
            ObservableCollection<ProductionOrderStatus> ProductionStatusName = new ObservableCollection<ProductionOrderStatus>(__productionOrderStatusService.GetListAllAsync().Result);
            this.ProductionOrderStatusName = ProductionStatusName;
            this.materialDescription = FindMaterialDescription(_CurrentProductionOrder.MaterialId);
            this.materialNumber = FindMaterialNumber(_CurrentProductionOrder.MaterialId);
            this.productionStatus = FindProductionOrderStatus(_CurrentProductionOrder.StatusId);
            this.remark = _CurrentProductionOrder.Remark;
            this.amount=_CurrentProductionOrder.Amount;
            this.finishDate = _CurrentProductionOrder.FinishDate;
            this.ordernumber = _CurrentProductionOrder.OrderNumber;

        }

感谢您的帮助!

你让事情变得比需要的更复杂

<Picker x:Name="statusPicker" 

 // this is the List of items X to display
 ItemsSource="{Binding ProductionOrderStatusName}" 
 // this tells the picker which property of X to display to the user
 ItemDisplayBinding="{Binding Name}" 
 // this is the specific X the user has selected
 SelectedItem="{Binding SelectedStatus}" />

然后在您的虚拟机中

 public ObservableCollection<ProductionOrderStatus> ProductionOrderStatusName { get; set; }

 public ProductionOrderStatus SelectedStatus { get; set; }