我有一个包含UI组件的常见PCL的Xamarin窗体(我正在使用Xamarin Studio 5.7)项目。我只是使用类(无XAML设计师)来启动我的项目,它运行良好,编译,并有一个ContentPage与几个子页面。我决定添加一个新的AboutPage.xaml和AboutPage.cs文件,并开始使用UI来编辑我的表单。所以,我通过新建文件创建了我的新页面... Forms ContentPage XAML .....正如我上面提到的那样,它创建了我的两个文件。自动生成的XAML.g.cs文件在Xamarin Forms PCL项目中不可编译
AboutPage.cs AboutPage.xaml
生成的文件看起来像这样...
AboutPage.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace IPSND.Xamarin
{
public partial class AboutPage : ContentPage
{
public AboutPage()
{
InitializeComponent();
}
}
}
AboutPage.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="IPSND.Xamarin.AboutPage">
<ContentPage.Content>
<StackLayout>
<Image Id="ImageLogo" Aspect = "AspectFit"></Image>
</StackLayout>
</ContentPage.Content>
</ContentPage>
现在,这看起来很好,我确信包括我的类声明命名空间。但是,当我编译时,生成的AboutPage.xaml.g.cs文件看起来像这样...请注意using语句是如何包含在命名空间内而不是在文件的顶部。不幸的是,这不是可编译的。
我在这里做了什么错?
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18408
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace IPSND.Xamarin {
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
public partial class AboutPage : ContentPage {
private void InitializeComponent() {
this.LoadFromXaml(typeof(AboutPage));
}
}
}
这将导致以下错误
D:\SVN\IPSND.Xamarin\obj\Debug\AboutPage.xaml.g.cs(19,19): Error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'IPSND.Xamarin' (are you missing an assembly reference?) (CS0234) (IPSND.Xamarin)
D:\SVN\IPSND.Xamarin\obj\Debug\AboutPage.xaml.g.cs(19,19): Error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'IPSND.Xamarin' (are you missing an assembly reference?) (CS0234) (IPSND.Xamarin)
D:\SVN\IPSND.Xamarin\obj\Debug\AboutPage.xaml.g.cs(38,38): Error CS0246: The type or namespace name 'ContentPage' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (IPSND.Xamarin)
所以,后来我想也许这是与使用该Xamarin的头部混淆我的命名空间(IPSND.Xamarin)的尾部。表单命名空间...因此,我将这个表单集(.cs名称空间和XAML类声明)上的命名空间更改为IPSND.Test。不幸的是,这导致了相同的错误。
显然,正如Jason在下面的评论中指出的那样,这种使用陈述的配置不仅被允许,而且根据这个文档here,是针对生成的文件而设计的。所以,我猜这个问题的症结可能与我在PCL库中的Xamarin.Forms引用有关(有点像错误说的)。使用上面引用的文档,我进入了我的.cs和.xaml文件,并让它们完全匹配(完全没有使用语句,也没有在Partial声明中从ContentPage继承,但这并没有帮助......但
这是实际上导致编译器错误还是什么?使用声明是有效的内部命名空间声明 – Jason 2015-02-07 04:14:37
哦,废话,我应该把它放在那里......是啊......我得到这个错误......哪个akes sense ...命名空间'IPSND.Xamarin'中没有类型或名称空间名称'Forms'(缺少程序集引用?)(CS0234)(IPSND.Xamarin)。所以,它似乎认为使用是嵌套的(这是有道理的)。 – jaskey 2015-02-07 04:55:12
我认为这是因为你的项目命名空间包含“.Xamarin”,这是导致冲突 – Jason 2015-02-07 05:00:56