我怎样才能制作 >在带有 Xamarin.Forms 的单元格中? [英] How can I make a > in a cell with Xamarin.Forms?
问题描述
我有一个应用程序,我可以在其中更改卡片的顺序和显示方式.对于任何拥有 iOS 的人,我需要与设置">联系人">排序顺序"页面的工作方式非常相似的内容.
I have an application where I can change the order and the way cards appear. For anyone who has iOS I need something very similar to the way the Settings > Contacts > Sort Order page works.
这显示了两行.一个带有 First, Last,另一个带有 Last, First.当用户单击一行时,它就像一个单选按钮,并且在行的末尾会出现一个勾号.
This shows two rows. One with First, Last and the other with Last, First. When a user clicks on a row it acts like a radio button and a tick mark appears at the end of the row.
我想尝试实现这个功能,但我不知道从哪里开始.我应该使用 ViewCell 还是 TextCell 来执行此操作,以及如何有人对如何实现此有任何想法
I would like to try and implement this functionality but I am not sure where to start. Should I do this with a ViewCell or a TextCell and how does anyone have any ideas as to how it is implemented this
.
推荐答案
EDIT 1:简化了 iOS 渲染器中的属性更改逻辑;现在没有要清理的引用或处理程序.
EDIT 1: Simplified property changed logic in iOS renderer; now there are no references or handlers to cleanup.
对@hankide 的回答的扩展:
In extension to @hankide's answer:
您可以在扩展 TextCell
或 ViewCell
的同时创建可绑定属性 IsChecked
并将您的 VM 状态绑定到它.
You can create a bindable property IsChecked
while extending a TextCell
or ViewCell
and bind your VM state to it.
public class MyTextCell : TextCell
{
public static readonly BindableProperty IsCheckedProperty =
BindableProperty.Create(
"IsChecked", typeof(bool), typeof(MyTextCell),
defaultValue: false);
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
}
下一步是创建侦听此属性并在 iOS 级别显示复选标记的渲染器.
Next step would be to create renderer that listens to this property and shows a check-mark at iOS level.
[assembly: ExportRenderer(typeof(MyTextCell), typeof(SampleApp.iOS.MyTextCellRenderer))]
namespace SampleApp.iOS
{
public class MyTextCellRenderer : TextCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var nativeCell = base.GetCell(item, reusableCell, tv);
var formsCell = item as MyTextCell;
SetCheckmark(nativeCell, formsCell);
return nativeCell;
}
protected override void HandlePropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.HandlePropertyChanged(sender, args);
System.Diagnostics.Debug.WriteLine($"HandlePropertyChanged {args.PropertyName}");
if (args.PropertyName == MyTextCell.IsCheckedProperty.PropertyName)
{
var nativeCell = sender as CellTableViewCell;
if (nativeCell?.Element is MyTextCell formsCell)
SetCheckmark(nativeCell, formsCell);
}
}
void SetCheckmark(UITableViewCell nativeCell, MyTextCell formsCell)
{
if (formsCell.IsChecked)
nativeCell.Accessory = UITableViewCellAccessory.Checkmark;
else
nativeCell.Accessory = UITableViewCellAccessory.None;
}
}
}
示例用法 1
而且,示例用法如下:
<TableView Intent="Settings">
<TableSection Title="Sort Order">
<local:MyTextCell Text="First Last" IsChecked="false" />
<local:MyTextCell Text="Last, First" IsChecked="true" />
</TableSection>
</TableView>
示例用法 2
您还可以监听 Tapped
事件以确保 IsChecked
属性按预期工作.
You can also listen to Tapped
event to ensure IsChecked
property works as expected.
例如,您将此属性绑定到 ViewModel:
For example, you bind this property to ViewModel:
<TableView Intent="Settings">
<TableSection Title="Sort Order">
<local:MyTextCell Tapped="Handle_Tapped" Text="{Binding [0].Name}" IsChecked="{Binding [0].IsSelected}" />
<local:MyTextCell Tapped="Handle_Tapped" Text="{Binding [1].Name}" IsChecked="{Binding [1].IsSelected}" />
</TableSection>
</TableView>
并处理点击事件:
public SettingViewModel[] Settings = new []{
new SettingViewModel { Name = "First Last", IsSelected = false },
new SettingViewModel { Name = "Last First", IsSelected = true },
};
void Handle_Tapped(object sender, System.EventArgs e)
{
var cell = sender as TextCell;
if (cell == null)
return;
var selected = cell.Text;
foreach(var setting in Settings)
{
if (setting.Name == selected)
setting.IsSelected = true;
else
setting.IsSelected = false;
}
}
这篇关于我怎样才能制作 >在带有 Xamarin.Forms 的单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!