从应用程序继承时,不能使用Xamarin InitializeComponent()

时间:2016-12-08 21:43:53

标签: c# visual-studio xaml xamarin xamarin.forms

我遵循了Xamarin Forms快速入门指南,我唯一的错误是: 错误CS0103名称' InitializeComponent'在当前背景下不存在。

app.xaml似乎只在类" App"继承自" Application",当我从ContentPage继承时,它可以工作。

app.xaml的构建操作是"嵌入式资源"并且自定义工具是" MSBuild:UpdateDesignTimeXaml"。我尝试在app.xaml上运行自定义工具,但它不起作用。此外,Xamarin.Forms软件包已更新至最新版本,我已删除并读取app.xaml。

有人有解决方案吗?

我正在使用vs2015 Update 3和Portable类库。

编辑:

App.xaml.cs

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Phoneword
{
public partial class App : Application
{
    public App()
    {
        this.InitializeComponent();
        MainPage = new Phoneword.MainPage();
    }

    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
    }
}
}

的App.xaml

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="PhoneWord.App">
<Label Text="{Binding MainText}" VerticalOptions="Center"       HorizontalOptions="Center" />
</Application>

1 个答案:

答案 0 :(得分:0)

你没有提供那么多信息,但我的猜测是基类之间存在不匹配

xaml应该以

开头
<Application ....
             x:Class="MyApp" >

表示MyApp继承自Application。

背后的代码也应该反映这一点:

public class MyApp : Application

如果两种基本类型不匹配,那你就麻烦了。