【问题标题】:Xamarin InitializeComponent() cannot be used when inherited from application从应用程序继承时无法使用 Xamarin InitializeComponent()
【发布时间】:2016-12-10 22:04:26
【问题描述】:

我遵循了 Xamarin Forms 快速入门指南,但我唯一的错误是: 错误 CS0103 当前上下文中不存在名称“InitializeComponent”。

app.xaml 似乎只在“App”类从“Application”继承时才产生此错误,当我让它从 ContentPage 继承时,它可以工作。

app.xaml 的构建操作是“嵌入式资源”,自定义工具是“MSBuild:UpdateDesignTimeXaml”。我尝试在 app.xaml 上运行自定义工具,但它不起作用。此外,包 Xamarin.Forms 已更新到最新版本,我已删除并重新添加了 app.xaml。

有人对此有解决方案吗?

我正在使用 vs2015 Update 3 和便携式类库。

编辑:

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>

【问题讨论】:

  • 我没有得到这个问题。使用 VS2015 Update 3 和空白 Xaml 应用程序(Xamarin.Forms Portable)进行测试。您是否尝试通过右键单击解决方案资源管理器中的项目并选择“卸载”来卸载和重新加载 PCL 项目,然后在卸载后选择“重新加载”。
  • 我尝试了几次,但它不起作用。 @jgoldberger

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


【解决方案1】:

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

xaml 应该以

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

表示 MyApp 继承自 Application。

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

public class MyApp : Application

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

【讨论】:

  • 我已经添加了我的代码。好像没有不匹配的,这个我已经搜了半个google了。
猜你喜欢
  • 2017-08-13
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多