小编Paz*_*ear的帖子

按钮命令绑定未在Xamarin.Forms中调用binded命令

我目前正在使用ListView创建一个页面,该ListView由我从Web API恢复的数据填充.
我添加了2个按钮:一个用于刷新数据,另一个用于在设备上本地存储数据.问题是:它们都不起作用,绑定不会在日志中产生错误所以我猜它确实将命令绑定到按钮,但是当我点击它时,命令不会被调用.

XAML

<ListView ItemsSource="{Binding Items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal">
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer CommandParameter="{Binding ItemId}">
                            <TapGestureRecognizer.Command>
                                <Binding Source="{x:Reference Page}">
                                    <Binding.Path>BindingContext.ShowItemDataCommand</Binding.Path>
                                </Binding>
                            </TapGestureRecognizer.Command>
                        </TapGestureRecognizer>
                    </StackLayout.GestureRecognizers>
                    <Switch IsToggled="{Binding IsOffline}"/>
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding ItemTitle}"/>
                        <Label Text="{Binding ItemDescription}" FontSize="Micro"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
<ListView.Footer>
    <StackLayout>
        <Button Text="Refresh items" HorizontalOptions="CenterAndExpand" Command="{Binding RefreshItemsCommand}"/>
        <Button Text="Save item data" HorizontalOptions="CenterAndExpand" Command="{Binding SaveItemData}"/>
    </StackLayout>
</ListView.Footer>
</ListView>
Run Code Online (Sandbox Code Playgroud)

ViewModel中的命令

public class ItemsViewModel : INotifyPropertyChanged
{
    (...)
    public ItemsViewModel() {
        (...)
        RefreshItemsCommand = new Command(GetItemsObservableCollection);
        TogglePacksAvailabilityCommand = new Command(async …
Run Code Online (Sandbox Code Playgroud)

c# data-binding xaml mvvm xamarin.forms

4
推荐指数
1
解决办法
6141
查看次数

标签 统计

c# ×1

data-binding ×1

mvvm ×1

xamarin.forms ×1

xaml ×1