C# (Xamarin) - 如何从 "ObservableCollection<T>" 访问对象属性?
C# (Xamarin) - How to access an object attribute from an "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");
}
我有这个功能可以创建一个 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");
}