Xamarin 中具有绑定和命令的按钮列表
List of Buttons with Binding and Command in Xamarin
我正在开发 Xamarin 应用程序,我想在其中动态显示来自 Class 注册表的注册表编号列表。
显示号码列表后,用户应选择其中一个号码登录应用程序。
我决定将此列表显示为按钮列表,因为一旦用户单击它,就无需执行任何其他操作。但是,大多数关于 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?
我仍在学习绑定,所以任何提示都非常有用!
谢谢!
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!");
}
}
}
通常,当您设置控件的 ItemSource 属性(在本例中是您的 ListView)时,您还必须设置 DataTemplate。
现在,DataTemplate 中视图的 BindingContext 是您绑定到的集合中的一项。在您的情况下,因为您已将 ItemSource 设置为 PhoneNumber 的集合,所以每个视图的 bindingContext 都是一个 PhoneNumber。
因此,当您尝试使用 'Command="{Binding LocalCommand}"' 访问您的命令时,您正在做的是在 PhoneNumber class 中搜索 LocalCommand 属性。您需要的是在您的页面 class 中搜索它。因此,使用 x:Name 为您的 ContentPage 命名,然后将您的命令绑定的源引用为根页面,将路径引用为命令的路径,从源开始(因此在我的示例中为 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.
}
}
我正在开发 Xamarin 应用程序,我想在其中动态显示来自 Class 注册表的注册表编号列表。
显示号码列表后,用户应选择其中一个号码登录应用程序。
我决定将此列表显示为按钮列表,因为一旦用户单击它,就无需执行任何其他操作。但是,大多数关于 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?
我仍在学习绑定,所以任何提示都非常有用!
谢谢!
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!");
}
}
}
通常,当您设置控件的 ItemSource 属性(在本例中是您的 ListView)时,您还必须设置 DataTemplate。
现在,DataTemplate 中视图的 BindingContext 是您绑定到的集合中的一项。在您的情况下,因为您已将 ItemSource 设置为 PhoneNumber 的集合,所以每个视图的 bindingContext 都是一个 PhoneNumber。
因此,当您尝试使用 'Command="{Binding LocalCommand}"' 访问您的命令时,您正在做的是在 PhoneNumber class 中搜索 LocalCommand 属性。您需要的是在您的页面 class 中搜索它。因此,使用 x:Name 为您的 ContentPage 命名,然后将您的命令绑定的源引用为根页面,将路径引用为命令的路径,从源开始(因此在我的示例中为 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.
}
}