【问题标题】:Xamarin Get ListItem ContextXamarin 获取 ListItem 上下文
【发布时间】:2017-05-29 13:30:15
【问题描述】:

我有一个包含在 ListView 中的用户控件,我想在用户控件基于 listitem 绑定进行初始化时添加动态内容。我不知道该怎么做。请参阅“我如何绑定它???”...我想我应该以某种方式将我的 PropertyBinding 绑定到列表项?

这是我的列表视图的原始视图

    <?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:EngineerApp" x:Class="EngineerApp.GigsPage" NavigationPage.HasNavigationBar="false" xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms">
<ContentPage Title="Gigs" Icon="icon.png">
    <StackLayout Orientation="Vertical">
        <local:HeaderBar LeftButtonText="Back" RightButtonText="Leave" LeftButtonClickEvent="Back" RightButtonClickEvent="Back"></local:HeaderBar>
        <local:ButtonBar LeftButtonText="Add Gig" RightButtonText="Month View" LeftButtonClickEvent="AddGig" RightButtonClickEvent="Back"></local:ButtonBar>
        <ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}"></ActivityIndicator>
        <ListView HasUnevenRows="true" SeparatorVisibility="Default" ItemsSource="{Binding gigs}" ItemSelected="Handle_ItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                      <Frame Padding="20,20,20,20">
                        <Frame.Content>
                          <Frame Padding="15,15,15,15" OutlineColor="Gray" BackgroundColor="White">
                            <Frame.Content>
                              <StackLayout Padding="20,0,0,0"  Orientation="Vertical" HorizontalOptions="CenterAndExpand">
                                <Label Text="{Binding venue}"
                                       HorizontalTextAlignment="Center"
                                       TextColor="#69add1"/>
                                <Label Text="{Binding date}"
                                       HorizontalTextAlignment="Center"
                                       FontFamily="OpenSans-Light"
                                       FontSize="9"
                                       TextColor="#69add1"/>
                                       <local:AuthorisationBar SelectedGig="{Binding .}"></local:AuthorisationBar>         
                              </StackLayout>
                            </Frame.Content>
                          </Frame>
                        </Frame.Content>
                      </Frame>
                    </ViewCell>

                    <local:GigCard />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>
</TabbedPage>

然后是用户控件 xaml。

    <?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EngineerApp.AuthorisationBar">
    <ContentView.Content>
        <StackLayout BackgroundColor="Red" HeightRequest="50" x:Name="barcontent" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
            <Label HorizontalOptions="FillAndExpand" VerticalOptions="Center" Text="I want to show some data here once the bindings are working"></Label>
        </StackLayout>
    </ContentView.Content>
</ContentView>

这是用户控件背后的代码

using System;
using System.Collections.Generic;
using EngineerApp.ViewModels;
using Xamarin.Forms;

namespace EngineerApp
{
    public partial class AuthorisationBar : ContentView
    {
        public static readonly BindableProperty GigProperty = BindableProperty.Create("SelectedGig", typeof(GigViewModel), typeof(AuthorisationBar), new GigViewModel());

        public GigViewModel SelectedGig
        {
            get
            {
                return (GigViewModel)GetValue(GigProperty);
            }

            set
            {
                SetValue(GigProperty, value);
            }
        }

        public AuthorisationBar()
        {
            InitializeComponent();

            BindingContext = this;

        }
    }
}

更新 1 - 更新所有页面以反映最新建议。使用 {Binding .} 我现在得到以下错误:

“无法分配属性“SelectedGig”:属性不存在,或者不可分配,或者值和属性之间的类型不匹配”

【问题讨论】:

  • 您想将您的用户控件与在项目列表中找到的值绑定?
  • 正确。 DataTemplate 包含我的用户控件,因此每次都应该知道它与哪个列表视图项相关。我不知道如何访问我的用户控件中的列表视图项。

标签: xamarin binding xamarin.forms


【解决方案1】:

试试这个:

<local:AuthorisationBar SelectedGig="{Binding .}"></local:AuthorisationBar>

在哪里“。”将是列表的“当前”项。

请注意,我在代码中使用了 SelectedGid,因为这是您定义自定义控件的属性的名称,而不是 SelectedGigId

您还需要从自定义控件的构造函数中删除此行:

BindingContext = SelectedGig;

BindingContext 已经是 GigViewModel 类型

希望这会有所帮助!

更新

您的自定义控件代码应如下所示:

public partial class AuthorisationBar : ContentView
{
    public AuthorisationBar()
    {
        InitializeComponent();
    }

    public static readonly BindableProperty SelectedGigProperty = BindableProperty.Create(nameof(SelectedGig), typeof(GigViewModel), typeof(AuthorisationBar), null);

    public GigViewModel SelectedGig
    {
        get
        {
            return (GigViewModel)GetValue(SelectedGigProperty);
        }
        set
        {
            SetValue(SelectedGigProperty, value);
        }
    }
}

如前所述,您无需为 BindingContext 设置任何内容

更新 2

回答您的问题。错误是您的自定义控件的 BindableProperty 不符合命名约定要求。

来自 Xamarin BindableProperty documentation

可绑定属性的命名约定是可绑定属性标识符必须与 Create 方法中指定的属性名称匹配,并附加“Property”。

这就是为什么将您的 BindableProperty 从 GigProperty 更新为 SelectedGigProperty 可以解决您的问题。

【讨论】:

  • 感谢您的建议,但这也不起作用。我已更正 PropertyBinding 以正确声明 SelectedGig 并返回正确的类型 (GigViewModel),但出现以下错误:“无法分配属性“SelectedGig”:属性不存在,或不可分配,或值和属性之间的类型不匹配"
  • 您能否更新您的原始帖子,说明您所做的更改后的样子?
  • 添加了更新。也只是为了确认gigsGigViewModel 的集合?
  • 这似乎奏效了。你能解释一下为什么这解决了这个问题吗?
【解决方案2】:

我为我当前的项目做了类似的事情 - 将当前项目的 ID 传递给我的 ViewModel(从您发布的代码来看,您似乎没有在 MVVM 范例下使用适当的关注点分离 - 虚拟机应该'不要在你的视图中初始化任何东西):

<Button Text="Reconnect" Command="{Binding Path=BindingContext.ReconnectCommand, Source={x:Reference Name=ServicesList}}" CommandParameter="{Binding}" VerticalOptions="Center"/>

作为参考,传递命令的按钮嵌入在 ListView.DataTemplate 中,并传递它所属的列表项的 ID

【讨论】:

  • 不幸的是,这并没有让我更接近解决方案。我不是试图从我的主 BindingContext 访问某些东西,而是试图从后面的用户控件代码访问列表项视图模型。
  • 啊啊啊。我使用助手来做到这一点 - YourViewModel yvm = new YourViewModel(); 然后 yvm.function()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多