2016-09-27 4 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 
       } 
      }; 
     }   
    } 
} 

나는이 문제를 해결하려면 어떻게해야합니까? 당신의 XAML이로 설정 있도록

답변

1

시도가하는 ContentView 당신에게 ContentPage을 설정 : <ContentView xmlns="http://xamarin.com/schemas/2014/forms" ...

을 그리고 당신의 코드 숨김이 작업을 수행 : public partial class WebView1 : ContentView

Also check out이 스레드를 더에있는 자 마린 포럼에 차이점. 어떤 기본적으로이 내려 온다 :

ContentViewContentPage는 에 대한 기본 클래스로 자신의 견해와 페이지를 사용하기위한 것입니다. 당신이 Page을 할 때, ContentPage를 사용하면 View

을 할 때 당신이 ContentPageContentView을 혼합 한 방법 XAML 코드 통지를 자세히 살펴보면 후 ContentView 사용합니다.

당신의 루트 개체는 같은하는 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.x에서 오류가 발생합니다. 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입니다.

... 
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에없는 이름 변경 : 당신은 당신이 코드에서보기를 정의로 전혀 XAML을 필요로하지 않는 것처럼

0

같습니다 필요하지만 멋지다.)

public WebView1() 
{ 
    Label header = new Label 
    { 
     ... 
    }; 
... 
관련 문제