Bug 38563 - Cannot reference share project converter using Xamarin.Forms
Summary: Cannot reference share project converter using Xamarin.Forms
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-02-09 06:56 UTC by Stephen Marcus
Modified: 2016-12-22 14:16 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Stephen Marcus 2016-02-09 06:56:46 UTC
I have a shared project Xamarin Forms solution.  I felt that the shared idea was very good, and saw no apparent limitations. However, now I am stumped.

The AM in my app is stored in the shared project, obviously.  So here is an example of a content page, starting a the top, and going down to where I declare the SizeConverter:

<?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:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
             xmlns:shared1="clr-namespace:MyApp.Shared;assembly=MyApp.iOS"
             x:Class="MyApp.Views.StatusPage">

	<ContentPage.Resources>
		<shared1:SizeConverter x:Key="SizeConverter" />
	</ContentPage.Resources>


Because the SizeConverter is in the Shared Project, it must link down into the code to find a reference including the Shared Project.  For some odd reason, it picks the IOS project. Ironically, I am actually compiling the Android app when this occurs.

When  I change the startup project to IOS, the converter reference above becomes invalid.  The compiler insists that I then remove the line:

             xmlns:shared1="clr-namespace:MyApp.Shared;assembly=MyApp.iOS"

and replace it with:

             xmlns:shared1="clr-namespace:MyApp.Shared;assembly=MyApp.Droid"

So no matter what I do, I can never change the target project without going into all of my content pages and changing every single one of these references.

Surely you guys had a plan for this, right?

The same problem happens when I try to declare a view model, which is stored in the Shared Project:

	<ContentPage.BindingContext>
		<viewModels:StatusViewModel />
	</ContentPage.BindingContext>

The compiler forces me to add the line:

             xmlns:viewModels="clr-namespace:MyApp.ViewModels;assembly=MyApp.iOS"

Again, referencing the wrong project.  But if I change the target back to IOS, the compiler forces me to remove this reference and replacing it with:

             xmlns:viewModels="clr-namespace:MyApp.ViewModels;assembly=MyApp.Droid"

This is "the embrace of death".  

Please help.
Comment 1 Paul DiPietro [MSFT] 2016-12-21 18:44:29 UTC
If you're still experiencing this problem on the latest stable, you possibly upload a reproduction for us to look at? It'd be helpful in making certain as to what the source of the issue is.
Comment 2 Stephane Delcroix 2016-12-22 14:16:04 UTC
This is one of the reason we strongly advice against NOT using Shared Assets Projects.

Shared Assets Projects aren't real projects, and do not produce assemblies. Instead, the source files are linked and compiled in the host project, and then in the host assembly.

That also means that the SizeConverter defined in MyApp.iOS is totally different than the one defined in MyApp.Android.

There are multiple ways to move forward with this issue on your side:

1/ Host your shared bits of code in a PCL project
2/ Make sure the assembly generated for iOS and Android have the same name (do that in your project preferences), so you can define 'xmlns:shared1="clr-namespace:MyApp.Shared;assembly=MyApp"' and it will magically work
3/ As you're using S.A.P., I guess that your views are, at least physically if not logically, in the same assembly as your shared assets. In that case, you can skip the `assembly` part of the `xmlns` definition: 'xmlns:shared1="clr-namespace:MyApp.Shared'. The Shared assets will then be located.