如何向 ContentView 发送参数?

How to send an argument to ContentView?

我正在尝试创建一个 ContentPage,其中包含来自 XamarinCommunityToolkitTabView

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

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

更新:

我已尝试使用 BindablePropertiy,但在调试器中,我可以看到已收到新值,但标签中未显示任何内容:

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>

类别class:

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'

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

 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>