Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How do I reference Custom Controls within a Xamarin Forms Shared Project?

FB123FB123 CAMember ✭✭
edited August 2015 in Xamarin.Forms

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

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    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.

  • FB123FB123 CAMember ✭✭

    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.

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭
    edited August 2015

    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.

  • michaelwarnesmichaelwarnes USMember ✭✭
    edited September 2017

    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

Sign In or Register to comment.