简体   繁体   中英

Use custom base class for ContentPage in Xamarin.Forms

I want to use a custom ContentPage for my pages in Xamarin.Forms. I tried creating a custom class and specify the page in xaml like this:

<local:ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   xmlns:local="clr-namespace:XamarinFormsApp.Controls"
                   x:Class="XamarinFormsApp.Views.WelcomeView">

    <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />

</local:ContentPage>

This would (normally) work in xaml, but gives me this error in Xamarin.Forms:

Error   7   The "XamlG" task failed unexpectedly.
System.Exception: Can't load types from xmlns clr-namespace:XamarinFormsApp.Controls
   at Xamarin.Forms.Build.Tasks.XamlG.GetNamespace(String namespaceuri)
   at Xamarin.Forms.Build.Tasks.XamlG.GetType(String nsuri, String type, IList`1 typeArguments, Func`2 getNamespaceOfPrefix)
   at Xamarin.Forms.Build.Tasks.XamlG.GenerateFile(String xamlFile, String outFile)
   at Xamarin.Forms.Build.Tasks.XamlG.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__20.MoveNext() XamarinFormsApp.iOS

Is this (yet) unsupported or am I doing something wrong?

this was fixed in Xamarin.Forms 1.0.6197 released on 2014-06-04.

also, your xmlns:local misses the assembly attribute. It should be xmlns:local="clr-namespace:XamarinFormsApp.Controls;assembly=XamarinFormsApp"

UPDATE: the assembly part in the xmlns is no longer required if you're loading types from the same assembly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM