MvvmCross自定义绑定显示对话框

时间:2015-05-15 08:01:49

标签: android xamarin mvvmcross

目标是显示对话框,供用户选择EditText上的日期。

我正在努力实现将在点击时显示对话框的绑定。代码如下:

public class EditDateBinding : BindingWrapper<EditText, DateTime>
{
    public EditDateBinding(EditText androidControl) : base(androidControl)
    {
    }

    public override void SubscribeToEvents()
    {
        Target.Click += InputClick;
    }

    private void InputClick(object sender, EventArgs args)
    {
        DateTime parsedDate = DateTime.Now;
        DateTime.TryParse(Target.Text, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDate);

        var dialog = new DatePickerDialogFragment(Target.Context, parsedDate, OnDateSet);

        dialog.Show(
                    // Can't get fragment manager here
            , "date");
    }

    private void OnDateSet(object sender, DatePickerDialog.DateSetEventArgs e)
    {
        SetValueToView(Target, e.Date);
    }

    protected override void Dispose(bool isDisposing)
    {
        base.Dispose(isDisposing);
        if (isDisposing)
        {
            if (Target != null)
            {
                Target.Click -= InputClick;
            }
        }
    }

    protected override void SetValueToView(EditText androidControl, DateTime value)
    {
        androidControl.Text = value.ToShortDateString();
    }
}

但是我无法找到一种获取FragmentManager实例的方法来调用实例化对话框的Show方法。这可以以任何方式实施吗?

1 个答案:

答案 0 :(得分:0)

找到了实现它的方法:

 var act = (Activity) Target.Context;
 dialog.Show(act.FragmentManager, "date");