类型对象与目标类型'Xamarin.Forms.ContentView'不匹配

时间:2016-09-27 09:53:08

标签: xaml mobile xamarin xamarin.forms

我正在使用便携式Xamarin表单项目。调试页面时出现此错误

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

我该怎么做才能解决这个问题?

3 个答案:

答案 0 :(得分: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

你的root对象声明了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>

现在应该可以了。

答案 1 :(得分: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表示类型Content上的属性contentView)与View的类型{{之间存在不匹配1}}。

答案 2 :(得分: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
    {
        ...
    };
...