我试图创建一个布局,其中有重复的自定义条目列表,并且当任何文本列表发生变化时,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...
我真的被困在这个地方,请给我一些建议,我也愿意改变,使建议文本是在我的对象列表本身,使建议文本可能是不同的输入,但这样做也提示相同的错误。
1条答案
按热度按时间oipij1gg1#
从你发布的代码中,我发现你将
MyObjectLists
设置为StackLayout
的BindableLayout.ItemsSource
,所以变量MyObjectLists
应该是IEnumerable items
的集合。但是从
MyViewModel.cs
中包含的以下代码中,我们可以发现它看起来像一个简单的对象(MyObject
)。请重新检查它。此外,如果要将父变量
SuggestedText
绑定到内部变量ListView
,可以尝试将BindableLayout.ItemsSource
替换为ItemsSource
,将BindableLayout.ItemTemplate
替换为ListView.ItemTemplate
。请参考以下代码:
也可以使用
StackLayout BindableLayout
:注:
如果使用
ListView
,请记住添加标记<ViewCell> </ViewCell>
。