在Xamarin表单内容视图中绑定ICommand

我有一个自定义视图,该视图将在许多页面上使用。我在自定义视图中有一个关闭按钮,我需要在MainViewModel中绑定“关闭按钮”命令。任何帮助将不胜感激。

这是我的HeaderView.xaml.cs文件

public partial class HeaderView : ContentView
{
    public HeaderView ()
    {
        InitializeComponent ();
    }
    public static readonly BindableProperty CloseButtonClickedProperty = BindableProperty.Create(nameof(CloseButtonClick),typeof(ICommand),typeof(HeaderView),null);
    public ICommand CloseButtonClick
    {
        get => (ICommand)Getvalue(CloseButtonClickedProperty);
        set => Setvalue(CloseButtonClickedProperty,value);
    }

}

这是HeaderView.Xaml文件中使用的关闭按钮代码

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    HeightRequest="65"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="NameSpace.Views.HeaderView" x:Name="headerView">
            <Image  x:Name="CloseButton" Source="ic_closewhite.png" WidthRequest="20" HeightRequest="20" VerticalOptions="CenterAndExpand"  HorizontalOptions="EndAndExpand">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding CloseButtonClick,Source={x:Reference headerView}}"  />
                </Image.GestureRecognizers>
            </Image>
</ContentView>

这是我要在MainView.xaml中使用命令的地方。

<c:HeaderView CloseButtonClick ="{Binding CloseButtonClickCommand}"/>

但是它抛出一个错误:

在Xamarin表单内容视图中绑定ICommand

beckyang123 回答:在Xamarin表单内容视图中绑定ICommand

您做得正确,但仅缺少一小件事,即ContentView HeaderView的x:Name。 只需在HeaderView的Xaml中包含这一行代码即可。

x:Name="headerView"

这是您修改过的Xaml:-

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    HeightRequest="65"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Name="headerView"
    x:Class="NameSpace.Views.HeaderView" x:Name="headerView">
            <Image  x:Name="CloseButton" Source="ic_closewhite.png" WidthRequest="20" HeightRequest="20" VerticalOptions="CenterAndExpand"  HorizontalOptions="EndAndExpand">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding CloseButtonClick,Source={x:Reference headerView}}"  />
                </Image.GestureRecognizers>
            </Image>
</ContentView>
,
public static readonly BindableProperty CloseButtonClickedProperty = BindableProperty.Create(nameof(CloseButtonClick),typeof(ICommand),typeof(HeaderView),null);

问题就在这行上,正如错误消息所言,

  找到

个“ CloseButtonClick”事件,或值之间的类型不匹配   和财产

您应将CloseButtonClickedProperty更改为CloseButtonClickProperty

public static readonly BindableProperty CloseButtonClickedProperty = BindableProperty.Create(nameof(CloseButtonClick),null);
public ICommand CloseButtonClick
{
    get => (ICommand)GetValue(CloseButtonClickedProperty);
    set => SetValue(CloseButtonClickedProperty,value);
}

public static readonly BindableProperty CloseButtonClickProperty = BindableProperty.Create(nameof(CloseButtonClick),null);
public ICommand CloseButtonClick
{
    get => (ICommand)GetValue(CloseButtonClickProperty);
    set => SetValue(CloseButtonClickProperty,value);
}
本文链接:https://www.f2er.com/3166832.html

大家都在问