【发布时间】:2021-03-11 05:44:10
【问题描述】:
我有这样定义的集合视图:
<RefreshView
Command="{Binding RefreshCommand}"
IsRefreshing="{Binding IsRefreshing}">
<CollectionView
ItemsSource="{Binding Menus}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32" />
<RowDefinition Height="128" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image
Source="{Binding ImageURI}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Aspect="AspectFill"
Opacity="0.64"
Grid.RowSpan="2"
Grid.ColumnSpan="3"/>
<Label
Text="{Binding Name}"
FontAttributes="Bold"
FontSize="Large"
TextColor="Black"
Grid.Row="0"
Grid.ColumnSpan="3"/>
<Label
Text="{Binding Description}"
FontSize="Body"
TextColor="Black"
Grid.Row="1"
Grid.ColumnSpan="3"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
但是在构建时,视图中没有出现任何内容,并且控制台显示以下错误:
[0:] Binding: 'ImageURI' property not found on '<model>', target property: 'Xamarin.Forms.Image.Source'
[0:] Binding: 'Name' property not found on '<model>', target property: 'Xamarin.Forms.Label.Text'
[0:] Binding: 'Description' property not found on '<model>', target property: 'Xamarin.Forms.Label.Text'
当我将视图更改为:
<Label Text="{Binding Menus.Count}"
TextColor="Black"/>
<Label Text="{Binding Menus[0]}"
TextColor="Black"/>
<Label Text="{Binding Menus[0].Name}"
TextColor="Black"/>
结果看起来像这样,但仍然产生相同的错误消息。
[0:] Binding: 'Name' property not found on 'hollywood.Models.MenuHandle', target property: 'Xamarin.Forms.Label.Text'
视图模型和模型分别如下所示:
public class MenuListViewModel : BaseViewModel
{
public MenuListViewModel()
{
//RefreshMenus();
Menus.Add(new MenuHandle { Name = "Test", Description="test2"});
Title = "Menu";
}
public async Task RefreshMenus()
{
IsRefreshing = true;
TimeSpan age = DateTime.Now - MenusAge;
if (age.TotalSeconds > 1)
{
try
{
Menus = await App.ApiConnection.GetMenusAsync();
MenusAge = DateTime.Now;
}
catch { }
}
IsRefreshing = false;
}
ObservableCollection<MenuHandle> menus = new ObservableCollection<MenuHandle>();
public ObservableCollection<MenuHandle> Menus
{
get { return menus; }
private set { SetProperty(ref menus, value); }
}
bool isRefreshing;
public bool IsRefreshing
{
get { return isRefreshing; }
private set { SetProperty(ref isRefreshing, value); }
}
DateTime MenusAge = DateTime.MinValue;
public ICommand RefreshCommand => new Command(async () => await RefreshMenus());
}
}
模型类:
public class MenuHandle
{
[JsonProperty("name")]
public string Name;
[JsonProperty("url_name")]
public string URLName;
[JsonProperty("description")]
public string Description;
[JsonProperty("image")]
public Uri ImageURI;
}
任何建议将不胜感激。
【问题讨论】:
-
也许你需要用 get 将它们声明为属性;在 MenuHandle 中设置:
public string Name { get; set; }
标签: c# xaml xamarin xamarin.forms xamarin.android