2016-09-27 49 views
0

我正在使用便攜式Xamarin表單項目。當我調試我得到這個錯誤我的網頁類型的對象與目標類型不匹配'Xamarin.Forms.ContentView'

enter image description here

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="Project1.Views.WebView1"> 

    <ContentView.Content> 
    </ContentView.Content> 
</ContentPage> 

xaml.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace Proje1.Views 
{ 
    public partial class WebView1 : ContentPage 
    { 
     public WebView1() 
     { 
      InitializeComponent(); 

      Label header = new Label 
      { 
       Text = "WebView", 
       FontSize = 20, 
       FontAttributes = FontAttributes.Bold, 
       HorizontalOptions = LayoutOptions.Center 
      }; 
      WebView wView = new WebView 
      { 
       Source = new UrlWebViewSource 
       { 
        Url = "https://www.acikakademi.com", 
       }, 
       VerticalOptions = LayoutOptions.FillAndExpand 
      }; 
      this.Content = new StackLayout 
      { 
       Children = 
       { 
        header, 
        wView 
       } 
      }; 
     }   
    } 
} 

我應該怎麼做才能解決這個問題?

回答

1

嘗試ContentPage設置你的ContentView,所以在您的XAML將其設置爲這樣:<ContentView xmlns="http://xamarin.com/schemas/2014/forms" ...

而在你的代碼隱藏做到這一點:public partial class WebView1 : ContentView

Also check out此線程上Xamarin論壇上更的差異。這基本上可以歸結爲這樣:

ContentViewContentPage旨在作爲基類 你自己的看法和頁面。使用ContentPage當你想要一個Page,使用 ContentView當你想要一個View

您的XAML代碼的通知的仔細檢查你怎麼混的ContentPageContentView後。

您根對象聲明瞭一個ContentPage,像這樣:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"...

但是你的內容說,它的一個ContentView

<ContentView.Content> 
</ContentView.Content> 

更改它是一致的,所以使它成爲一個頁面用途如下:

<?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="Project1.Views.WebView1"> 

    <ContentPage.Content> 
    </ContentPage.Content> 
</ContentPage> 

它現在應該可以工作。

+0

但是,當我使它從ContentView繼承,我得到錯誤App.cs.我應該如何設置App.cs中的初始頁面? –

+0

你會得到什麼錯誤?你怎麼試圖顯示你的網頁? –

+0

你好,我問過這裏http://stackoverflow.com/questions/39724234/cannot-implicily-convert-type-to-xamarin-forms-page –

0

更改此:

<?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="Project1.Views.WebView1"> 

    <ContentView.Content> 
    </ContentView.Content> 
</ContentPage> 

<?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="Project1.Views.WebView1"> 

    <ContentPage.Content> 
    </ContentPage.Content> 
</ContentPage> 

在你的情況,有(ContentView.Content,這意味着該類型contentView財產Content你想設置的屬性之間的不匹配)和視圖的類型ContentPage

0

它看起來像你不需要XAML中,就好象你在代碼中定義自己的看法:

... 
Label header = new Label 
{ 
    Text = "WebView", 
    FontSize = 20, 
    FontAttributes = FontAttributes.Bold, 
    HorizontalOptions = LayoutOptions.Center 
}; 
WebView wView = new WebView 
{ 
    Source = new UrlWebViewSource 
    { 
     Url = "https://www.acikakademi.com", 
    }, 
    VerticalOptions = LayoutOptions.FillAndExpand 
}; 
this.Content = new StackLayout 
{ 
    Children = 
    { 
    header, 
    wView 
    } 
}; 
... 

所以,你可以刪除.xaml文件,保持.xaml.cs之一,但最終其重命名爲.cs(不需要,但不錯),並從您的構造函數中刪除InitializeComponent()

public WebView1() 
{ 
    Label header = new Label 
    { 
     ... 
    }; 
... 
相關問題