通过Xamarin中的新活动加载时布局不显示内容

x33g5p2x  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(169)

我正在用Xamarin开发一个应用程序。
我有三个活动:拨号活动联系人活动启动活动-以及两个.axml布局:主.axml和联系人.axml

SplashActivity是第一个加载的,它在打开应用程序时显示一个闪屏,当它完成时,它加载DiallerActivity,它显示我的Main.axml布局-这工作得很好。

在我的Main.axml布局中,我有一个按钮,当单击时,它会加载ContactsActivity,然后加载Contacts.axml,其中只有3个按钮和一个标签...没有一个按钮被编程来做任何事情。

问题是,当点击该按钮时,显示屏变为空白屏幕,屏幕顶部仍显示Android栏..只是不显示.axml文件中的任何内容。

我需要在运行活动时显示Contacts.axml布局。我希望我已经说清楚了。我当前的代码如下。
拨号程序活动代码

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

Button btnAcceptClick = FindViewById<Button> (Resource.Id.btnAccept);

btnAcceptClick.Click += delegate {
            StartActivity (typeof(VoWiFiApplicationFinal.ContactsActivity));
        };

联系人活动代码

public class ContactsActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // setting the contacts.axml as the view
        SetContentView (Resource.Layout.Contacts);
    }
}

有人知道为什么Contacts.axml没有显示吗?如果你需要我提供更多的信息,只要说,我会把它带过来。顺便说一句,我使用**C#**作为我的语言,所以我希望相关的帮助,如果它甚至适用于脑海中的问题。感谢阅读。
联系人.xaml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/toptest"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+/label1"
        android:text="testlabel" />
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/testagain"
    android:layout_height="wrap_content"
    android:orientation="horizontal" />
<LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/menuBar"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="fill_parent"
        android:text="ACCEPT"
        android:id="@+id/btnAccep"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnDeclin"
        android:layout_weight="1"
        android:text="DECLINE" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btntes"
        android:layout_weight="1"
        android:text="TEST" />
</LinearLayout>
</LinearLayout>
dbf7pr2w

dbf7pr2w1#

Xamarin中似乎有一个bug。当我反复重命名.axml-file并尝试values\strings.xml中的字符串时,我也得到了一个空白屏幕(除了操作栏),有时给它们的名称与.axml-file相同。虽然不确定字符串是否与此有关,但我记得在编译时有一次得到了某种错误。
总之,我的解决办法是
1.备份.axml-file的内容并完全删除该文件。
1.创建一个新的.axml-file,其名称与原始文件名不同。
1.将备份的内容粘贴到新文件中。
1.将新文件重命名回旧名称。
在那之后,我做了一个测试运行和一切工作了。

相关问题