2015-05-16 129 views
1

我在Xamarin表單導航中遇到問題,因爲它根本不起作用。Xamarin表單導航

我的代碼:

appname.cs

using System; 
using Xamarin.Forms; 
namespace List 
{ 
    public class App : Application { 
     public App() { 
      MainPage = new NavigationPage (new HomePage()); 
     } 
    } 
} 

homepage.xaml.cs

using System; 
using Xamarin.Forms; 
using System.ComponentModel; 
using System.Globalization; 
namespace List 
{ 
    public partial class HomePage : ContentPage { 
     public HomePage() { 
      InitializeComponent(); 
     } 
     public void OnButtonClicked (object sender, EventArgs args) { 
      Navigation.PushAsync (new DetailsPage()); 
     } 
    } 
} 

我的目的顯然是要打開主頁面,有一個按鈕在那裏按一下就進入第二頁。 但是出現這種情況,而不是每當按鈕被點擊:

錯誤信息

Could not load type 'UIKit.UIView_UITextField' from assembly 'Xamarin.Forms.Platform.iOS'. 

有人能告訴我,我做錯了什麼嗎?

編輯 - 爲DetailsPage代碼,因爲這可能是錯誤的來源:

using System; 
using System.Collections.Generic; 
using Xamarin.Forms; 

namespace List 
{ 
    public partial class DetailsPage : ContentPage 
    { 
     public DetailsPage() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

編輯2 - 爲XAML頁面代碼(感謝大家的幫助,我真的很感激):

首頁的.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" xmlns:local="clr-namespace:List" x:Class="List.HomePage"> 
      <ContentPage.Title> 
       1 
      </ContentPage.Title> 
      <Button Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" /> 
    </ContentPage> 

DetailsPage.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="List.DetailsPage"> 
     <Label Text="LOL"/> 
    </ContentPage> 
+0

聽起來好像您的DetailsPage有問題。您的導航代碼是正確的。 – Jason

+0

我注意到你正在使用Xaml。你能不能請張貼你的Xaml,因爲這看起來是錯誤的根源。 –

+0

我會檢查你的iOS項目上安裝了Xamarin Forms nuget包以及你的Forms項目。 –

回答

-3

Navigation.PushModalAsync(new DetailsPage());

also check this link

+0

沒有相同的錯誤消息...從我的理解PushModalAsync和PushAsync非常相似,除了PushModalAsync您不能回到上一頁 – trololol

+0

PushModalAsync支持所有頁面,而不僅僅是一個NavigationPage堆棧。它基本上將頁面放置在當前頁面(或堆棧)的頂部,並且可以使用Navigation.PopModalAsync()方法返回到上一頁。 –

0

在XAML中,第一個元素必須是一個佈局(如果您有多個元素)。我會嘗試類似於下面的內容,看看是否有任何改變。

HomePage.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" xmlns:local="clr-namespace:List" x:Class="List.HomePage" Title="1"> 
     <StackLayout> 
      <Button Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" /> 
     </StackLayout> 
    </ContentPage> 

DetailsPage.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="List.DetailsPage"> 
     <StackLayout> 
      <Label Text="LOL"/> 
     </StackLayout> 
    </ContentPage> 

另外,我注意到你設置你的標題像使用ContentPage.Title 1 ContentPage.Title。這是不必要的。就像我在上面的例子中展示的那樣,直接在你的ContentPage標籤中設置標題。

+0

不是仍然是同樣的問題。這是值得一試,但我的XML格式有點關閉。 – trololol

0

嗯,我和你有同樣的問題。我遇到了xamarin指南的解決方案this

看來,在app.cs你需要調用using Xamarin.Forms.Xaml;

右命名空間上方插入[assembly:XamlCompilation(XamlCompilationOptions.Compile)]

但我真的不知道爲什麼。我正在尋找原因。試着看看它是否解決。在我的情況下進展順利。抱歉,關於這個帖子的壞的英文和壞的編輯,有點新的。但是比較你所有的代碼,它也錯過了你的代碼,也許這就是原因。

EDIT(2017年12月1日) - 添加

我發現this,這或許可以解釋爲什麼不工作,好像我們使用是Xamarin了一套新的,幫助他的部分功能變得更快。

我可能是錯的,如果我是,請更正我的人,知道更好。