【发布时间】:2015-04-07 08:10:07
【问题描述】:
我有一个 Xamarin 表单(我正在使用 Xamarin Studio 5.7)项目,其中包含一个包含 UI 组件的通用 PCL。我只是使用类(没有 XAML 设计器)来启动我的项目,它运行良好,可以编译,并且有一个带有几个子页面的 ContentPage。我决定添加一个新的 AboutPage.xaml 和 AboutPage.cs 文件,然后使用 UI 来编辑我的表单。所以,我通过 New File...Forms ContentPage XAML 创建了我的新页面.....正如我上面提到的,它创建了我的两个文件。
关于页面.cs AboutPage.xaml
生成的文件如下所示...
关于页面.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)
所以,然后我想可能是我的命名空间的尾部 (IPSND.Xamarin) 与 Xamarin.Forms 命名空间的使用头部混淆了......所以我在这个表单集上更改了我的命名空间(两者都是.cs 命名空间和 XAML 类声明)为 IPSND.Test。不幸的是,这导致了同样的错误。
显然,正如 Jason 在下面的 cmets 中指出的那样,这种具有 using 语句的配置不仅是允许的,而且根据该文档 here,这是为生成的文件设计的。所以,我猜这个问题的症结可能与我在 PCL 库中对 Xamarin.Forms 的引用更相关(有点像错误所说的)。使用上面引用的文档,我进入了我的 .cs 和 .xaml 文件并让它们完全匹配(根本没有 using 语句,并且在 Partial 声明中没有从 ContentPage 继承,但这并没有帮助......然而.
【问题讨论】:
-
这实际上是导致编译器错误还是什么? using 语句在命名空间声明中有效。
-
哦,废话,我应该把它放在那里......是的......我得到这个错误......这是有道理的......类型或命名空间名称'Forms'不存在在命名空间“IPSND.Xamarin”中(您是否缺少程序集引用?)(CS0234)(IPSND.Xamarin)。所以,似乎认为 usings 是嵌套的(这是有道理的)。
-
我认为是因为您的项目命名空间包含“.Xamarin”,导致冲突
-
我刚刚更新了上面的原始问题并将其更改为 IPSND.Test 并且出现了相同的错误。嗯...我仍在尝试,但还没有找到解决方案。感谢 Jason 的快速反馈。
标签: c# android xamarin xamarin.forms portable-class-library