【问题标题】:How to send an argument to ContentView?如何向 ContentView 发送参数?
【发布时间】:2021-05-05 16:27:54
【问题描述】:

我正在尝试创建一个包含来自XamarinCommunityToolkitTabViewContentPage

假设选项卡定义了一个ObservableCollection 类别,每个TabViewItem 都应该加载一个 ContentView 并将 GroupId 作为参数/属性传递,然后我使用该 GroupId 来过滤产品列表。

将参数传递给 ContentView 的最佳方式是什么?

更新:

我尝试使用 BindablePropertiy,但在调试器中,我可以看到收到的 newValue,但标签中没有显示任何内容:

MainPage.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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup- 
compatibility/2006"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             mc:Ignorable="d"
             x:Class="mynamespace.Views.MainPage"
             Title="{Binding Title}"
             xmlns:local="clr-namespace:mynamespace.Views"
             xmlns:vm="clr-namespace:mynamespace.ViewModels"
             xmlns:model="clr-namespace:mynamespace.Models"  
             x:Name="MainPage">
    <ContentPage.Content>
        <xct:TabView Grid.Row="0"
                 TabStripPlacement="Top"
                 TabStripBackgroundColor="White"
                 TabStripHeight="48"
                 TabIndicatorColor="Orange"
                 TabIndicatorHeight="2"
                 TabItemsSource="{Binding Categories}">
        <xct:TabView.TabViewItemDataTemplate>
            <DataTemplate>
                <Grid>
                <Label Text="{Binding Name}"
                       FontAttributes="Bold"
                       VerticalOptions="Center"
                       Padding="6, 0"/>
                </Grid>
            </DataTemplate>
        </xct:TabView.TabViewItemDataTemplate>
        <xct:TabView.TabContentDataTemplate>
            <DataTemplate>
                <local:GroupView GroupId="{Binding Id}" />
            </DataTemplate>
        </xct:TabView.TabContentDataTemplate>
    </xct:TabView>
</ContentPage.Content>

GroupView.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace mynamespace.Views
{
    public partial class GroupView : ContentView
    {        
        public  string GroupId
        {
            get { return (string)GetValue(GroupIdProperty); }
            set { SetValue(GroupIdProperty, value); }
        }

        public static readonly BindableProperty GroupIdProperty = BindableProperty.Create(
            nameof(GroupId),
            typeof(string),
            typeof(GroupView),
            "Default_V",
            defaultBindingMode: BindingMode.OneWay,
            propertyChanged: GroupIdChanged
            );

        private static void GroupIdChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (ProductListPage)bindable;
            control.GroupId = newValue?.ToString();
            
        }

        public GroupView()
        {
            InitializeComponent();
            BindingContext = this;
         }
    }
}

GroupView.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="mynamespace.Views.GroupView">
  <ContentView.Content>
      <StackLayout>
            <Label Text="{Binding GroupId}" /> <!-- Shows nothing -->
        </StackLayout>
  </ContentView.Content>
</ContentView>

类别类:

public class Category
    {
        private string id;
        private string name;
        private string description;

        public string Id { get => id; set => id = value; }
        public string Name { get => name; set => name = value; }
        public string Description { get => description; set => description = value; }
    }

ProductListViewModel.cs

public class ProductListViewModel : BaseViewModel
    {
        public string GroupId { get; set; }

        public ProductListViewModel()
        {

        }

        public ProductListViewModel(string groupId)
        {
            GroupId = groupId;
        }
    }

更新:

[0:] Binding: 'GroupId' property not found on 'mynamespace.Models.Category', target property: 'Xamarin.Forms.Label.Text'

【问题讨论】:

  • Id 定义在哪里?你的ContentPageBindingContext 是什么?
  • IdCategory 对象的属性。并且因为TabView 使用TabItemsSource="{Binding Categories}" 它会得到当前选中Tab 的Id

标签: c# xaml xamarin.forms data-binding xamarin-community-toolkit


【解决方案1】:

不要在自定义控件内部分配绑定。你可以这样做:

 public partial class GroupView : ContentView
    {

       GroupViewModel _viewModel;
   
        public string GroupId
        {
            get { return (string)GetValue(GroupIdProperty); }
            set { SetValue(GroupIdProperty, value); }
        }

        public static readonly BindableProperty GroupIdProperty = BindableProperty.Create(
            nameof(GroupId),
            typeof(string),
            typeof(GroupView),
            "Default_V",
            defaultBindingMode: BindingMode.OneWay,
            propertyChanged: GroupIdChanged
            );

        private static void GroupIdChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (GroupView)bindable;
            control.GroupId = (string)newValue;
            control.label.Text = control.GroupId;


        }

        public GroupView()
        {
            InitializeComponent();
        }
}

然后在 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="mynamespace.Views.GroupView">
  <ContentView.Content>
    <StackLayout>
      <Label x:Name="label"  /> 
    </StackLayout>
  </ContentView.Content>
</ContentView>

【讨论】:

  • 啊,好吧,我认为应该在 MainPage 的 BindingContext 中定义 Id ?否则,如何将 Category.Id 参数传递给 GroupView(TabItemsSource="{Binding Categories}"ObservableCollection&lt;Category&gt;
  • 我更新了我的问题,并在控制台上得到了新的输出。
  • @NadjibRebahi 你能告诉我你的ProductListViewModelCategory here
  • 我已经添加了Category类,我不再使用ProductListViewModel了。
  • @NadjibRebahi 这是我的错误。我已经更新了我的答案,你可以检查一下。
猜你喜欢
  • 1970-01-01
  • 2017-05-01
  • 2012-12-01
  • 2021-12-06
  • 2023-03-31
  • 1970-01-01
  • 2010-12-25
  • 2014-10-16
  • 1970-01-01
相关资源
最近更新 更多