【发布时间】: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