I'm running into an issue when trying to reference my custom control within my XAML page. It seems that it needs the Assembly, but with the Shared Project template how do I do that since the assembly is based on the platform?
Case 1:
< fa:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:fa="clr-namespace:FrazzApps.Common.Pages;assembly=FrazzApps.Common"
xmlns:local="clr-namespace:MyApp.Views"
x:Class="MyApp.Pages.TimerPage">
< local:TimerView>
< /fa:BaseContentPage>
This fails with on the LoadFromXaml in the InitializeComponent method with an ArgumentNullException : {"Value cannot be null.\r\nParameter name: assemblyName"}
Case 2:
< fa:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:fa="clr-namespace:FrazzApps.Common.Pages;assembly=FrazzApps.Common"
xmlns:local="clr-namespace:MyApp.Views;assembly=MyApp"
x:Class="MyApp.Pages.TimerPage">
< local:TimerView>
< /fa:BaseContentPage>
This fails with on the LoadFromXaml in the InitializeComponent method with an FileNotFoundException : {"Could not load file or assembly 'MyApp, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"MyApp, Culture=neutral, PublicKeyToken=null"}
Case 3:
< fa:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:fa="clr-namespace:FrazzApps.Common.Pages;assembly=FrazzApps.Common"
xmlns:local="clr-namespace:MyApp.Views;assembly=MyApp.WinPhone"
x:Class="MyApp.Pages.TimerPage">
< local:TimerView>
< /fa:BaseContentPage>
This works, but is not a functional solution since it won't work on Android or iOS...
There must be something I'm missing...
Answers
Consider moving your custom controls to a Portable Class Library. Then you will be able to reference the assembly name of the Portable Class Library within the Views in your Shared Project, since PCLs compile as separate libraries.
Yes I could do that but I was hoping that there was a solution available for the project template that would work. I could also just forego the XAML and code the page and control directly.
Keep in mind that the project template is a starting point which helps you get the base projects in place, but there's nothing stopping you from adding projects into the solution. You can still add in one or more Portable Class Libraries into your solution while keeping your shared project and platform specific projects in tact. Simply make sure to add a reference in each platform-specific project to the PCL, if you do choose to grow out your solution that way. However, you could also go the route of adding the control to the views in code. Whatever your preference is, that's perfectly fine. Just don't feel restricted by what the project templates provide as the starting point in your development efforts.
It seems to me that creating a new project just to get round syntactic inadequacies is a bit hacky.
What if you do not want to use a PCL? History has shown the problematic emergence of the .Net standard, and using a shared project is a very reasonable approach in fact.
IMHO the real answer is here Xamarin Forms - Shared Project Headaches, a great blog post that really addresses this question, many thanks to Matthew Soucoup