我正在使用Visual Studio尝试使用Xamarin.Forms。我正在尝试按照指南操作:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getting_started_with_xaml/
简而言之,我创建了一个Xamarin.Forms解决方案,使用PCL,然后尝试将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();
}
}