【问题标题】:Picker not firing to selecteditem选择器未触发所选项目
【发布时间】:2020-07-02 19:46:31
【问题描述】:

我在选择器中选择值时遇到问题。 我已经实现了视图模型,并且可以显示选项,但是一旦我选择了该项目,所选选项就不会被触发并且 returnig 值为空。我已经在 baseviewmodel 中通知了,请您帮忙

  class CategoriesViewModel : BaseViewModel
    {
        public CategoriesViewModel()
        {
            _mainSection = FillMainSection();
        }
        private List<MainSection> _mainSection = new List<MainSection>();
        private MainSection _selectedMainSection;
        public List<MainSection> MainSection
        {
            get => _mainSection;
            set => SetProperty(ref _mainSection, value);
        }

        public MainSection SelectedMainSection
        {
            get => _selectedMainSection;
            set
            {
                
                switch (_selectedMainSection.Position)
                {
                    case 0:
                        _selectedMainSection.Value = 0;
                        break;
                    case 1:
                        _selectedMainSection.Value = 1;
                        break;
                }
                SetProperty(ref _selectedMainSection, value);
            }
        }

        public List<MainSection> FillMainSection()
        {
            var newListMainSection = new List<MainSection>()
            {
                new MainSection()
                {
                    Position = 0,
                    Text = Resources.AppResources.LabelPetsCategory,
                    Value = 0
                },
                new MainSection()
                {
                    Position = 1,
                    Text = Resources.AppResources.LabelLostAndFound,
                    Value = 1
                },
              
            };

            return newListMainSection;
        }
    }
}

Xaml 
    <Picker
                                        x:Name="SectionPicker"
                                        Title="{x:Static resources:AppResources.LabelSection}"
                                        Grid.Row="2"
                                        ItemDisplayBinding="{Binding Text}"
                                        ItemsSource="{Binding MainSection}"
                                        SelectedItem="{Binding SelectedMainSection, Mode=OneWay}" 
                                        
                                        />

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    我用你的代码做一个测试。这是正在运行的 gif。

    首先我将SelectedItem的绑定模式更改为Mode=TwoWay,然后我添加一个Label来显示来自Picker的选择项。

      <StackLayout>
            <!-- Place new controls here -->
            <Picker
                     x:Name="SectionPicker"
                     Title="title"
                     Grid.Row="2"
                     ItemDisplayBinding="{Binding Text}"
                     ItemsSource="{Binding MainSection}"
                     SelectedItem="{Binding SelectedMainSection, Mode=TwoWay}"              
                                            />
            <Label Text="test" TextColor="OrangeRed" />
            <Label Text="{Binding MainSectionText}" TextColor="OrangeRed" />
        </StackLayout>
    

    这里是我的CategoriesViewModel.cs,请注意SelectedMainSectionPositionValue的值相同,为什么需要使用switch重新设置,如果你使用MVVM,你使用@987654332 @,它会为模型更新,你不需要为这个模型再次更新属性。

       public class CategoriesViewModel : BaseViewModel
        {
            private List<MainSection> _mainSection;
            private MainSection _selectedMainSection;
            public CategoriesViewModel()
            {
                _mainSection = FillMainSection();
              //  _selectedMainSection = new MainSection();
            }
           
            public List<MainSection> MainSection
            {
                get => _mainSection;
                set => SetProperty(ref _mainSection, value);
            }
    
          
            public MainSection SelectedMainSection
            {
                get => _selectedMainSection;
                set
                {
                    SetProperty(ref _selectedMainSection, value);
                    MainSectionText = "select item" + _selectedMainSection.Value.ToString();
                  //  switch (_selectedMainSection.Position)
                   // {
                  //      case 0:
                  //          _selectedMainSection.Value = 0;
                  //          break;
                  //      case 1:
                  //          _selectedMainSection.Value = 1;
                  //          break;
                 //   }
                   
                   
                }
            }
            private string _mainSectionText;
            public string MainSectionText
            {
                get
                {
                    return _mainSectionText;
                }
                set
                {
                    SetProperty(ref _mainSectionText, value);
                }
            }
            public List<MainSection> FillMainSection()
            {
                var newListMainSection = new List<MainSection>()
                {
                    new MainSection()
                    {
                        Position = 0,
                        Text = "LabelPetsCategory",
                        Value = 0
                    },
                    new MainSection()
                    {
                        Position = 1,
                        Text = "LabelLostAndFound",
                        Value = 1
                    },
    
                };
    
                return newListMainSection;
            }
        }
    
        public class MainSection
        {
            public MainSection()
            {
            }
    
            public int Position { get; set; }
            public string Text { get; set; }
            public int Value { get; set; }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2013-01-23
      • 2021-06-27
      • 1970-01-01
      相关资源
      最近更新 更多