将数据从DialogFragment返回到调用活动

时间:2019-05-19 18:54:13

标签: c# xamarin visual-studio-2017

我有一个DialogFragment正在工作,需要从微调器中返回所选的物品。我已经尝试了许多在Stack Overflow和其他地方找到的方法,但是它们都使用了Java(在Visual Studio 2017的Xamarin中不能(显然)很好地转换为c#)。到目前为止,没有任何工作有效。My {{ 1}}的布局是:

DialogFragment

课程代码是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  
    android:minWidth="300dp" 
    android:minHeight="75dp">
    <TextView
        android:text="Select the department you are registering for."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center" 
        android:textStyle="bold"
        android:id="@+id/textView2" />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:id="@+id/department_spinner" />
    <Button
        android:text="Ok"
        android:layout_width="200px"
        android:layout_gravity="center"
        android:layout_height="34.5dp"
        android:id="@+id/button_ok" />
</LinearLayout>

这是显示对话框的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace MyProject
{
    class selectDepartment : DialogFragment
    {
        static Spinner department;
        public string selection = "";

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            View view = inflater.Inflate(Resource.Layout.selectDepartment, container, false);

            Button ok = view.FindViewById<Button>(Resource.Id.button_ok);
            department = view.FindViewById<Spinner>(Resource.Id.department_spinner);

            List<string> list = new List<string>();
            list.Add("Select Department");
            list.Add("Dept. A");
            list.Add("Dept. B");

            var adapter = new ArrayAdapter<string>(this.Activity, Android.Resource.Layout.SimpleSpinnerItem, list.ToArray());
            department.Adapter = adapter;

            ok.Click += (sender, args) =>
            {
                selection = string.Format("{0}", department.GetItemAtPosition(department.SelectedItemPosition));
            };
            return view;
        }

    }
}

在上一次尝试中,我将微调器选择分配给一个属性,然后尝试读取该属性以获取选定的值,但是对话框(显然)显示在不同的线程上,并且当该选择时未选择该选择代码行被执行。我尝试使方法 FragmentTransaction getdepartment = FragmentManager.BeginTransaction(); selectDepartment getDept = new selectDepartment(); getDept.Show(getdepartment , "Select Department"); // Here I attempt to read a property which contains the selection string selection = getDept.selection; async成为对话框,但这只会使情况变得更糟。我想念什么?

1 个答案:

答案 0 :(得分:0)

使用custom event

public class DialogEventArgs : EventArgs
{
    public string Selection { get; set; }
}

,然后在selectDepartment中添加:

public delegate void DialogEventHandler(object sender, DialogEventArgs args);

public event DialogEventHandler Dismissed;

最后在按钮的单击处理程序中添加:

if (null != Dismissed)
    Dismissed(this, new DialogEventArgs { Selection = selection });

创建对话框时,请附加事件处理程序

selectDepartment getDept = new selectDepartment();
getDept.Dismissed += (s, e) => { /* do something with e.Selection here */ };