【发布时间】:2014-10-02 08:21:51
【问题描述】:
我有两个关于 Xamarin 的问题:
1) 假设我们已经安装了 Xamarin 并创建了一个包含 3 个项目的示例解决方案:一个跨平台通用项目,例如 Xamarin.Forms 引用(此项目将被另外两个引用);一个参考 Xamarin.Android 的 Android 平台特定项目;以及另外一个针对 iOS 的平台特定项目。
那么,问题来了:这些特定于平台的项目是“原生开发”项目的全功能副本吗?换句话说 - 我是否能够在 Xamarin.Android 项目中做同样的事情,例如在 adt+java 中?
这是否意味着,如果我在我的解决方案中不使用任何跨平台库,那么我最终会使用相同的原生开发工具,就像我在 Android 上使用 Adt+java 一样?
2)第二个问题与Android Xamarin开发有关。我的主要活动类是从Activity 类派生而来的
[Activity(Label = "HelloWorld", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.myLay);
}
}
在方法OnCreate 中,我正在设置我创建的主视图。一切正常,我的布局显示在模拟器上。这是设置布局的正确方法吗?
如果我将尝试访问属于我的布局一部分的某些元素,如下所示:
var textView = FindViewById(Resource.Id.textView1);
然后我得到空引用,就像没有这样的元素但它存在。
这是我的布局`myLay.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<EditText
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="285.1dp"
android:id="@+id/editText1"
android:layout_marginBottom="306.8dp"
android:text="some text" />
</LinearLayout>
那么如何在代码中访问视图呢?
【问题讨论】:
-
您的代码看起来正确。您在哪里调用 FindViewById?你能展示更多你的代码以及你得到异常的地方吗?
标签: c# android xamarin xamarin.ios xamarin.android