在选择器上绑定 SelectedItem 不起作用

Binding SelectedItem on Picker Not Working

我在选择器菜单中设置 selected 项时遇到问题。我的 viewModel 中有一个列表,我将 ItemSource 绑定到视图中并且效果很好,我可以 select 选择器并查看我的设施列表,但我无法设置 SelectedItem 或 SelectedItem 索引以显示默认值select离子。当我在 NavigatedTo() 方法中设置它们时,我在视图中绑定的其他数据工作得很好。我也进入 setter for Facility 因为我的 writeLine 正在控制台中写入。当我在 NavigateFrom() 方法中将它添加到我的参数时,我成功地将 sectedItem 从列表中取出,没有任何问题,我似乎无法设置选择器的值。另外,如果 displayAlert 在 NavigateTo() 方法的底部没有注释,我可以看到 Facility 等于一个有效对象,所以我知道它设置正确但视图不想在选择器中显示它?

    <Grid Grid.Row="3" Grid.ColumnSpan="2" Padding="15">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
            <Label Text="Facility:"  VerticalTextAlignment="Center"/>
            <Picker Grid.Column="1" ItemsSource="{Binding FacilityList}" 
                    ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding Facility, Mode=TwoWay" />
    </Grid>
public class CallInfoPageViewModel : ViewModelBase, INavigationAware
    {
        private Facility _facility;
        public Facility Facility
        {
            get { return _facility; }
            set
            {
                Console.WriteLine("\n**************\n we hit the thing!\n************");
                SetProperty(ref _facility, value);
            }
        }

        public List<Facility> FacilityList { get; set; }

        private readonly INavigationService _navigationService;
        private DelegateCommand _showHelpPageCommand;
        public DelegateCommand ShowHelpPageCommand =>
            _showHelpPageCommand ?? (_showHelpPageCommand = new DelegateCommand(ExecuteShowHelpPageCommand));


        public CallInfoPageViewModel(INavigationService navigationService)
        {
            Title = "Call Information";
            _navigationService = navigationService;

            FacilityList = new MockDataStore().GetFacilityList();

            CallDate = SpillDate = DateTime.Now.Date;
            CallTime = DateTime.Now.TimeOfDay;
            SpillTime = CallTime.Subtract(TimeSpan.FromHours(1));
        }

        async void ExecuteShowHelpPageCommand()
        {
            await _navigationService.NavigateAsync("CallInfoHelpPage");
        }

        // called when leaving this view/viewModel
        public async void OnNavigatedFrom(INavigationParameters parameters)
        {
            if (CallerName != null)
            {
                parameters.Add("callerName", CallerName);
            }

            if (UserName != null)
            {
                parameters.Add("userName", UserName);
            }

            if (Facility != null)
            {
                parameters.Add("facility", Facility);
            }

            parameters.Add("callDate", CallDate);
            parameters.Add("callTime", CallTime);
            parameters.Add("spillDate", SpillDate);
            parameters.Add("spillTime", SpillTime);
            parameters.Add("spillStillOngoing", SpillStillOngoing);
            parameters.Add("spillContainedOnSite", SpillContainedOnSite);
            parameters.Add("needAssistance", NeedAssistance);
        }

        // called when everything created/ initialised and page added to navigation stack
        public async void OnNavigatedTo(INavigationParameters parameters)
        {

            if (parameters.ContainsKey("demoMode") || parameters.ContainsKey("callInfoPageComplete"))
            {
                UserName = parameters.GetValue<string>("userName");
                CallerName = parameters.GetValue<string>("callerName");
                SpillDate = parameters.GetValue<DateTime>("spillDate");
                CallDate = parameters.GetValue<DateTime>("callDate");
                SpillTime = parameters.GetValue<TimeSpan>("spillTime");
                CallTime = parameters.GetValue<TimeSpan>("callTime");
                Facility = parameters.GetValue<Facility>("facility");
                SpillStillOngoing = parameters.GetValue<bool>("spillOngoing");
                SpillContainedOnSite = parameters.GetValue<bool>("spillContainedOnSite");
                NeedAssistance = parameters.GetValue<bool>("needAssistance");
            }

            //await Application.Current.MainPage.DisplayAlert("params",
            //    "Facilty " + Facility + "\n name: " + Facility.Name + "\naddr: " + Facility.Address,
            //    "OK");
        }
    }
async void ExecuteLoadDemoCommand()
    {
        loadDemoBool = true;
        report.CallerName = "Stephan Kemper";
        report.UserName = "Tyler Bartlett";
        report.SpillDate = new DateTime(2019, 12, 10);
        report.CallDate = new DateTime(2019, 12, 10);
        report.SpillTime = new TimeSpan(13, 0, 0);
        report.CallTime = new TimeSpan(14, 0, 0);
        report.Facility = new MockDataStore().GetFacility("Millersburg - Main");
        report.SpillStillOngoing = false;
        report.SpillContainedOnSite = true;
        report.NeedAssistance = false;
    }
public Facility GetFacility(string facilityName)
    {
        return facilities.Find(facility => facility.Name == facilityName);
    }

您还没有使用 new 分配 _facility。这可能是原因。 在构造函数中添加: _facility = new Facility();

SelectedItem 需要是对 ItemsSource 中项目的引用。不是对象的 COPY,而是对同一对象的引用。您启动一个新的 MockDataStore() 来获取 Facility 这一事实让我认为它们不是相同的引用