C# 如何在ContentView中链接XAML端的对象?

C# 如何在ContentView中链接XAML端的对象?,c#,mvvm,xamarin.forms,C#,Mvvm,Xamarin.forms,我正在尝试制作一个模板来集中显示产品 我成功地使用了x:Name,但我想在XAML端进行这种绑定,就像一个页面一样 为什么这不起作用 MainPage.cs <?xml version="1.0" encoding="utf-8" ?> <base:PageBase x:Class="MobileApp.Pages.Main.MainPage" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="

我正在尝试制作一个模板来集中显示产品

我成功地使用了x:Name,但我想在XAML端进行这种绑定,就像一个页面一样

为什么这不起作用

MainPage.cs

<?xml version="1.0" encoding="utf-8" ?>
<base:PageBase
    x:Class="MobileApp.Pages.Main.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:main="clr-namespace:MobileApp.Pages.Main"
    xmlns:templates="clr-namespace:MobileApp.Pages.Templates"
    x:DataType="main:MainPageViewModel">
    <StackLayout Padding="10">
        <StackLayout>
            <templates:ProductTemplate Product="{Binding SelectedProduct}" />
        </StackLayout>
    </StackLayout>
</base:PageBase>

我认为您需要在Xamarin表单中设置
BindingContext

所以我认为这已经给了你结果:

this.BindingContext = this;
但请不要这样做。将您的
产品
包装到另一个型号中,或将
绑定上下文
设置为
产品
,并直接绑定到
代码
名称

this.BindingContext=this.Product

并绑定:

<StackLayout>
        <Label Text="{Binding Code}" />
        <Label Text="{Binding Name}" />
</StackLayout>

您可以在自定义视图的xaml中设置绑定路径

ProductTemplate.xaml 在MainPage.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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
              x:Name="CustomView"  // set the name of view
             x:Class="App18.ProductTemplate">
    <ContentView.Content>
        <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
            <Label Text="{Binding Product.Code,Source={x:Reference CustomView}}" />
            <Label Text="{Binding Product.Name,Source={x:Reference CustomView}}" />
        </StackLayout>
    </ContentView.Content>
</ContentView>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ProductTemplate : ContentView, INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ProductTemplate()
    {
        InitializeComponent();
      
    }

    public static readonly BindableProperty ProductProperty = BindableProperty
    .Create(nameof(Product),
    typeof(Product),
    typeof(ProductTemplate),
    default(Product),
    propertyChanged: (obj,oldValue,newValue) =>{


        var bindableObj = obj as ProductTemplate;

        bindableObj.Product = newValue as Product;

    });

    Product product;
    public Product Product
    {
        get {

            return product;
          
        }

        set
        {
            if(product != value)
            {
                product = value;
                NotifyPropertyChanged("Product");
            }
        }
    }
}
<StackLayout  VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">

    <local:ProductTemplate WidthRequest="300" HeightRequest="400" BackgroundColor="LightPink" Product="{Binding xxx}"/>
      
</StackLayout>