我有一个Xamarin表单(我使用Xamarin Studio 5.7)项目,其中包含一个包含UI组件的常见PCL。我只是使用Classes(没有XAML设计师)来启动我的项目,它运行良好,编译,并有一个带有几个子页面的ContentPage。我决定添加一个新的AboutPage.xaml和AboutPage.cs文件,然后使用UI编辑我的表单。所以,我通过新文件创建了我的新页面... Forms ContentPage XAML .....正如我上面提到的,它创建了我的两个文件。
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)
所以,我想可能是我的命名空间(IPSND.Xamarin)的尾部混淆了Xamarin.Forms名称空间的使用头...所以我在这个表单集上更改了我的命名空间(两者都是.cs命名空间和XAML类声明)是IPSND.Test。不幸的是,这导致了相同的错误。
显然,正如Jason在下面的评论中所指出的那样,不仅允许使用using语句的这种配置,而且根据本文档here,设计用于生成的文件。所以,我想这个问题的关键可能与我在PCL库中对Xamarin.Forms的引用有关(有点像错误所说)。使用上面引用的文档,我进入了我的.cs和.xaml文件并使它们完全匹配(根本没有使用任何语句,而且Partial声明中没有来自ContentPage的继承,但这没有帮助....
答案 0 :(得分:8)
好吧,归功于Jason .....他正走在正确的轨道上并将其钉死......我的IPSND.Xamarin命名空间就是问题所在。只是更改特定xaml / cs上的命名空间不是问题,而是我的项目命名空间中有&#39; Xamarin&#39;最后。我制作了整个项目的副本,并将所有内容都移到了IPSND.X中,所有内容都编译得很好。