【问题标题】:Binding ICommand in Xamarin Forms Content View在 Xamarin 表单内容视图中绑定 ICommand
【发布时间】:2019-11-04 12:15:46
【问题描述】:

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

这是我的 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}"/>

但它会引发错误:

【问题讨论】:

  • 我可以看看你的 ViewModel 吗?
  • 可以吗?
  • 编译不好。我问题中的代码工作得很好..!!!我又重新编译了。

标签: xamarin xamarin.forms binding icommand


【解决方案1】:

你做对了,但只缺少一件小事,那就是 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>

【讨论】:

  • 感谢您的回答。我的编译不好。我在问题中提到的代码工作正常。我只是清理和重建。
【解决方案2】:
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), typeof(ICommand), typeof(HeaderView), null);
public ICommand CloseButtonClick
{
    get => (ICommand)GetValue(CloseButtonClickedProperty);
    set => SetValue(CloseButtonClickedProperty, value);
}

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

【讨论】:

  • 感谢您的回答。我的编译不好。我在问题中提到的代码工作正常。我只是清理和重建。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多