【发布时间】:2017-08-24 11:28:37
【问题描述】:
我们的应用程序显示类别组列表:
List<CategoryGroupWordCountVM> categoryGroups;
在屏幕上看起来像这样:
体育 23 [开启]
爱好 5 [开启]
国家 55 [开启]
职业 0 [关闭]
这样的行对象:
public class CategoryGroupWordCountVM : ObservableProperty {
public int Id { get; set; }
public string Name { get; set; }
public bool IsToggled { get; set; }
}
页面 XAML:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Japanese;assembly=Japanese" x:Class="Japanese.CategoryGroupPage" x:Name="CategoryGroupPage">
<ContentPage.Content>
<TableView x:Name="tableView" Intent="Settings">
</TableView>
</ContentPage.Content>
</ContentPage>
行 XAML:
<ViewCell
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Japanese.MyViewCell">
<Grid>
<Label Text = "{Binding Name}" />
<Label Text = "{Binding TotalWordCount}" />
<Switch IsToggled = "{Binding IsToggled}" />
</Grid>
</ViewCell>
支持 C#
protected void RefreshPage() {
categoryGroups = App.DB.GetCategoryGroupWithWordCount();
var section = new TableSection("Available Categories");
foreach (var category in categoryGroups) {
var cell = new MyViewCell { BindingContext = category };
section.Add(cell);
}
tableView.Root.Add(section);
}
protected override void OnAppearing() {
base.OnAppearing();
RefreshPage();
}
我们想要做的是,当用户切换切换时,会调用如下所示的方法,并更新切换更改所在行的 TotalWordCount 值。
void selectCategoryGroup(object sender, SelectedItemChangedEventArgs e) {
if (e.SelectedItem == null) {
return;
}
var categoryGroup = (CategoryGroupWordCountVM)e.SelectedItem;
App.DB.UpdateCategoryGroupIsToggled(categoryGroup.IsToggled, categoryGroup.Id);
// The row that follows needs to update the value so that
// the screen shows a new value for on the left side of
// the switch. When the switch is off this will always be
// 0. When on it will be the list of words in the category
TotalWordCount = GetWordCountForCategory(categoryGroup.Id);
}
这是以前通过添加此代码实现的 `
<ListView ItemSelected="selectCategoryGroup" >
但是现在我们使用的是 TableView,我们没有这个。
因此,如果有人能就如何在开关更改时触发 selectCategoryGroup 之类的方法提供一些建议,我们将不胜感激。
请注意,categoryGroups 列表中通常会有大约 20-30 个不同的条目。另外,如果 MyViewCell 是用 C# 编码的,我会很高兴。
【问题讨论】:
-
为什么更改开关后又要创建TableView??你想根据拨动开关来操作db,没问题,但是不需要调用RefreshPage()方法,因为视图已经改变了。
标签: xamarin xamarin.forms