我有一个自定义视图,该视图将在许多页面上使用。我在自定义视图中有一个关闭按钮,我需要在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}"/>