Bengi Besceli
Bengi Besceli

Reputation: 3748

Object of type doesn't match target type 'Xamarin.Forms.ContentView'

I'm using portable Xamarin forms project. I get this error when I debug my page

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
                }
            };
        }        
    }
}

What should I do to fix this ?

Upvotes: 0

Views: 1260

Answers (3)

Gerald Versluis
Gerald Versluis

Reputation: 34013

Try to set you ContentPage to a ContentView, so in your XAML set it to this: <ContentView xmlns="http://xamarin.com/schemas/2014/forms" ...

And in your code-behind do this: public partial class WebView1 : ContentView

Also check out this thread on the Xamarin Forums on more of the differences. Which basically comes down to this:

ContentView and ContentPage are meant to be used as base class for your own views and pages. Use ContentPage when you want a Page, use ContentView when you want a View

After closer examination of your XAML code notice how you've mixed the ContentPage and ContentView.

You root object declares a ContentPage, like so:

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

But your Content says its a ContentView:

<ContentView.Content>
</ContentView.Content>

Change it to be consistent, so make it a page to use is as one, like this:

<?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>

It should now work.

Upvotes: 1

Stephane Delcroix
Stephane Delcroix

Reputation: 16232

It looks like you do not need the Xaml at all as you define your view in code:

...
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
  }
};
...

So you can delete the .xaml file, keep the .xaml.cs one but eventually rename it to .cs (not required, but nice), and remove the InitializeComponent() from your constructor:

public WebView1()
{
    Label header = new Label
    {
        ...
    };
...

Upvotes: 0

Stephane Delcroix
Stephane Delcroix

Reputation: 16232

Change this:

<?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>

to

<?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>

In your case, there is a mismatch between the property you're trying to set (ContentView.Content which means the property Content on the type contentView) and the View's type ContentPage.

Upvotes: 0

Related Questions