【问题标题】:List of Buttons with Binding and Command in XamarinXamarin 中具有绑定和命令的按钮列表
【发布时间】:2021-07-21 19:31:47
【问题描述】:

我正在开发一个 Xamarin 应用程序,我想在其中动态显示类注册表中的注册表号列表。
显示号码列表后,用户应选择其中一个号码登录App。

我已决定将此列表显示为按钮列表,因为一旦用户单击它,就无需执行任何其他操作。但是,大多数关于 Binding 和 ListView 的文档都没有使用 Buttons 作为显示元素。

我已决定按照excellent video 上的步骤操作,但我不断收到以下错误:

Binding: 'LocalCommand' property not found on '31348', target property: 'Xamarin.Forms.Button.Command'
Binding: 'LocalCommand' property not found on '10227', target property: 'Xamarin.Forms.Button.Command'

其实3134810227是我要显示的数字。事实上,我在某些时候将它们表示为绑定上下文。但我想“改变”那个 Binding 以便我可以调用 LocalCommand 方法。可能在对象中实现 LocalCommand 可以解决问题,但我绝对不想这样做!

问题:

  1. 有没有更好更简单的方法来做到这一点?
  2. 如何才能“恢复”与 LocalCommand 的绑定?

我还在学习 Binding,所以任何提示都会非常有用!
谢谢!

RegistryPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behavior1="clr-namespace:SPConsigMobile.Utils"
             x:Class="App.Views.RegistryPage"
             BackgroundColor="{StaticResource AppBackgroundColor}"
             Title="App"
             NavigationPage.HasNavigationBar="false">
    <ContentPage.Content>
        <ListView x:Name="RegistryView"
                    ItemsSource="{Binding User.Registry}"
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Button Text="{Binding Number}"
                                Command="{Binding LocalCommand}"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

RegistryPage.xaml.cs

namespace App.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RegistryPage : ContentPage
    {
        public RegistryPage()
        {
            InitializeComponent();
            RegistryView.ItemsSource = User.Registries;
        }
        public ICommand LocalCommand => new Command(CommandClicked);

        private void CommandClicked()
        {
            Console.WriteLine("Command Button Clicked!");
        }
    }
}

【问题讨论】:

    标签: c# xaml listview button binding


    【解决方案1】:

    通常,当您设置控件的 ItemSource 属性时(在本例中为 ListView),您还必须设置 DataTemplate。

    现在,DataTemplate 中视图的 BindingContext 是您已绑定到的集合的一项。在您的情况下,因为您已将 ItemSource 设置为 PhoneNumber 的集合,所以每个视图的 bindingContext 都是 PhoneNumber。

    因此,当您尝试使用“Command="{Binding LocalCommand}"' 访问您的命令时,您所做的是在 PhoneNumber 类中搜索 LocalCommand 属性。相反,您需要在 Page 类中搜索它。因此,使用 x:Name 为您的 ContentPage 命名,然后将命令绑定的源引用为 Root Page,将 Path 作为命令的路径,从 Source 开始(在我的示例中为 NumberSelectedCommand)。命令参数应该是数字,所以它是一个空路径绑定。

    XAML

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="App1.RegistryPage"
                 x:Name="Root"
                 >
    
        <StackLayout>
            <ListView x:Name="RegistryView"
                        ItemsSource="{Binding User.Registry}"
                <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Button Text="{Binding Number}"
                                Command="{Binding Source={x:Reference Root}, Path=NumberSelectedCommand}"
                                CommandParameter="{Binding}"
                                />
                    </ViewCell>
                </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    
    </ContentPage>
    

    RegistryPage.xaml.cs

        [XamlCompilation(XamlCompilationOptions.Compile)]  
        public partial class RegistryPage : ContentPage
        {
            public RegistryPage()
            {
                InitializeComponent();
                RegistryView.ItemsSource = User.Registries;
                NumberSelectedCommand = new Command<PhoneNumber>(OnNumberSelected);
            }
    
    
            // Commands
            public ICommand NumberSelectedCommand { get; }
    
    
            // Commands Handlers
            private void OnNumberSelected(PhoneNumber selectedNumber)
            {
                // Do what you need with selected number.
            }
        }
    

    【讨论】:

    • 感谢您的帮助!这为我解决了!除了CommandParameter="{Binding}",我实际使用了CommandParameter="{Binding Number}"
    猜你喜欢
    • 2019-05-11
    • 2017-04-16
    • 2019-07-30
    • 2015-06-21
    • 1970-01-01
    • 2013-10-09
    • 2020-03-06
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多