【发布时间】: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'
其实31348和10227是我要显示的数字。事实上,我在某些时候将它们表示为绑定上下文。但我想“改变”那个 Binding 以便我可以调用 LocalCommand 方法。可能在对象中实现 LocalCommand 可以解决问题,但我绝对不想这样做!
问题:
- 有没有更好更简单的方法来做到这一点?
- 如何才能“恢复”与 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