【问题标题】:BindingContext with command and parameter bindingBindingContext 与命令和参数绑定
【发布时间】:2019-12-25 14:20:12
【问题描述】:

我有一个 ViewModel,我从中构造了一个对象集合。我希望能够在我的视图上绑定我的对象,同时能够使用我的button

目前,我的属性已正确绑定到我的标签(partnerLogo、partnerText、partnerLink)。不幸的是,我无法使用按钮命令发送我绑定的命令参数。

视图模型

Public class CarouselViewModel : INotifyPropertyChanged
{
    public CarouselViewModel()
    {
        partners = new ObservableCollection<Carousel>()
        {
            new Carousel(){partnerText = "Test123", partnerLogo = "Test123", partnerLink = "Test123" },
        };

        openWebsite = new Command<string>((key) =>
        {
            Device.OpenUri(new Uri(key));
        });
    }
    public ObservableCollection<Carousel> partners
    {
        get;
        set;
    }
    public ICommand openWebsite
    {
        private set;
        get;
    }
}

XAML:

<CarouselView ItemsSource="{Binding partners}">
    <CarouselView.ItemsLayout>
        <GridItemsLayout/>
             </CarouselView.ItemsLayout>
                 <CarouselView.ItemTemplate>
                     <DataTemplate>
                          <Frame>
                              <StackLayout>
                                  <Image Source="{Binding partnerLogo}"/>
                                  <Label Text="{Binding partnerText}"/>
                                  <Button Text="Read" Command="{Binding openWebsite}" CommandParameter="{Binding partnerLink}"/>
                             </StackLayout>
                         </Frame>
                     </DataTemplate>
            </CarouselView.ItemTemplate>
</CarouselView>

如何访问我的按钮命令,同时能够发送命令参数?

【问题讨论】:

标签: c# xamarin xamarin.forms


【解决方案1】:

你可以做的是,给你正在使用的内容页面一个x:name:-

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyProject;assembly= MyProject"
             x:Class="MyProject.Views.MyPage"
             x:Name="ThisPage">

然后将 Source 提供给 Command。像这样。此方法将调用当前页面的 ViewModel 中的命令。此外,您可以使用命令参数来了解哪个项目触发了命令。

<CarouselView ItemsSource="{Binding partners}">
    <CarouselView.ItemsLayout>
        <GridItemsLayout/>
             </CarouselView.ItemsLayout>
                 <CarouselView.ItemTemplate>
                     <DataTemplate>
                          <Frame>
                              <StackLayout>
                                  <Image Source="{Binding partnerLogo}"/>
                                  <Label Text="{Binding partnerText}"/>
                                  <Button Text="Read" Command="{Binding openWebsite, Source={x:Reference Name=ThisPage}}" CommandParameter="{Binding partnerLink}"/>
                             </StackLayout>
                         </Frame>
                     </DataTemplate>
            </CarouselView.ItemTemplate>
</CarouselView>

【讨论】:

    【解决方案2】:

    对于CarouselView 中的每个项目,绑定上下文将是绑定到ItemsSource 的基础数据项目,即Carousel。对于CarouselView,绑定上下文将是您的ViewModel如果您已定义它

    要解决此问题,请在 XAML 中将 x:Name="mycaroselview" 设置为 CarouselView,并将其路径引用引用到 Button 命令,如下面的代码示例所示。

    <CarouselView x:Name="MyCarousel" ItemsSource="{Binding partners}">
    <CarouselView.ItemsLayout>
        <GridItemsLayout/>
             </CarouselView.ItemsLayout>
                 <CarouselView.ItemTemplate>
                     <DataTemplate>
                          <Frame>
                              <StackLayout>
                                  <Image Source="{Binding partnerLogo}"/>
                                  <Label Text="{Binding partnerText}"/>
                                  <Button Text="Read" Command="{Binding BindingContext.openWebsite, Source={x:Reference Name=MyCarousel}}" CommandParameter="{Binding partnerLink}"/>
                             </StackLayout>
                         </Frame>
                     </DataTemplate>
            </CarouselView.ItemTemplate>
    

    确保您必须将绑定上下文设置为ContentPageCarouselView

    问候,
    迪内什

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多