Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 370
Geert van Horrik Avatar asked Jun 03 '14 18:06

Geert van Horrik


1 Answers

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

like image 139
Stephane Delcroix Avatar answered Oct 15 '22 19:10

Stephane Delcroix