【发布时间】:2015-05-03 07:41:25
【问题描述】:
我正在使用 Visual Studio 来试用 Xamarin.Forms。我正在尝试遵循指南: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getting_started_with_xaml/
简而言之,我使用 PCL 创建了一个 Xamarin.Forms 解决方案,然后尝试将 Forms XAML Page 添加到 PCL 项目中。
创建的代码隐藏如下所示:
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
}
}
这里的问题是InitializeComponent(); 是红色的。
当我尝试构建时,我得知The name 'InitializeComponent' does not exist in the current context
我一直在寻找解决方案,尽管其他人遇到了同样的问题,但他们的解决方案对我不起作用。这是我尝试使用的一个建议: http://blog.falafel.com/xamarin-error-initializecomponent-does-not-exist-in-the-current-context/
如果您有解决此问题的方法,请告诉我。谢谢!
更新:
我的 PCL(我也想在其中添加我的 XAML 页面)包含:
App.cs:
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = "Welcome to Xamarin Forms!"
}
}
}
};
}
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
}
}
还有我的 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="XamaTest.MyXamlPage">
<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
</ContentPage>
代码隐藏:
public partial class MyXamlPage : ContentPage
{
public MyXamlPage()
{
InitializeComponent();
}
}
【问题讨论】:
-
你有什么版本的 xamarin forms 项目。我似乎记得旧项目模板的一个问题。
-
我应该提到我使用的是视觉工作室和试用版。如何检查版本或更新到最新版本?谢谢你的回答。我尝试右键单击 pcl 并从 nuget 下载最新的 xamarin-Forms。同样的问题。
-
你能分享你的项目的源代码吗?
-
谢谢,我更新了问题。如果您需要更多信息,请告诉我。
-
以防万一:将解决方案移动到新文件夹后,我遇到了同样的问题。我不得不编辑 .csproj 文件来手动更新“packages”文件夹,然后它碰巧再次编译。
标签: xamarin xamarin.forms