I tested a way of creating a XAML-defined custom compound control in a separate project and using it in another one.
I created a Xamarin.Forms (template “Blank App (Xamarin.Forms Portable)”) solution named “TestApplication3” (using .NET 4.5).
I set the “TestApplication3.Droid” project to be the startup project.
I added a new project to the solution. I used the “Class Library (Xamarin.Forms Portable)” template and I named the project “AppDevControls”. I deleted the default “AppDevControls.cs” file.
In the References of the “TestApplication3” project, I added the “AppDevControls” project.
In the properties of the solution, I opened the “Project Dependencies” and I verified that the “TestApplication3” project had a dependency on the “AppDevControls” project.
I right-clicked the “AppDevControls” project, I selected “Add – New Item... – Forms Xaml Page” and I filled in the name “Label1.cs”.
I modified the “Label1.xaml” file:
<?xml version="1.0" encoding="utf-8" ?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppDevControls.Label1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" x:Name="innerLabel" />
</Grid>
I modified the “Label1.xaml.cs” file:
`
using Xamarin.Forms;
namespace AppDevControls
{
public partial class Label1 : Grid
{
public static readonly BindableProperty ContentProperty = BindableProperty.Create<Label1, string>(x => x.Content, string.Empty, BindingMode.TwoWay, propertyChanged: (bindable, oldValue, newValue) => { ((Label1)bindable).Content = newValue; } );
///
/// [Bindable] the text content of the label
///
public string Content
{
get
{
return (string)this.GetValue(Label1.ContentProperty);
}
set
{
this.SetValue(Label1.ContentProperty, value);
this.innerLabel.Text = value;
}
}
public Label1()
{
this.InitializeComponent();
}
}
}
`
I right-clicked the “TestApplication3” project, I selected “Add – New Item... – Forms Xaml Page” and I filled in the name “TestPage.cs”.
I modified the “TestPage.xaml” file:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:appDevControls="clr-namespace:AppDevControls;assembly=AppDevControls"
x:Class="TestApplication3.TestPage">
<StackLayout>
<appDevControls:Label1 Content="Hello, world!" />
</StackLayout>
</ContentPage>
I modified the constructor in the “App.cs” file:
`namespace TestApplication3
{
public class App : Application
{
public App()
{
this.MainPage = new TestPage();
}
...
}
}`
I built the solution successfully. I deployed the solution on a Lenovo Yoga Tablet 2-10510F.
When I launched the solution, an exception occurred at the creation of the “TestPage” page:
08-26 17:20:52.259 I/MonoDroid(32373): UNHANDLED EXCEPTION:
08-26 17:20:52.259 I/MonoDroid(32373): System.IO.FileNotFoundException: Could not load file or assembly 'AppDevControls' or one of its dependencies. The system cannot find the file specified.
08-26 17:20:52.259 I/MonoDroid(32373): File name: 'AppDevControls'
08-26 17:20:52.259 I/MonoDroid(32373): at System.AppDomain.Load (System.Reflection.AssemblyName,System.Security.Policy.Evidence) [0x00081] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System/AppDomain.cs:706
08-26 17:20:52.259 I/MonoDroid(32373): at System.AppDomain.Load (System.Reflection.AssemblyName) [0x00000] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System/AppDomain.cs:674
08-26 17:20:52.259 I/MonoDroid(32373): at (wrapper remoting-invoke-with-check) System.AppDomain.Load (System.Reflection.AssemblyName) <IL 0x00033, 0x0008f>
08-26 17:20:52.269 I/MonoDroid(32373): at System.Reflection.Assembly.Load (System.Reflection.AssemblyName) [0x00000] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Reflection/Assembly.cs:551
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.XamlParser.GetElementType (Xamarin.Forms.Xaml.XmlType,System.Xml.IXmlLineInfo) <IL 0x000fd, 0x005af>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.CreateValuesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode,Xamarin.Forms.Xaml.INode) <IL 0x00015, 0x000b7>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor,Xamarin.Forms.Xaml.INode) <IL 0x0008e, 0x0036d>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor,Xamarin.Forms.Xaml.INode) <IL 0x00065, 0x00290>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor,Xamarin.Forms.Xaml.INode) <IL 0x00065, 0x00290>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.XamlLoader.Load (Xamarin.Forms.BindableObject,string) <IL 0x00098, 0x00396>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.XamlLoader.Load (Xamarin.Forms.BindableObject,System.Type) <IL 0x0002d, 0x00113>
08-26 17:20:52.269 I/MonoDroid(32373): at Xamarin.Forms.Xaml.Extensions.LoadFromXaml<TestApplication3.TestPage> (TestApplication3.TestPage,System.Type) <0x0002f>
08-26 17:20:52.269 I/MonoDroid(32373): at TestApplication3.TestPage.InitializeComponent () [0x00001] in c:\users\filip.drsek\documents\visual studio 2015\Projects\TestApplication3\TestApplication3\TestApplication3\obj\Debug\TestPage.xaml.g.cs:20
08-26 17:20:52.269 I/MonoDroid(32373): at TestApplication3.TestPage..ctor () [0x00008] in c:\users\filip.drsek\documents\visual studio 2015\Projects\TestApplication3\TestApplication3\TestApplication3\TestPage.xaml.cs:15
08-26 17:20:52.269 I/MonoDroid(32373): at TestApplication3.App..ctor () [0x00008] in c:\users\filip.drsek\documents\visual studio 2015\Projects\TestApplication3\TestApplication3\TestApplication3\App.cs:14
08-26 17:20:52.269 I/MonoDroid(32373): at TestApplication3.Droid.MainActivity.OnCreate (Android.OS.Bundle) [0x00011] in c:\users\filip.drsek\documents\visual studio 2015\Projects\TestApplication3\TestApplication3\TestApplication3.Droid\MainActivity.cs:20
08-26 17:20:52.269 I/MonoDroid(32373): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-22/src/generated/Android.App.Activity.cs:2741
08-26 17:20:52.269 I/MonoDroid(32373): at (wrapper dynamic-method) object.3c9aa523-e098-4016-9c24-ae31130dfcac (intptr,intptr,intptr) <IL 0x00017, 0x00027>
How is it possible that a project in a solution cannot be found by another project, by which it is referenced?
NuGet says that Xamarin.Forms solution has the version 1.3.3.6323 and Xamarin.Android.Support.v4 has the version 21.0.3.
I renamed the “Content” property of the custom control to “Content1” to avoid a possible collision with a property of the base class, but it does not help.