Xamarin Forms Binding 无法转换为类型

Xamarin Forms Binding cannot be converted to type

Xamarin 表单的新手,在过去一天半的时间里试图弄清楚为什么我的某些绑定有效而其他绑定无效。它们的构造方式相同但行为不同。大多数元素都绑定到我创建的 objects 的可观察 collections,从我的 API 中的 ENUMS 中提取。 XAML 选择器正确加载我的 collections - 当我的应用程序是 运行 并且我单击选择器时,我可以很好地查看列表。问题是 SelectedItem 的绑定。标签类型选择器正确显示所选项目,模拟类型选择器不显示我选择的项目 - 它是空白的(当我点击选择器时 collection 仍然显示,只是所选项目是空白)。在我创建的服务中,这两个属性同时以相同的方式设置,以实现与我的模型中的属性交互的方法。视图模型然后创建该服务的实例以访问属性。需要注意的一件奇怪的事情是,当热重载为 运行 时,我删除了 re-add 这一行:SelectedItem="{Binding MyItemSimulationType}" 它有效!但是一旦我关闭程序和 re-run 应用程序,所选项目又是空白的:/

输出 window 表示如下:

[0:] Binding: '' cannot be converted to type 'TagType'
[0:] Binding: '' cannot be converted to type 'SimulationType'

奇怪的是 TagType 的绑定有效而 SimulationType 无效。这是 XAML:

 <RefreshView x:DataType="local:ReadWriteViewModel" Command="{Binding LoadReadWriteValuesCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
    <ScrollView>
        <StackLayout Padding="10">

            <!--Item Properties-->
            <Frame>
                <StackLayout>
                    <Label Text="Item" FontSize="Subtitle" TextColor="Black" VerticalTextAlignment="Center" />

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <!--Tag Type-->
                        <Label Grid.Row="0" Grid.Column="0" Text="Tag Type" VerticalTextAlignment="Center" HorizontalTextAlignment="End"/>
                        <Picker Grid.Row="0" Grid.Column="1" FontSize="Small" ItemsSource="{Binding MyItemHWTagTypes}" SelectedItem="{Binding MyItemHWTagType}" >
                            <Picker.Items></Picker.Items>
                        </Picker>

                        <!--Simulation Type-->
                        <Label Grid.Row="1" Grid.Column="0" Text="Simulation Type" VerticalTextAlignment="Center" HorizontalTextAlignment="End" />
                        <Picker Grid.Row="1" Grid.Column="1" FontSize="Small" ItemsSource="{Binding MyItemSimulationTypes}" SelectedItem="{Binding MyItemSimulationType}" HorizontalOptions="FillAndExpand">
                            <Picker.Items></Picker.Items>
                        </Picker>
                    </Grid>
                </StackLayout>
            </Frame>             
        </StackLayout>
    </ScrollView>
</RefreshView>

视图模型:

    private ObservableCollection<TagType> _itemHWTagTypes;
    private TagType _itemHWTagType;
    private ObservableCollection<SimulationType> _itemSimulationTypes;
    private SimulationType _itemSimulationType;

    public ObservableCollection<TagType> MyItemHWTagTypes
    {
      get => _itemHWTagTypes;
      set => SetProperty(ref _itemHWTagTypes, value);
    }

    public TagType MyItemHWTagType
    {
      get => _itemHWTagType;
      set => SetProperty(ref _itemHWTagType, value);
    }

    public ObservableCollection<SimulationType> MyItemSimulationTypes
    {
      get => _itemSimulationTypes;
      set => SetProperty(ref _itemSimulationTypes, value);
    }

    public SimulationType MyItemSimulationType
    {
      get => _itemSimulationType;
      set => SetProperty(ref _itemSimulationType, value);
    }

   public Command LoadReadWriteValuesCommand { get; }

   public ReadWriteViewModel()
    {

      //Load Model
      LoadReadWriteValuesCommand = new Command(async () => await         ExecuteLoadReadWriteValuesCommand());
    }

async Task ExecuteLoadReadWriteValuesCommand()
    {
      IsBusy = true;

      try
      {
        var asc = await ASCService.GetASCObjectAsync();
    
          // ----- Set Item properties to default values in model
          MyItemHWTagTypes = asc.ModelItemHWTagTypes;
          MyItemHWTagType = asc.ModelItemHWTagType;
          MyItemSimulationTypes = asc.ModelItemSimulationTypes;
          MyItemSimulationType = asc.ModelItemSimulationType;
     }
      catch (TaskCanceledException tcex)
      {
        Debug.WriteLine(tcex);
      }
      finally
      {
        IsBusy = false;
      }
    }

如果需要更多代码,请告诉我,我试图在此处的示例代码中对其进行简化,并删除了与我的问题无关的额外代码。我在 ExecuteLoadReadWriteValuesCommand() 中的所有属性都已正确设置,因此我确信这不是问题所在。 我的输出中还收到很多其他垃圾邮件 window,我不认为这些消息相关但也许吧?这些看起来可能有问题:

[TabLayout] MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
[Choreographer] Skipped 38 frames!  The application may be doing too much work on its main thread.
[OpenGLRenderer] Davey! duration=1656ms; Flags=0, IntendedVsync=1769860264400, Vsync=1770843597694, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=1770846662700, AnimationStart=1770846714100, PerformTraversalsStart=1770847196400, DrawStart=1771495643200, SyncQueued=1771503136700, SyncStart=1771503918600, IssueDrawCommandsStart=1771504436800, SwapBuffers=1771515968100, FrameCompleted=1771517457000, DequeueBufferDuration=303000, QueueBufferDuration=902000, 

我想通了!您不能将绑定 属性 设置为集合位置 [0] 中的值。我经历了并将默认值设置为位置 [1] 并且它起作用了!不确定这是一个错误还是它是如何工作的,但很高兴一切都结束了。