试图找出如何浏览多页Android应用程序。
我的第一页的XAML如下所示:
<?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">
<Button
android:text="WELFARE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cmdWelfare"
android:onClick="cmdWelfareClicked" />
<Button
android:text="SETTINGS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cmdSettings"
android:onClick="cmdSettingsClicked" />
</LinearLayout>
活动如下:
using Android.App;
using Android.OS;
using Android.Views;
namespace Welf
{
[Activity(Label = "Welf", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
}
public void cmdWelfareClicked(View v)
{
SetContentView(Resource.Layout.p1);
}
public void cmdSettingsClicked(View v)
{
v.SetBackgroundColor(Android.Graphics.Color.Azure);
}
}
}
但是当单击按钮打开下一页时,会抛出一个未知异常。
错误文本(来自复制详细信息)为An unhandled exception occured. occurred
我试图学习如何做到这一点并且不知道可能出现什么问题,所以任何帮助都会受到赞赏。
答案 0 :(得分:1)
您需要导出该方法。试试这样:
[Export("cmdWelfareClicked")]
public void cmdWelfareClicked(View v)
{
SetContentView(Resource.Layout.p1);
}
[Export("cmdSettingsClicked")]
public void cmdSettingsClicked(View v)
{
v.SetBackgroundColor(Android.Graphics.Color.Azure);
}
但是你为什么在SetContentView(Resource.Layout.p1);
内做cmdWelfareClicked
?如果你想使用不同的布局你应该开始另一个Activity
另一种方法是从onClick
中的attribute
删除Button
xml
并使用OnCreate
中的ID
像:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
Button buttonCmdWelfare = FindViewById<Button> (Resource.Id.cmdWelfare);
buttonCmdWelfare.Click += delegate {
//Clicked
};
Button buttonCmdSettings = FindViewById<Button> (Resource.Id.cmdSettings);
buttonCmdSettings.Click += delegate {
//Clicked
};
}
不要忘记删除android:onClick="cmdSettingsClicked"
和android:onClick="cmdWelfareClicked"