【问题标题】:Unable to set "Main view" as ContentView in Android project with VS 2015无法在 VS 2015 的 Android 项目中将“主视图”设置为 ContentView
【发布时间】:2018-03-15 09:53:35
【问题描述】:

我在开发 Android 应用程序时遇到了 Visual Studio 2015 的问题。

由于某种原因,我无法在 MyActivity 中设置主视图

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Id.Main);
    }

构建时返回错误“'Resource.Id'不包含'Main'的定义”,尽管该视图称为“Main.axml” 我以前确实遇到过这个问题,但可以通过保存视图来解决这个问题,这总是有帮助的。

我不确定,但它似乎与最近的 Xamarin 更新有关。

我尝试了几件事:清理解决方案,重建(以错误结束),关闭 VS 并重新打开,重新启动计算机。 我还创建了一个新项目,但这也无济于事。

下面是我的(简单)Main.axml 文件

<?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:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="read" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

【问题讨论】:

  • 尝试删除bin和obj,然后重启你的VS。如果还是不行,请使用VS 2017。

标签: c# android visual-studio android-layout xamarin


【解决方案1】:

应该是:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
}

因为您指的是您希望该活动看起来的布局。不是布局中的特定视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 2020-10-11
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多