c# - 按钮命令不起作用,集合视图内的按钮
问题描述
我在尝试通过单击按钮调用命令时遇到问题,这是我的代码,请帮助 Postscript,如果我将按钮从 collectionView 中移开,则命令有效。命令的名称是ButtonCommand,我想我需要更多参数,帮助。
MaingPage.Xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="XamarinCollectionView.MainPage"
xmlns:viewModels="clr-namespace:PruebaAppMap.ViewModels"
xmlns:Models="clr-namespace:PruebaAppMap.Models"
x:DataType="viewModels:MainPageViewModel"
BackgroundColor="#2471A3">
<CollectionView ItemsSource="{Binding Products}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="Models:Product">
<Frame
Padding="5"
CornerRadius="5"
IsClippedToBounds="False">
<Grid HeightRequest="100">
<Grid.ColumnDefinitions >
<ColumnDefinition Width=".3*"/>
<ColumnDefinition Width=".7*"/>
<ColumnDefinition Width=".4*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=".5*"/>
<RowDefinition Height=".5*"/>
<RowDefinition Height=".7*"/>
</Grid.RowDefinitions>
<Label Grid.Column="1" FontAttributes="Bold" FontSize="Large" VerticalOptions="Center" Text="{Binding Name}"/>
<Label Grid.Column="1" Grid.Row="1" FontSize="Medium" Text="{Binding Price,StringFormat='{0:C}'}"/>
<Button Grid.Column="3" Grid.RowSpan="2" Grid.Row="1" x:DataType="viewModels:MainPageViewModel" Text="Click me!" CommandParameter="{Binding}" Command="{Binding ButtonCommand}"/>
</Grid>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
视图模型--MainPageViewModel
namespace PruebaAppMap.ViewModels{
public class MainPageViewModel
{
public ObservableCollection<Product> Products { get; set; }
public ICommand ButtonCommand { get; set; }
public MainPageViewModel()
{
Products = new ObservableCollection<Product>
{
new Product
{
Name="Yogurt",
Price=5.50m,
Image="yogurt.png",
hasOffer=false
},
};
ButtonCommand = new Command(async () => await buton());
}
public async Task buton()
{
await App.Current.MainPage.DisplayAlert("Button pressed", "button was pressed", "ok");
}}
}
解决方案
正如 Jason 所说,BindingContext
DataTemplate 中的按钮是Product
. ButtonCommand
没有为产品定义。ButtonCommand 在您的 ViewModel 中,因此您需要参考ViewModel.ButtonCommand
:
首先,给你的 ContentPage 起个名字,让我们说MyPage
:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="MyPage"
x:Class="App266.MainPage">
在您的绑定中:
<Button Grid.Column="3" Grid.RowSpan="2" Grid.Row="1" Text="Click me!" Command="{Binding BindingContext.ButtonCommand, Source={x:Reference MyPage}}"/>
顺便说一句,没有必要写x:DataType
。
推荐阅读
- azure - 在 ItelliJ IDEA 中使用 Java SDK 访问 azure 权限时的 Httpresponse 403
- c# - 我想删除 C# 中的一些文本
- javascript - Firebase 获取 3 级数据
- python - 转换列行中的(数字)字符串,获取每行中这些字符串的最大值/最小值
- linux - 如何在 Makefile 中提供 -C 选项用于交叉编译内核模块?
- javascript - 更改 IMG src 时遇到一个奇怪的问题
- excel - 使用不同 ms excel 表中存在的现金流量计算 XIRR
- javascript - 防止在数组中添加重复值
- arrays - 在jq中用节点替换来自根的路径
- sql - 如何在今天的行中将前 7 天的数据显示为一列?