【问题标题】:Return data from DialogFragment to calling activity将数据从 DialogFragment 返回到调用活动
【发布时间】:2019-05-19 18:54:12
【问题描述】:

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

<?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 和await 成为对话框,但这只会让事情变得更糟。我错过了什么?

【问题讨论】:

    标签: c# xamarin visual-studio-2017


    【解决方案1】:

    使用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 */ };
    

    【讨论】:

    • 只需要对 DialogEventArgs 类进行轻微修正,public delegate void DialogEventHandler(object sender, DialogEventArgs args); 超出了类定义。否则它按预期工作。在您的答案中进行更改,我会将其标记为答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多