Return 从 DialogFragment 到调用的数据 activity
Return data from DialogFragment to calling activity
我有一个正在工作的 DialogFragment
,需要 return 从微调器中选择的项目。我已经尝试了很多在 Stack Overflow 和其他地方找到的方法,但它们都使用 java,这(显然)不能很好地转换为 Xamarin 中的 c# for Visual Studio 2017。迄今为止,没有任何效果我的 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>
class代码是:
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
成为对话框,但这只会让事情变得更糟。我错过了什么?
使用 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 */ };
我有一个正在工作的 DialogFragment
,需要 return 从微调器中选择的项目。我已经尝试了很多在 Stack Overflow 和其他地方找到的方法,但它们都使用 java,这(显然)不能很好地转换为 Xamarin 中的 c# for Visual Studio 2017。迄今为止,没有任何效果我的 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>
class代码是:
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
成为对话框,但这只会让事情变得更糟。我错过了什么?
使用 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 */ };