![](/img/trans.png)
[英]Xamarin Forms - How to create custom render to give TableSection on Android a footer?
[英]How can I get a custom TableSection footer to respond to dynamic changes?
我正在使用此代碼:
public class ExtFooterTableViewRenderer : TableViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
base.OnElementChanged(e);
if (Control == null)
return;
var tableView = Control as UITableView;
var formsTableView = Element as TableView;
tableView.WeakDelegate = new CustomFooterTableViewModelRenderer(formsTableView);
}
private class CustomFooterTableViewModelRenderer : TableViewModelRenderer
{
public CustomFooterTableViewModelRenderer(TableView model) : base(model)
{
}
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
var txtView = new UITextView
{
//Text = "Select or deselect cards from the list above and they will added or removed from the card deck,",
Text = TitleForFooter(tableView, section),
TextColor = UIColor.Gray,
TextAlignment = UITextAlignment.Justified,
TextContainerInset = new UIEdgeInsets(top: 10, left: 15, bottom: 5, right: 15),
BackgroundColor = Color.Transparent.ToUIColor()
};
txtView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
return txtView;
}
//Retrieves the footer text for corresponding section through the attached property
public override string TitleForFooter(UITableView tableView, nint section)
{
var tblSection = View.Root[(int)section];
return ExtFooterTableView.GetFooterText(tblSection);
}
}
}
像這樣在XAML中更改頁腳時,效果很好:
<TableSection Title="Cards" local:ExtFooterTableView.FooterText="ABC">
但是當我嘗試通過如下方式進行綁定時:
<TableSection Title="Cards" local:ExtFooterTableView.FooterText="{Binding CardsFooter}">
然后,它似乎不響應在我的代碼后端C#中進行的CardsFooter值的更改。
有沒有一種方法可以使我對綁定值的動態更改做出響應,從而使更改顯示出來?
即使在附加屬性的情況下,也會在相應的可綁定對象中調用PropertyChanged
事件。
為了收聽這些更改-您可以訂閱它們,並相應地更新/重新加載它的本機副本(部分):
private class CustomFooterTableViewModelRenderer : TableViewModelRenderer
{
public CustomFooterTableViewModelRenderer(TableView model) : base(model)
{
}
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
....
}
//Retrieves the footer text for corresponding section through the attached property
public override string TitleForFooter(UITableView tableView, nint section)
{
var tblSection = View.Root[(int)section];
Table = tableView;
tblSection.PropertyChanged -= OnSectionPropertyChanged;
tblSection.PropertyChanged += OnSectionPropertyChanged;
return ExtFooterTableView.GetFooterText(tblSection);
}
void OnSectionPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (ExtFooterTableView.FooterTextProperty.PropertyName.Equals(e.PropertyName))
{
if (sender is TableSection section)
{
var index = View.Root.IndexOf(section);
var indexSet = Foundation.NSIndexSet.FromIndex(index);
Table.ReloadSections(indexSet, UITableViewRowAnimation.None);
}
}
}
//Also ensure unsubscribe during dispose, or unload
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
foreach(var section in View?.Root)
section.PropertyChanged -= OnSectionPropertyChanged;
}
}
另外,建議您在表格視圖渲染器的OnElementChanged
中為Element
添加一個空檢查:
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
base.OnElementChanged(e);
if (Control == null || Element == null)
return;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.