绑定到Xamarin Forms Picker不会更新数据

时间:2018-09-19 19:48:28

标签: c# xamarin.forms

我的xaml页面有问题。选择案例的眼睛的选择器不会将EyeId保存到窗体。这是代码。它应该显示眼睛,然后将其选中时将EyeId保存到表单上的EyeId字段。

这是xaml页面

<?xml version="1.0" encoding="utf-8" ?>
<forms:ContentPage
    x:Class="SxQiApp.EditSxCasePage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
    xmlns:viewModels="clr-namespace:SxQiApp.ViewModels;assembly=SxQiApp">

    <forms:ContentPage.BindingContext>
        <viewModels:SxCaseViewModel />
    </forms:ContentPage.BindingContext>

    <forms:StackLayout>
        <Entry Text="{Binding SelectedSxCase.SxCaseId}" />
        <Label Text="Record" />
        <Entry Text="{Binding SelectedSxCase.Record}" />
        <Label Text="Case" />
        <Entry Text="{Binding SelectedSxCase.Facesheet}" />
        <Label Text="Return OR" />
        <Switch IsToggled="{Binding SelectedSxCase.ReturnOr}" />
        <Label Text="Complication" />
        <Switch IsToggled="{Binding SelectedSxCase.Complication}" />
        <Picker
            ItemDisplayBinding="{Binding Eye1}"
            ItemsSource="{Binding Eyes}"
            SelectedIndex="{Binding EyeId, Mode=TwoWay}"
            SelectedItem="{Binding SelectedEye,Mode=TwoWay}"
            Title="Select Eye" />
        <Label Text="{Binding SelectedSxCase.UniqueId}" />
        <Label Text="Eye"/>
        <Entry Text="{Binding SelectedSxCase.EyeId}" />
        <Button Command="{Binding PutSxCaseCommand}" Text="Edit Case" />
        <Button Command="{Binding DeleteSxCaseCommand}" Text="Delete Case" />
    </forms:StackLayout>

</forms:ContentPage>

这是背后的代码

using SxQiApp.Models;
using SxQiApp.ViewModels;
using System.Collections.Generic;
using Xamarin.Forms;

namespace SxQiApp
{
    // XamlC is set in App.xaml.cs
    //[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EditSxCasePage : ContentPage
    {

        public EditSxCasePage()
        {
            InitializeComponent();

        }

        public EditSxCasePage(SxCaseViewModel SxCaseViewModel)
        {
            InitializeComponent();

            this.SxCaseViewModel = SxCaseViewModel;
            BindingContext = SxCaseViewModel;



        }

        public SxCaseViewModel SxCaseViewModel { get; }


    }
}

这是ViewModel

using SxQiApp.Models;
using SxQiApp.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace SxQiApp.ViewModels
{
    public class SxCaseViewModel : INotifyPropertyChanged
    {
        private List<SxCase> _sxcase;
        private SxCaseDataService _sxCaseDataService = new SxCaseDataService();
        private SxCase _selectedSxCase;
        private List<Eye> _eye;
        private EyeDataService _eyeDataService = new EyeDataService();
        private Eye _selectedEye;

        public SxCase SelectedSxCase
        {
            get { return _selectedSxCase; }
            set
            {
                _selectedSxCase = value;
                OnPropertyChanged();
            }
        }


        public List<SxCase> SxCases
        {
            get
            {
                return _sxcase;

            }
            set
            {
                _sxcase = value;
                OnPropertyChanged();
            }
        }

        public Command PutSxCaseCommand
        {
            get
            {
                return new Command(async () =>
                {
                    var sxCaseDataService = new SxCaseDataService();
                    await sxCaseDataService.PutSxCase(_selectedSxCase.SxCaseId, _selectedSxCase);
                });
            }
        }

        public Command DeleteSxCaseCommand
        {
            get
            {
                return new Command(async () =>
                {
                    var sxCaseDataService = new SxCaseDataService();
                    await sxCaseDataService.DeleteSxCaseAsync(_selectedSxCase.SxCaseId);
                });
            }
        }


        public ICommand AddRecordCommand => new Command(async () =>
        {

            await _sxCaseDataService.PostSxCase(SelectedSxCase);


        });



        public SxCaseViewModel()
        {


            SelectedSxCase = new SxCase();
            GetSxCases();

            SelectedEye = new Eye();
            GetEyes();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private async Task GetSxCases()
        {


            SxCases = await _sxCaseDataService.GetSxCases();
        }

        public Eye SelectedEye
        {
            get { return _selectedEye; }
            set
            {
                _selectedEye = value;
                OnPropertyChanged();
            }
        }


        public List<Eye> Eyes
        {
            get
            {
                return _eye;

            }
            set
            {
                _eye = value;
                OnPropertyChanged();
            }

        }

        private async Task GetEyes()
        {


            Eyes = await _eyeDataService.GetEyes();
        }


    }



}

0 个答案:

没有答案