Xamarin Android 将 MainActivity 传递给 AlertDialog
Xamarin Android passing MainActivity to AlertDialog
我正在尝试通过将其代码分离到另一个 class 中并根据代码中的要求调用 class 来实现 AlertDialog。
这是我的 AlertDialog
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace NAndroid
{
public class AlertViewController: Activity
{
public AlertViewController ()
{
}
public void ShowAlertBox (string titleTxt, string messageTxt)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle(titleTxt);
builder.SetMessage(messageTxt);
builder.SetCancelable(false);
builder.SetPositiveButton("OK", delegate { Finish(); });
RunOnUiThread (() => {
builder.Show();
} );
}
}
}
从其他人那里调用它class
AlertViewController alertView = new AlertViewController ();
alertView.ShowAlertBox ("Test", "Test");
在线崩溃了。它抛出空指针异常
AlertDialog.Builder builder = new AlertDialog.Builder(this);
public class AlertViewController: Activity
AlertViewController extends Activity...因此您的 AlertViewController 充当 activity 因此它应该在清单中注册并且还应该使用 intent 开始...稍后使用主题进行转换它进入对话框...或
一个解决方案只需创建一个简单的 class 作为
public class AlertViewController
并在构造函数中传递 activity 上下文
AlertViewController alertView = new AlertViewController (YourActivity.this);
alertView.ShowAlertBox ("Test", "Test");
另一个解决方案扩展对话框 class 并创建自定义对话框
public class AlertViewController : Dialog
否则你也可以去寻找对话片段...更多信息请查看http://javatechig.com/xamarin/alertdialog-and-dialogfragment-example-in-xamarin-android
我正在尝试通过将其代码分离到另一个 class 中并根据代码中的要求调用 class 来实现 AlertDialog。
这是我的 AlertDialog
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace NAndroid
{
public class AlertViewController: Activity
{
public AlertViewController ()
{
}
public void ShowAlertBox (string titleTxt, string messageTxt)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle(titleTxt);
builder.SetMessage(messageTxt);
builder.SetCancelable(false);
builder.SetPositiveButton("OK", delegate { Finish(); });
RunOnUiThread (() => {
builder.Show();
} );
}
}
}
从其他人那里调用它class
AlertViewController alertView = new AlertViewController ();
alertView.ShowAlertBox ("Test", "Test");
在线崩溃了。它抛出空指针异常
AlertDialog.Builder builder = new AlertDialog.Builder(this);
public class AlertViewController: Activity
AlertViewController extends Activity...因此您的 AlertViewController 充当 activity 因此它应该在清单中注册并且还应该使用 intent 开始...稍后使用主题进行转换它进入对话框...或
一个解决方案只需创建一个简单的 class 作为
public class AlertViewController
并在构造函数中传递 activity 上下文
AlertViewController alertView = new AlertViewController (YourActivity.this);
alertView.ShowAlertBox ("Test", "Test");
另一个解决方案扩展对话框 class 并创建自定义对话框
public class AlertViewController : Dialog
否则你也可以去寻找对话片段...更多信息请查看http://javatechig.com/xamarin/alertdialog-and-dialogfragment-example-in-xamarin-android