【问题标题】:Commands of BindableLayout list inside list列表内的 BindableLayout 列表的命令
【发布时间】:2022-01-20 07:32:14
【问题描述】:

我正在开发 Xamarin Forms 应用程序并尝试在 Listview 中实现绑定列表的功能。早些时候,我有一个包含“普通”类型的类,例如字符串、长整数、整数等。

现在我被要求添加列表(所以它是列表中的列表)并为其添加功能(特别是如果检查了给定的列表记录,则有一些复选框应该获取信息,当然是在谈论主列表中的嵌套项目) .

使用 BindableLayout,我实际上可以将其设为“只读”,这意味着我现在可以看到每个列表项中的列表。问题是我无法将命令绑定到该内部列表(我想这是因为现在不同的路径)。

请记住,我已经使用分组来对这些项目进行分组。所以结构是这样的:

按类中的属性分组 -> 项目列表视图 -> 在每个项目记录中我都有这个嵌套列表。

我不知道(当然,如果可能的话)如何为这些内部项目设置路径。也许还有其他方法可以使其发挥作用。对我来说,使用复选框从嵌套项传递参数也很重要。

我的 XAML 看起来与此类似:

<ListView ItemsSource="{Binding Items}">
<...>
<ListView.ItemTemplate>
 <DataTemplate>
  <ViewCell>
   <ViewCell.View>
    <Label Text="{Binding ItemText}">
     <StackLayout BindableLayout.ItemsSource="{Binding Positions}"> 
      <BindableLayout.ItemTemplate>
       <DataTemplate>
        <Label Text="{Binding PositionId}">
        <CheckBox />
       </DataTemplate>
      </BindableLayout.ItemTemplate>
     </StackLayout>
    <ViewCell.View>
   <ViewCell>
  </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

物品型号:

public class Items
{
 public string ItemText {get; set;}
 public List<Positions> Positions {get; set;}
}

职位模型:

public class Positions
{
 public long? PositionId {get; set;}
}

创建此页面是通过从上一页中获取来完成的,如下所示:

public override Page GivePage ()
    {
        ContentPage view = new ItemsView();
        var controller = new ItemsViewModel();
        view.BindingContext = controller;
        return view;
    }

在 ViewModel 中,我有从 API、命令等获取数据的方法。 我唯一想念的是基本上处理这个嵌套项目的命令(同样重要的是我需要同时获取 PositionId 和 ItemText)。 非常感谢任何帮助或建议,我为此苦苦挣扎了很长一段时间。

编辑:

GeraldCodingLumis 的大力帮助下 我终于明白我做错了什么,我应该如何绑定它等等。非常感谢你!

【问题讨论】:

  • 我不太确定我是否明白你在这里问什么,但我想我已经制作了一些可能对这里有帮助的视频:youtube.com/… 如果有帮助,请告诉我 :)
  • 我也不确定您的意思,但是您可以放置​​一个适当的命令来处理您的任一“模型”中的复选框更改事件,并且绑定路径会很简单。如果您真的想将所有内容放在页面级视图模型中,那么您可以使用 RelativeBindings 指定绑定源所需的路径:docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 对不起,伙计们,如果我没有清楚地描述它(我也无法分享具体的代码,因为这是公司代码),但这就像我在 Stack 上的第二或第三个帖子。如前所述,基本上不会为嵌套列表触发命令,这可能是因为更改了路径。 @GeraldVersluis 非常感谢杰拉德。我实际上正在浏览视频,希望我能更好地理解它;)
  • @CodingLumis 嗯,模型命令似乎合理,我试试看,谢谢。
  • @GeraldVersluis 哇,第一个视频,我已经可以调用命令了,非常感谢!现在唯一的问题是以某种方式传递参数。

标签: c# .net xamarin xamarin.forms


【解决方案1】:

在得到 Gerald Versluis 和 CodingLumis 的帮助后,我能够解决我的问题并传递参数。我已经命名了我的 ListView,例如“myList”,然后用复选框引用它。

我刚刚做到了(根据 Gerald 的视频):

xmlns:behaviorsPack="clr-namespace:Xamarin.Forms.BehaviorsPack;assembly=Xamarin.Forms.BehaviorsPack"

<CheckBox>
 <CheckBox.Behaviors>
  <behaviorsPack:EventToCommandBehavior EventName="CheckedChanged"
   Command="{Binding Path=BindingContext.CheckedChangedCommand,
   Source={Reference myList}}"
   CommandParameter="{Binding .}"/>
 </CheckBox.Behaviors>
</Checkbox>

行为包仅用于将 CheckedChanged 事件处理为命令。

在我的 ViewModel 中,它看起来像:

// In class, added command
public ICommand CheckedChangedCommand { get; set; }    

// Then in ctor
CheckedChangedCommand = new Command<Positions>(CheckedChanged);

// and finally, method called by command
private async void CheckedChanged(object obj)
{
    var position = obj as Positions;
}

非常感谢你们的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多