Xamarin Android - Dismiss DialogFragment

时间:2017-07-21 09:19:51

标签: c# android xamarin.android android-dialogfragment dialogfragment

我已经看了几个关于如何解雇DialogFragment的不同主题,但似乎没有什么对我有用。当我从我所拥有的click事件切换到一个新的Activity时,我希望能够关闭DialogFragment。我尝试使用click事件中的this.Activity.Dismiss()之类的东西,但也尝试了我显示DialogFragment的地方:

if (_exportFragment != null)
            {
                _exportFragment.Dismiss();
            }

但这些似乎都不起作用。

修改

这是我显示DialogFragment的地方:

gridview.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args)
            {
                // DIALOG FRAGMENT
                FragmentTransaction ft = FragmentManager.BeginTransaction();
                //Remove fragment else it will crash as it is already added to backstack
                Fragment prev = FragmentManager.FindFragmentByTag("dialog");
                if (prev != null)
                {
                    ft.Remove(prev);
                }

                ft.AddToBackStack(null);

                // Create and show the dialog.
                _exportFragment = new VideoExportDialogFragment();
                _exportFragment.VideoCreationDate = VideoList[args.Position].CreationDate;
                //_exportFragment.
                _exportFragment.VideoPathFragment = VideoListPosition(args.Position);

                //_exportFragment.ThumbnailActivity = this;
                //Add fragment
                _exportFragment.Show(ft, "dialog");
                dismissLoader();
            };
        }

        private void dismissLoader()
        {
            if (_exportFragment != null)
            {
                _exportFragment.Dismiss();
            }
        }

1 个答案:

答案 0 :(得分:0)

  

当我从我所拥有的点击事件切换到新的活动时,我希望能够关闭DialogFragment。

在此点击事件中,您可以使用dialogFragment方法找到要关闭的FindFragmentByTag("dialog"),然后您可以使用_exportFragment.Dismiss()来关闭此DialogFragment。像这样的代码:

bt.Click += (sender, e) =>
{
     MyDialogFragment _exportFragment = (MyDialogFragment)FragmentManager.FindFragmentByTag("dialog");
     if (_exportFragment != null)
     {
           _exportFragment.Dismiss();
     }
     //StartActivity(you);
};