我有一個包含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