gpt4 book ai didi

c# - 按钮命令不起作用,按钮位于 Collection View 内

转载 作者:行者123 更新时间:2023-12-03 08:43:07 25 4
gpt4 key购买 nike

我在尝试通过单击按钮调用命令时遇到问题,这是我的代码,请帮忙后记,如果我将按钮从 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>

View Model --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 所说,DataTemplate 内按钮的 BindingContextProduct 的一个实例。未为产品定义 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">

在您的绑定(bind)中:

<Button Grid.Column="3" Grid.RowSpan="2" Grid.Row="1" Text="Click me!" Command="{Binding BindingContext.ButtonCommand, Source={x:Reference MyPage}}"/>

顺便说一句,不需要写x:DataType

引用:Xamarin Button Command (inside of ListView.ItemTemplate) Not Firing

关于c# - 按钮命令不起作用,按钮位于 Collection View 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com