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