Xamarin.Android中的DialogFragments将不会显示(甚至不会调用生命周期方法)

时间:2018-08-22 15:16:03

标签: android android-fragments xamarin alertdialog dialogfragment

我一直在网上搜索该问题的答案,但到目前为止还没有发现任何可行的方法。

我正在尝试使用DialogFragment打开一个快速弹出窗口,其中包含一些有关我的应用程序正在执行的工作类型的详细信息。我有一个显示订单号的TextView,如果单击该TextView,则会打开一个Dialog窗口。问题是,每次我单击TextView时,屏幕都会变暗,就像显示一个对话框一样,但是什么也没有发生。我可以再次单击以“删除”对话框,但是它从不显示窗口。

我尝试过扩大OnCreateView()中的布局,我尝试过使用OnCreateDialog()中的AlertDialog Builder构建它,但没有一个起作用。事实证明,打开对话框时,这些生命周期方法中的 none 都不会被调用(我尝试覆盖的其他任何方法也都无法进行测试)。这是我到目前为止的代码:

打开对话框的我的事件处理程序:

private void TicketNumber_Click(object sender, EventArgs e)
    {
        // Instantiate the fragment manager and begin a new transaction, and retrieve any previous instances of this fragment if it exists.
        var transaction = FragmentManager.BeginTransaction();
        var previous = FragmentManager.FindFragmentByTag("Ticket Details");

        // Remove the previous dialog if it exists.
        if (previous != null)
        {
            transaction.Remove(previous);
        }

        transaction.AddToBackStack(null);

        var details = new TicketDetailDialogFragment();
        details.Show(transaction, "Ticket Details");
    }

还有DialogFragment内部的代码:

[Register("android/app/DialogFragment", DoNotGenerateAcw =true)]
public class TicketDetailDialogFragment : DialogFragment
{
    private TextView ticketNumber;
    private TextView customer;
    private TextView contact;
    private TextView leaseName;
    private TextView wellNumber;

    private MainApplication _global;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        _global = (MainApplication)Application.Context;
    }

    private void FindViews()
    {
        ticketNumber =  View.FindViewById<TextView>(Resource.Id.ticketNumberTextView);
        customer     =  View.FindViewById<TextView>(Resource.Id.customerTextView);
        contact      =  View.FindViewById<TextView>(Resource.Id.contactTextView);
        leaseName    =  View.FindViewById<TextView>(Resource.Id.leaseNameTextView);
        wellNumber   =  View.FindViewById<TextView>(Resource.Id.wellNumberTextView);
    }

    private void BindData()
    {
        // Filter for empty or default values and display appropriately.
        ticketNumber.Text  = _global.ticketNumber == String.Empty ? "Not Available" : _global.ticketNumber;
        customer.Text      = _global.customer     == String.Empty ? "Not Available" : _global.customer;
        contact.Text       = _global.contact      == String.Empty ? "Not Available" : _global.contact;
        leaseName.Text     = _global.leaseName    == String.Empty ? "Not Available" : _global.leaseName;
        wellNumber.Text    = _global.wellNumber   == 0            ? "Not Available" : _global.wellNumber.ToString();
    }

    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        var builder = new AlertDialog.Builder(this.Activity);
        var inflater = (LayoutInflater)this.Activity.GetSystemService(Context.LayoutInflaterService);

        builder.SetView(inflater.Inflate(Resource.Layout.TicketDetailView, null));

        FindViews();
        BindData();

        return builder.Create();
    }
}

同样,我在OnCreate()和OnCreateDialog()中放置了断点,但都没有碰到,所以这里肯定有一些我想念的东西。我是Xamarin开发的新手,所以如果我提出的问题之前已经回答过,或者缺少明显的内容,我深表歉意。

后续问题-我是否在正确的位置绑定到布局中的视图?还是应该在通话活动中这样做?

0 个答案:

没有答案