我刚开始新的Xamarin.Forms项目而且我不知道自己做错了什么。 在我的App.cs文件中,我尝试将我的TestPage(我已经通过Add-> New Item-> BlankPage创建的TestPage)设置为主页面但是我发现了这个错误&# 34;严重性代码描述项目文件行抑制状态 错误CS0029无法隐式转换类型' Paternity_Test.TestPage'到' Xamarin.Forms.Page'"
我的TesPage.xaml.cs
namespace Paternity_Test
{
public partial class TestPage : Xamarin.Forms.Page
{
public TestPage()
{
this.InitializeComponent();
}
}
}
和我的App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace Paternity_Test
{
public class App : Application
{
public App ()
{
MainPage = new TestPage(); //here Im geting this error
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
我做错了什么?
答案 0 :(得分:2)
<Page x:Class="Paternity_Test.TestPage" xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation"; xmlns:x="schemas.microsoft.com/winfx/2006/xaml"; xmlns:local="using:Paternity_Test" xmlns:d="schemas.microsoft.com/expression/blend/2008"; xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006"; mc:Ignorable="d">
您不能使用那些Microsoft XAML名称空间,它们需要基于Xamarin / WinFx:
仅供参考:您目前无法使用Blend
创建这些XAML文件
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TableHeaderBug.MyView">
<ContentView.Content>
</ContentView.Content>
</ContentView>
<?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="TableHeaderBug.MyPage99">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
Part 1. Getting Started with XAML
第一个XML名称空间声明意味着在没有前缀的XAML文件中定义的标记引用Xamarin.Forms中的类,例如ContentPage。第二个名称空间声明定义了x的前缀。这用于XAML本身固有的几个元素和属性,并且(理论上)XAML的所有实现都支持这些元素和属性。但是,这些元素和属性略有不同,具体取决于URI中嵌入的年份。 Xamarin.Forms支持2009 XAML规范,但不是全部。
答案 1 :(得分:0)
已弃用Device.OnPlatform,建议使用switch(Device.RuntimePlatform)
以下是使用开关的示例:
switch(Device.RuntimePlatform)
{
case Device.iOS:
Padding = new Thickness(0, 20, 0, 0);
break;
case Device.Android:
Padding = new Thickness(10, 20, 0, 0);
break;
case Device.WinPhone:
Padding = new Thickness(30, 20, 0, 0);
break;
}