Xamarin形式:在可绑定对象内添加全局值时,指定的转换无效:列表视图

2cmtqfgy  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(110)

我试图创建一个布局,其中有重复的自定义条目列表,并且当任何文本列表发生变化时,SuggestedText出现,这只是一个字符串观察集合,在构建视图模式期间初始化。

<StackLayout BindableLayout.ItemsSource="{Binding MyObjectLists.ListofLicensePlates}"  VerticalOptions="FillAndExpand">
        <BindableLayout.ItemTemplate>
               <DataTemplate>
                      <Frame Margin="0,10,0,10" Padding="0" HasShadow="False" BackgroundColor="Transparent">
                         <Grid Margin="20">
                            <Grid.ColumnDefinitions>
                                   <ColumnDefinition Width="*" ></ColumnDefinition>
                                   <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <StackLayout Orientation="Vertical" Grid.Column="0" Grid.Row="0" >
                                   <Label Text="Text1" TextColor="Black" FontAttributes="Bold" Grid.Column="0" Grid.Row="0" />
                                              <AbsoluteLayout>
                                                   <Frame Grid.Column="0" Grid.Row="1"  Padding="5" HasShadow="False" BorderColor="{StaticResource GrayBorderColor}" CornerRadius="30" HeightRequest="55" HorizontalOptions="FillAndExpand">
                                                      <controls:BorderlessEntry 
                                                             TextTransform="Uppercase" Text="{Binding LicensePlate}"
                                                             Index = "{Binding Index}"
                                                             TextChanged ="ShowListView" Completed="ShowListView"
                                                             MaxLength="50" x:Name="Text1" Keyboard="Text" 
                                                             Placeholder="Text1"
                                                                                     AbsoluteLayout.LayoutBounds="15,70,285,38" AbsoluteLayout.LayoutFlags="None"
                                                                                     FontFamily="OpenSansRegular" />
                                                  </Frame>
                                                  <ListView BindableLayout.ItemsSource="{Binding Path=BindingContext.SuggestedText, Source={x:Reference thispage}}" 
                                                                                  x:Name="LicensePlateListView" IsVisible="{Binding IsVisible}" CachingStrategy="RecycleElement" 
                                                                                  BackgroundColor="White" ItemTapped="OnItemTapped" AbsoluteLayout.LayoutBounds="20,110,269,160" 
                                                                                  AbsoluteLayout.LayoutFlags="None">
                                                       <BindableLayout.ItemTemplate>
                                                            <DataTemplate>
                                                                  <StackLayout BackgroundColor="White">
                                                                        <Label Text="{Binding .}" FontSize="16" TextColor="#FF464859"/>
                                                                  </StackLayout>
                                                            </DataTemplate>
                                                        </BindableLayout.ItemTemplate>
                                                   </ListView>
                                            </AbsoluteLayout>
                           </Grid>
                      </Frame>
               </DataTemplate>
          </BindableLayout.ItemTemplate>
</StackLayout>

以下是创建建议文本的方式:

public class MyViewModel : BaseViewModel
{
        public LicensePlateListViewModel()
        {
            
        }

        public MyViewModel (MyObject parameters, INavigation navigation)
        {
            MyObjectLists = parameters;
            
            Device.BeginInvokeOnMainThread(() => {
                try
                {
                    FillSuggestText();
                }
                catch (Exception ex)
                {
                    IsBusy = false;
                    Helper.GlobalErrorTrashing(ex);
                }
            });

        }
        private MyObject myObjectLists;
        public MyObject MyObjectLists
        {
            get { return myObjectLists; }
            set { myObjectLists= value; OnPropertyChanged(); }
        }
        private ObservableCollection<string> suggestedText;
        public ObservableCollection<string> SuggestedText
        {
            get { return suggestedText; }
            set { suggestedText= value; OnPropertyChanged(); }
        }

        public void FillSuggestText()
        {
            if (PaidGuestParking.GuestZoneParking.Parkings != null)
            {
                var texts = MyObjectLists.TextList.Select(p => p.Text).ToList();
                foreach(var text in texts ) { 
                    SuggestedText.Add(text);
                }
            }
        }

...
...
...
}

MyObject是这样的:

public class MyObject
{
   public List<TextObject> TextList { get; set; }
   public ObservableCollection<string> ListofLicensePlates { get; set; }
}

show list view是一个简单的函数,它可以为条目下的特定列表视图更改IsVisible。如果我静态列出所有列表视图源代码,它可以正常工作,但每当我尝试动态添加来自SugestedText的内容时,我在导航到此页面之前会收到错误。

ex = {System.InvalidCastException: Specified cast is not valid.
  at Xamarin.Forms.BindableLayout+<>c.<.cctor>b__19_4 (Xamarin.Forms.BindableObject b) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableLayout.cs:24 
  at Xamarin.Forms.BindableObject.CreateAndAddCo...

我真的被困在这个地方,请给我一些建议,我也愿意改变,使建议文本是在我的对象列表本身,使建议文本可能是不同的输入,但这样做也提示相同的错误。

oipij1gg

oipij1gg1#

从你发布的代码中,我发现你将MyObjectLists设置为StackLayoutBindableLayout.ItemsSource,所以变量MyObjectLists应该是IEnumerable items的集合。

<StackLayout BindableLayout.ItemsSource="{Binding MyObjectLists}"  >

但是从MyViewModel.cs中包含的以下代码中,我们可以发现它看起来像一个简单的对象(MyObject)。请重新检查它。

private MyObject myObjectLists;
    public MyObject MyObjectLists
    {
        get { return myObjectLists; }
        set { myObjectLists= value; OnPropertyChanged(); }
    }

此外,如果要将父变量SuggestedText绑定到内部变量ListView,可以尝试将BindableLayout.ItemsSource替换为ItemsSource,将BindableLayout.ItemTemplate替换为ListView.ItemTemplate
请参考以下代码:

<ListView ItemsSource="{Binding Path=BindingContext.SuggestedText, Source={x:Reference thispage}}" 
                                                                              x:Name="LicensePlateListView"  CachingStrategy="RecycleElement" 
                                                                              BackgroundColor="White" >
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout BackgroundColor="White">
                                                <Label Text="{Binding .}" FontSize="16" TextColor="#FF464859"/>
                                            </StackLayout>

                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>

也可以使用StackLayout BindableLayout

<StackLayout BindableLayout.ItemsSource="{Binding Path=BindingContext.SuggestedText, Source={x:Reference thispage}}" Orientation="Horizontal"  > 
                                    <BindableLayout.ItemTemplate>
                                        <DataTemplate>
                                            <StackLayout BackgroundColor="White">
                                                <Label Text="{Binding .}" FontSize="16" TextColor="#FF464859"/>
                                            </StackLayout>
                                        </DataTemplate>
                                    </BindableLayout.ItemTemplate>
                                </StackLayout>

注:
如果使用ListView,请记住添加标记<ViewCell> </ViewCell>

相关问题