使用 Prism Xamarin 表单创建动态 TabbedPage
Creating Dynamic TabbedPage using Prism Xamarin forms
我正在将 Prism 与项目源一起使用,在每个选项卡式页面中都有一个列表视图,我无法利用 "ItemTapped" 事件作为命令行为的事件,以便在列表视图项目是时触发视图模型中的命令点击,但是当我点击列表视图项目时在调试模式下没有触发器,请帮助我理解为什么会发生这种情况,是否有任何替代方法可以使用命令来实现。此外,当我检查事件在 PrismTabbedPage1.xaml.cs 中触发但在 PrismTabbedPage1ViewModel.cs
中未触发时
Prism Xamarin 形式
PrismTabbedPage1.Xaml
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" xmlns:behaviors="clr-namespace:Prism.Behaviors;assembly=Prism.Forms" prism:ViewModelLocator.AutowireViewModel="True" x:Class="testapp.Views.PrismTabbedPage1" ItemsSource="{Binding countries}"> <TabbedPage.ItemTemplate>
<DataTemplate>
<ContentPage Title="{Binding CountryName}">
<ListView ItemsSource="{Binding Cities}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding CityName}" TextColor="Black"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Behaviors>
<behaviors:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ListItemTapped}" EventArgsParameterPath="Item"/>
</ListView.Behaviors> </ListView>
</ContentPage>
</DataTemplate>
</TabbedPage.ItemTemplate>
</TabbedPage>`
PrismTabbedPage1ViewModel.cs
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using testapp.Models;
namespace testapp.ViewModels
{
public class PrismTabbedPage1ViewModel : BindableBase
{
public ObservableCollection<Country> countries { get; set; }
public DelegateCommand<Country> ListItemTapped => new DelegateCommand<Country>(OnTapped);
private void OnTapped(Country obj)
{
//NavigationService.NavigateAsync("MainPage");
}
public PrismTabbedPage1ViewModel()
{
ObservableCollection<City> Cities1 = new ObservableCollection<City>()
{
new City(){ CityName = "City1" },new City(){ CityName = "City2" },new City(){ CityName = "City3" },
new City(){ CityName = "City4" },new City(){ CityName = "City5" },new City(){ CityName = "City6" },
new City(){ CityName = "City7" },new City(){ CityName = "City8" },new City(){ CityName = "City9" },
};
countries = new ObservableCollection<Country>()
{
new Country(){ CountryName = "Country 1", Cities = Cities1 },
new Country(){ CountryName = "Country 2", Cities = Cities1 },
new Country(){ CountryName = "Country 3", Cities = Cities1 },
};
}
}
}
Country.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace testapp.Models
{
public class Country
{
public string CountryName { get; set; }
public ObservableCollection Cities { get; set; }
}
public class City
{
public string CityName { get; set; }
}
}
在此代码中,未触发 listview 命令行为。
我不熟悉 Prism,但由于您的 EventToCommand 在 ListView 中,您的默认绑定是 listview 传递的数据,而不是您的 ViewModel。
您可以尝试这样的操作:
<behaviors:EventToCommandBehavior EventName="ItemTapped"
Command="{Binding Source={x:Reference this},Path=BindingContext.ListItemTapped}"
EventArgsParameterPath="{Binding .}"/>
并为您的页面命名:
<TabbedPage .... x:Name="this">
因此您在列表视图中的绑定应该有效地重定向到视图模型中的命令。
希望对您有所帮助。
我正在将 Prism 与项目源一起使用,在每个选项卡式页面中都有一个列表视图,我无法利用 "ItemTapped" 事件作为命令行为的事件,以便在列表视图项目是时触发视图模型中的命令点击,但是当我点击列表视图项目时在调试模式下没有触发器,请帮助我理解为什么会发生这种情况,是否有任何替代方法可以使用命令来实现。此外,当我检查事件在 PrismTabbedPage1.xaml.cs 中触发但在 PrismTabbedPage1ViewModel.cs
中未触发时Prism Xamarin 形式
PrismTabbedPage1.Xaml
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" xmlns:behaviors="clr-namespace:Prism.Behaviors;assembly=Prism.Forms" prism:ViewModelLocator.AutowireViewModel="True" x:Class="testapp.Views.PrismTabbedPage1" ItemsSource="{Binding countries}"> <TabbedPage.ItemTemplate>
<DataTemplate>
<ContentPage Title="{Binding CountryName}">
<ListView ItemsSource="{Binding Cities}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding CityName}" TextColor="Black"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Behaviors>
<behaviors:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ListItemTapped}" EventArgsParameterPath="Item"/>
</ListView.Behaviors> </ListView>
</ContentPage>
</DataTemplate>
</TabbedPage.ItemTemplate>
</TabbedPage>`
PrismTabbedPage1ViewModel.cs
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using testapp.Models;
namespace testapp.ViewModels
{
public class PrismTabbedPage1ViewModel : BindableBase
{
public ObservableCollection<Country> countries { get; set; }
public DelegateCommand<Country> ListItemTapped => new DelegateCommand<Country>(OnTapped);
private void OnTapped(Country obj)
{
//NavigationService.NavigateAsync("MainPage");
}
public PrismTabbedPage1ViewModel()
{
ObservableCollection<City> Cities1 = new ObservableCollection<City>()
{
new City(){ CityName = "City1" },new City(){ CityName = "City2" },new City(){ CityName = "City3" },
new City(){ CityName = "City4" },new City(){ CityName = "City5" },new City(){ CityName = "City6" },
new City(){ CityName = "City7" },new City(){ CityName = "City8" },new City(){ CityName = "City9" },
};
countries = new ObservableCollection<Country>()
{
new Country(){ CountryName = "Country 1", Cities = Cities1 },
new Country(){ CountryName = "Country 2", Cities = Cities1 },
new Country(){ CountryName = "Country 3", Cities = Cities1 },
};
}
}
}
Country.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace testapp.Models
{
public class Country
{
public string CountryName { get; set; }
public ObservableCollection Cities { get; set; }
}
public class City
{
public string CityName { get; set; }
}
}
在此代码中,未触发 listview 命令行为。
我不熟悉 Prism,但由于您的 EventToCommand 在 ListView 中,您的默认绑定是 listview 传递的数据,而不是您的 ViewModel。
您可以尝试这样的操作:
<behaviors:EventToCommandBehavior EventName="ItemTapped"
Command="{Binding Source={x:Reference this},Path=BindingContext.ListItemTapped}"
EventArgsParameterPath="{Binding .}"/>
并为您的页面命名:
<TabbedPage .... x:Name="this">
因此您在列表视图中的绑定应该有效地重定向到视图模型中的命令。
希望对您有所帮助。