xamarin.Forms页面未显示xaml布局

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

我有一个C#的内容页面沿着一个XAML文件。
Page1.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace CompanyName.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
        }
    }
}

Page1.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CompanyName.Pages.Page1">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand"
                   x:Name="test"
                   />
            <Button Text="Hello, World!" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是一个相当大的项目,因此为了隔离可能导致问题的任何内容,我将MainPage设置为Page1。
App.xaml.cs:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Threading.Tasks;
using System.Linq;
using CompanyName.DataBases;
using CompanyName.Pages;
using CompanyName.Models;

namespace CompanyName
{
    public partial class App : Application
    {

   

        public static String OnDatabasesLoaded { get; } = "DatabasesLoaded";

        public static Boolean IsDatabasesLoaded { get; private set; }
       
        public App()
        {
            InitializeComponent();

          // The below line is my normal entry point
         //   MainPage = new NavigationPage(new MainPage()) { BarBackgroundColor = Color.FromHex("#2196f3"),BarTextColor=Color.White};

            MainPage = new NavigationPage(new Page1()); // I added this to see if Page1 would even work
        }

        

        protected async override void OnStart()
        {

            // Create the tabels

            /*
            ColorDatabase colorDatabase = await ColorDatabase.Instance();
            QuiltDatabase quiltDatabase = await QuiltDatabase.Instance();
            TextDatabase textDatabase = await TextDatabase.Instance();
            */

           

        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

   
}

我的问题是我的xaml文件没有被呈现。在物理设备和模拟器上,都出现了一个空白屏幕(除了顶部的导航栏)。xaml和后面的代码都被设置为嵌入式资源。我使用的是Visual Studio 2022 with Xamarin.Forms 5.0.0.2478。我做了一个新项目,我能够创建一个新页面,布局显示得很好。
我还没能看到这个错误是否发生在ios上。我所有的测试都是在Android上进行的。
谢谢

pcww981p

pcww981p1#

  • “xaml和后台代码都设置为嵌入资源。"*

为什么?代码和xaml不是资源类型。VS必须“编译”这些文件。
让VS将它们正确地插入到您的项目中:

  • 将这些文件复制到项目之外的某个安全位置。
  • 从项目中删除它们。
  • 右键单击项目/添加新项/内容页,名称为“Page 1”。
  • 然后在文本编辑器中打开您的原件,这样您就可以复制文本,将它们粘贴到VS创建的文件中。

相关问题