命令未从我的视图模型 [XAMARIN FORMS] 触发
Command is not triggered from my view model [XAMARIN FORMS]
我有一个<ListView>
,它有自己的模板,像这样
Main.xaml
<Label IsVisible="{Binding LabelNbToursIsVisible}"
Text="{Binding LabelNbToursText}"
Margin="0" />
<ListView ItemsSource="{Binding ItemsSource}">
<ListView.ItemTemplate>
<DataTemplate>
<templates:TourCell></templates:TourCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
所以<templates:TourCell>
是另一个有自己视图模型的页面
ViewCell.xaml
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Sv="clr-namespace:enVisite360.Controls"
xmlns:helper="clr-namespace:enVisite360.Helper"
xmlns:ff="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
x:Class="enVisite360.Views.DataTemplates.TourCell"
x:Name="tourCell">
<Grid AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="{OnPlatform Android='0.9,1,0.35,.3', iOS='.9,1,.35,.25'}"
BackgroundColor="Red">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding GoToNext}" />
</Grid.GestureRecognizers>
</Grid>
</ViewCell>
我想触发 GoToNext
命令但什么也没发生
我的 viewModel 命令
ViewCellViewModel.cs
public Command GoToNext
{
get
{
return new Command(async () =>
{
await _page.DisplayAlert("test", "test", "OK");
});
}
}
我有 2 个视图模型,第一个用于 <Label>
,<ListView>
,第二个用于 <DataTemplate>
,但我无法在第二个中触发命令。
我没有看到你的完整代码,所以这里有一个简单的例子供你参考。
在page.xaml.cs中:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class YourPage: ContentPage
{
public ViewCellViewModel viewCellViewModel { get; set; } //for your TourCell
public PageViewModel pageViewModel { get; set; } //for your Label and Listview
public YourPage()
{
InitializeComponent();
viewCellViewModel = new ViewCellViewModel(this);
pageViewModel = new PageViewModel (this);
BindingContext = this;
}
}
在你的page.xaml中:
<ContentPage
...
x:Name="page">
<ContentPage.Content>
<StackLayout>
<Label IsVisible="{Binding pageViewModel.LabelNbToursIsVisible}" Text="{Binding pageViewModel.LabelNbToursText}"
Margin="0" />
<ListView ItemsSource="{Binding pageViewModel.ItemsSource}">
<ListView.ItemTemplate>
<DataTemplate>
<templates:TourCell BindingContext="{Binding Source={x:Reference page},Path=BindingContext.viewCellViewModel}" ></templates:TourCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
然后在你的 TourCell.xaml.cs:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TourCell : ViewCell
{
public TourCell()
{
InitializeComponent();
}
protected override void OnBindingContextChanged()
{
var item = BindingContext as ViewCellViewModel;
if (item == null)
return;
BindingContext = item;
base.OnBindingContextChanged();
}
}
我有一个<ListView>
,它有自己的模板,像这样
Main.xaml
<Label IsVisible="{Binding LabelNbToursIsVisible}"
Text="{Binding LabelNbToursText}"
Margin="0" />
<ListView ItemsSource="{Binding ItemsSource}">
<ListView.ItemTemplate>
<DataTemplate>
<templates:TourCell></templates:TourCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
所以<templates:TourCell>
是另一个有自己视图模型的页面
ViewCell.xaml
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Sv="clr-namespace:enVisite360.Controls"
xmlns:helper="clr-namespace:enVisite360.Helper"
xmlns:ff="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
x:Class="enVisite360.Views.DataTemplates.TourCell"
x:Name="tourCell">
<Grid AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="{OnPlatform Android='0.9,1,0.35,.3', iOS='.9,1,.35,.25'}"
BackgroundColor="Red">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding GoToNext}" />
</Grid.GestureRecognizers>
</Grid>
</ViewCell>
我想触发 GoToNext
命令但什么也没发生
我的 viewModel 命令
ViewCellViewModel.cs
public Command GoToNext
{
get
{
return new Command(async () =>
{
await _page.DisplayAlert("test", "test", "OK");
});
}
}
我有 2 个视图模型,第一个用于 <Label>
,<ListView>
,第二个用于 <DataTemplate>
,但我无法在第二个中触发命令。
我没有看到你的完整代码,所以这里有一个简单的例子供你参考。
在page.xaml.cs中:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class YourPage: ContentPage
{
public ViewCellViewModel viewCellViewModel { get; set; } //for your TourCell
public PageViewModel pageViewModel { get; set; } //for your Label and Listview
public YourPage()
{
InitializeComponent();
viewCellViewModel = new ViewCellViewModel(this);
pageViewModel = new PageViewModel (this);
BindingContext = this;
}
}
在你的page.xaml中:
<ContentPage
...
x:Name="page">
<ContentPage.Content>
<StackLayout>
<Label IsVisible="{Binding pageViewModel.LabelNbToursIsVisible}" Text="{Binding pageViewModel.LabelNbToursText}"
Margin="0" />
<ListView ItemsSource="{Binding pageViewModel.ItemsSource}">
<ListView.ItemTemplate>
<DataTemplate>
<templates:TourCell BindingContext="{Binding Source={x:Reference page},Path=BindingContext.viewCellViewModel}" ></templates:TourCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
然后在你的 TourCell.xaml.cs:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TourCell : ViewCell
{
public TourCell()
{
InitializeComponent();
}
protected override void OnBindingContextChanged()
{
var item = BindingContext as ViewCellViewModel;
if (item == null)
return;
BindingContext = item;
base.OnBindingContextChanged();
}
}