如何解决C#Xamarin-如何从“ ObservableCollection <T>”访问对象属性?
我具有创建ObservableCollection<MenuBody>
来创建Xamarin.Forms用户菜单的功能。
private ObservableCollection<MenuBody> _userList;
public ObservableCollection<MenuBody> UserList
{
set { SetProperty(ref _userList,value); }
get { return _userList; }
}
private void LoadUserMenu()
{
UserList = new ObservableCollection<MenuBody>
{
new MenuBody
{
Id = 1,Text = "Principal",Icon = IconFont.FileSignature,},new MenuBody
{
Id = 2,Text = "Configurações",Icon = IconFont.Cog,};
}
XAML CollectionView绑定属性:
<CollectionView
x:Name="MyUserCollectionView"
BackgroundColor="Transparent"
ItemsSource="{Binding UserList}"
SelectionMode="Single"
SelectionChangedCommand="{Binding MenuUserTappedCommand}"
SelectionChangedCommandParameter="{Binding SelectedItem,Source{x:ReferenceMyUserCollectionView}}"
private DelegateCommand<object> _menuUserTappedCommand;
public DelegateCommand<object> MenuUserTappedCommand =>
_menuUserTappedCommand ?? (_menuUserTappedCommand = new DelegateCommand<object>(ExecuteMenuUserTappedCommand));
async void ExecuteMenuUserTappedCommand(object parameter)
{
await App.Current.MainPage.displayAlert("Message","Item " + parameter + " clicked","Ok");
}
我正在尝试从此处访问MenuBody Id属性:
我试图写成parameter.Id
来引用它,但它没有按预期出现。
有人可以帮我找出我哪里错了吗?
解决方法
要么投下
var item = (MenuBody)parameter;
// then you can use item.Id
或使用正确的类型代替对象
public DelegateCommand<MenuBody> MenuUserTappedCommand =>
_menuUserTappedCommand ?? (_menuUserTappedCommand = new DelegateCommand<MenuBody>(ExecuteMenuUserTappedCommand));
async void ExecuteMenuUserTappedCommand(MenuBody parameter)
{
await App.Current.MainPage.DisplayAlert("Message","Item " + parameter.Id + " clicked","Ok");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。