【问题标题】:Object of type doesn't match target type 'Xamarin.Forms.ContentView'类型的对象与目标类型“Xamarin.Forms.ContentView”不匹配
【发布时间】:2017-02-04 21:40:25
【问题描述】:

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

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

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

【问题讨论】:

    标签: xaml mobile xamarin xamarin.forms


    【解决方案1】:

    尝试将您的 ContentPage 设置为 ContentView,因此在您的 XAML 中将其设置为:&lt;ContentView xmlns="http://xamarin.com/schemas/2014/forms" ...

    在您的代码隐藏中执行以下操作:public partial class WebView1 : ContentView

    Also check out 这个线程在 Xamarin 论坛上了解更多不同之处。这基本上归结为:

    ContentViewContentPage 被用作基类 您自己的视图和页面。当你想要 Page 时,使用 ContentPage,使用 ContentView 当你想要 View

    仔细检查您的 XAML 代码后,请注意您是如何混合使用 ContentPageContentView 的。

    你的根对象声明了一个ContentPage,像这样:

    &lt;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>
    

    现在应该可以工作了。

    【讨论】:

    • 但是当我让它从 ContentView 继承时,我在 App.cs 中得到错误。 App.cs中应该如何设置初始页面?
    • 那你得到什么错误?您是如何尝试显示您的页面的?
    【解决方案2】:

    改变这个:

    <?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 的属性)与视图的类型 ContentPage 不匹配。

    【讨论】:

      【解决方案3】:

      当您在代码中定义视图时,您似乎根本不需要 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
          {
              ...
          };
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-06
        相关资源
        最近更新 更多