使用Prism Xamarin表单创建动态TabbedPage
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Prism Xamarin表单创建动态TabbedPage相关的知识,希望对你有一定的参考价值。
我正在使用Prism和item source,在每个标签页中有一个Listview,我无法利用“ItemTapped”事件作为事件来命令行为在轻触listview项时触发viewmodel中的命令,但是没有触发器在调试模式下,当我点击列表视图项目时,请帮助我理解为什么会发生这种情况,并且有任何替代方式我可以使用命令。当我检查时,事件在PrismTabbedPage1.xaml.cs中被触发但在PrismTabbedPage1ViewModel.cs中没有被触发
棱镜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">
因此,listview中的绑定应该有效地重定向到viewmodel中的命令。
我希望这有帮助。
以上是关于使用Prism Xamarin表单创建动态TabbedPage的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Xamarin 表单中的视图模型之间最好地传递信息 - Prism [关闭]