Xamarin.ios “单触式”对话框在单个视图上显示多个放射组(单个根元素)
我想在一个带有monotouch对话框的RootElement中使用多个无线电组。每个放射组都有自己的部分。我找不到一种方法来实现这一点,因为单个放射组只能分配给根元素svn 这是我的解决办法Xamarin.ios “单触式”对话框在单个视图上显示多个放射组(单个根元素),xamarin.ios,monotouch.dialog,Xamarin.ios,Monotouch.dialog,我想在一个带有monotouch对话框的RootElement中使用多个无线电组。每个放射组都有自己的部分。我找不到一种方法来实现这一点,因为单个放射组只能分配给根元素svn 这是我的解决办法 public class CustomRootElement : RootElement { private RadioGroup _defaultGroup = new RadioGroup(0); private Dictionary<string, RadioGroup>
public class CustomRootElement : RootElement
{
private RadioGroup _defaultGroup = new RadioGroup(0);
private Dictionary<string, RadioGroup> _groups = new Dictionary<string, RadioGroup>();
public CustomRootElement(string caption = "") : base(caption , new RadioGroup("default",0))
{
}
public CustomRootElement(string caption, Group group, Func<RootElement, UIViewController> createOnSelected) : base(caption, group)
{
var radioGroup = group as RadioGroup;
if(radioGroup != null)
{
_groups.Add(radioGroup.Key.ToLower(), radioGroup);
}
this.createOnSelected = createOnSelected;
}
public override UITableViewCell GetCell(UITableView tv)
{
var cell = base.GetCell(tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
public int Selected(string group)
{
if (string.IsNullOrEmpty(group))
{
throw new ArgumentNullException("group");
}
group = group.ToLower();
if (_groups.ContainsKey(group))
{
return _groups[group].Selected;
}
return 0;
}
public void Select(string group, int selected)
{
if (string.IsNullOrEmpty(group))
{
throw new ArgumentNullException("group");
}
var radioGroup = GetGroup(group);
radioGroup.Selected = selected;
}
internal RadioGroup GetGroup(string group)
{
if (string.IsNullOrEmpty(group))
{
throw new ArgumentNullException("group");
}
group = group.ToLower();
if (!_groups.ContainsKey(group))
{
_groups[group] = new RadioGroup(group , 0);
}
return _groups[group];
}
internal NSIndexPath PathForRadioElement(string group, int index)
{
foreach (var section in this)
{
foreach (var e in section.Elements)
{
var re = e as SlRadioElement;
if (re != null
&& string.Equals(re.Group, group,StringComparison.InvariantCultureIgnoreCase)
&& re.Index == index)
{
return e.IndexPath;
}
}
}
return null;
}
}
public class CustomRadioElement : RadioElement
{
public event Action<CustomRadioElement> ElementSelected;
private readonly static NSString ReuseId = new NSString("CustomRadioElement");
private string _subtitle;
public int? Index { get; protected set; }
public CustomRadioElement(string caption, string group = null, string subtitle = null) :base(caption, group)
{
_subtitle = subtitle;
}
protected override NSString CellKey
{
get
{
return ReuseId;
}
}
public override UITableViewCell GetCell(UITableView tv)
{
EnsureIndex();
var cell = tv.DequeueReusableCell(CellKey);
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Subtitle , CellKey);
}
cell.ApplyStyle(this);
cell.TextLabel.Text = Caption;
if (!string.IsNullOrEmpty(_subtitle))
{
cell.DetailTextLabel.Text = _subtitle;
}
var selected = false;
var slRoot = Parent.Parent as CustomRootElement;
if (slRoot != null)
{
selected = Index == slRoot.Selected(Group);
}
else
{
var root = (RootElement)Parent.Parent;
selected = Index == root.RadioSelected;
}
cell.Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None;
return cell;
}
public override void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
{
var slRoot = Parent.Parent as CustomRootElement;
if (slRoot != null)
{
var radioGroup = slRoot.GetGroup(Group);
if (radioGroup.Selected == Index)
{
return;
}
UITableViewCell cell;
var selectedIndex = slRoot.PathForRadioElement(Group, radioGroup.Selected);
if (selectedIndex != null)
{
cell = tableView.CellAt(selectedIndex);
if (cell != null)
{
cell.Accessory = UITableViewCellAccessory.None;
}
}
cell = tableView.CellAt(indexPath);
if (cell != null)
{
cell.Accessory = UITableViewCellAccessory.Checkmark;
}
radioGroup.Selected = Index.Value;
var handler = ElementSelected;
if (handler != null)
{
handler(this);
}
}
else
{
base.Selected(dvc, tableView, indexPath);
}
}
private void EnsureIndex()
{
if (!Index.HasValue)
{
var parent = Parent as Section;
Index = parent.Elements.IndexOf(this);
}
}
}
公共类CustomRootElement:RootElement
{
私有放射组_defaultGroup=新放射组(0);
专用词典_groups=新词典();
public CustomRootElement(string caption=”“):基本(标题,新的放射组(“默认”,0))
{
}
公共CustomRootElement(字符串标题、组组、Func createOnSelected):基本(标题、组)
{
var radioGroup=组作为放射组;
如果(放射组!=null)
{
_添加(radioGroup.Key.ToLower(),radioGroup);
}
this.createOnSelected=createOnSelected;
}
公用覆盖UITableViewCell GetCell(UITableView电视)
{
var cell=base.GetCell(tv);
cell.SelectionStyle=UITableViewCellSelectionStyle.None;
返回单元;
}
已选择公共整数(字符串组)
{
if(string.IsNullOrEmpty(组))
{
抛出新的异常(“组”);
}
group=group.ToLower();
如果(_groups.ContainsKey(组))
{
返回_组[组]。选中;
}
返回0;
}
公共无效选择(字符串组,整数选择)
{
if(string.IsNullOrEmpty(组))
{
抛出新的异常(“组”);
}
变量组=GetGroup(组);
radioGroup.Selected=已选择;
}
内部放射组GetGroup(字符串组)
{
if(string.IsNullOrEmpty(组))
{
抛出新的异常(“组”);
}
group=group.ToLower();
如果(!_groups.ContainsKey(组))
{
_组[组]=新的放射组(组,0);
}
返回组[组];
}
内部NSIndexPath PathForRadioElement(字符串组,int索引)
{
foreach(本节中的var部分)
{
foreach(第节元素中的变量e)
{
var re=作为放射性元素的e;
如果(re!=null
&&string.Equals(re.Group、Group、StringComparison.invariantCultureInogoreCase)
&&re.Index==索引)
{
返回e.indexath;
}
}
}
返回null;
}
}
公共类CustomRadioElement:RadioElement
{
选择公共事件行动要素;
私有只读静态NSString ReuseId=新NSString(“CustomRadioElement”);
私人字符串(字幕),;
公共int?索引{get;protected set;}
public CustomRadioElement(字符串标题,字符串组=null,字符串标题=null):基本(标题,组)
{
_副标题=副标题;
}
受保护的覆盖NSString单元密钥
{
得到
{
返回ReuseId;
}
}
公用覆盖UITableViewCell GetCell(UITableView电视)
{
EnsureIndex();
var cell=tv.DequeueReusableCell(CellKey);
if(单元格==null)
{
单元格=新的UITableViewCell(UITableViewCellStyle.Subtitle,CellKey);
}
cell.ApplyStyle(this);
cell.textlab.Text=标题;
如果(!string.IsNullOrEmpty(_subtitle))
{
cell.DetailTextLabel.Text=\u副标题;
}
选择的var=false;
var slRoot=Parent.Parent作为CustomRootElement;
if(slRoot!=null)
{
选定=索引==slRoot.选定(组);
}
其他的
{
var root=(RootElement)Parent.Parent;
已选择=索引==根。已选择;
}
cell.Accessory=选中?UITableViewCellAccessory。选中标记:UITableViewCellAccessory.None;
返回单元;
}
已选择公共覆盖无效(DialogViewController dvc、UITableView tableView、NSIndexPath indexPath)
{
var slRoot=Parent.Parent作为CustomRootElement;
if(slRoot!=null)
{
var radioGroup=slRoot.GetGroup(组);
if(radioGroup.Selected==索引)
{
返回;
}
UITableViewCell;
var selectedIndex=slRoot.PathForRadioElement(组,放射组.Selected);
if(selectedIndex!=null)
{
cell=tableView.CellAt(selectedIndex);
如果(单元格!=null)
{
cell.accessority=UITableViewCellAccessority.None;
}
}
cell=tableView.CellAt(indexPath);
如果(单元格!=null)
{
cell.accessority=UITableViewCellAccessority.Checkmark;
}
选定的放射组=索引值;
var handler=ElementSelected;
if(处理程序!=null)
{
处理者(本);
}
}
其他的
{
base.Selected(dvc、tableView、indexPath);
}
}
私有索引()
{
如果(!Index.HasValue)
{
var parent=作为节的父级;
Index=parent.Elements.IndexOf(this);
}
}
}
希望这有帮助 获取编译错误:错误CS1061:Type
MonoTouch.UIKit.UITableViewCell'不包含
ApplyStyle'的定义,并且找不到类型为MonoTouch.UIKit.UITableViewCell'的扩展方法ApplyStyle'。CustomRadioElement.cs(61,61):错误CS0103:当前上下文中不存在名称“Fonts”(CS0103)。当我删除编译错误行(ApplyStyle和Font)时,这种方法会起作用。但是如何在每个组上预设所选的值呢?。也可在选择