我在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与Xamarin一起使用。表格5.0.0.2478。我做了一个新项目,我能够创建一个新页面,布局显示得很好。
我还没能看到这个错误是否发生在ios上。我所有的测试都是在安卓系统上进行的。
感谢
"xaml和代码隐藏都设置为嵌入式资源">
为什么?code和xaml不是资源类型。VS必须";编译";那些文件。
让VS正确地将它们插入到您的项目中:
- 将这些文件复制到项目之外的安全位置
- 从项目中删除它们
- Rt点击项目/添加新项目/内容页面,名称";Page1">
- 然后在文本编辑器中打开原件,这样您就可以复制文本,将它们粘贴到VS创建的文件中