2016-09-27 8 views
0

私は移植可能なXamarinフォームプロジェクトを使用しています。タイプのオブジェクトがターゲットタイプ 'Xamarin.Forms.ContentView'と一致しません

<?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::私はデバッグするとき、私は私のページ

enter image description here

XAMLをこのエラーが出る

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

はあなたのXAMLでこれにそれを設定するので、ContentViewにあなたContentPageを設定してください:public partial class WebView1 : ContentView

Also check outより上Xamarinフォーラムでこのスレッド:<ContentView xmlns="http://xamarin.com/schemas/2014/forms" ...

そして、あなたのコードビハインドでは、これを行います違いのこれは基本的にはこれに尽きる:

ContentViewContentPageが 独自のビューとページのための基本クラスとして使用されることを意図しています。あなたがPageをしたいとき、あなたがContentPageContentViewを混ぜてきたどのようにあなたのXAMLコード通知の綿密に検討した後View

をしたいとき ContentViewを使用し、ContentPageを使用してください。

あなたのルートオブジェクトはそうのような、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"> 

    <ContentPage.Content> 
    </ContentPage.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"> 

    <ContentView.Content> 
    </ContentView.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 1を維持するが、最終的に.csに名前を変更することができます(ありません良いですが)、コンストラクタからInitializeComponent()を削除してください:

public WebView1() 
{ 
    Label header = new Label 
    { 
     ... 
    }; 
... 
関連する問題