2

I am wrote this XAML code in an Xamarin.Forms app (multiplatform):

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:MyNameSpace;assembly=MyAssemblyName"
            Padding="0,20,0,0">

<TabbedPage.Children>

    <local:Page1 />
    <local:Page2 />
...

MyNameSpace is the namespace I choose when creating the project, and MyAssemblyName is the project name.

Page1 and Page2 are others XAML/cs pages.

This does not work. I had errors on Xamarin preview and at execution. Those errors said my assembly name is not found or something like that.

I have checked options in iOS and Android sub projects. Those options where auto generated at creation.

I see that assembly names are: MyAssemblyName.iOS and MyAssemblyName.Droid
If I rename both to MyAssemblyName, my project works.

My questions are:

  • Is it a good thing to have the same assembly name for iOS and Android project?

  • Why this tags did not work with default assembly names?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • If you are working with a forms project you would want as much code as possible to be shared and inside of the PCL name space. For example to load in custom controls you would build them in the PCL and reference the assembly as something like MyProject.MyCustomControlFolder.PCL. I cannot think of a scenario where you would want to reference platform specific namespaces in your XAML in a forms project. Platform specific UI work in forms is generally handled through custom renders. – ClintL Feb 17 '17 at 16:35
  • So you mean renaming like i did is the best solution ? – Bob5421 Feb 17 '17 at 16:42
  • No. I mean not referencing platform specific (droid/ios/win) inside your XAML is the best solution. – ClintL Feb 17 '17 at 22:09

1 Answers1

4

You are probably in a "Shared Project". If this is the case, each platform you have in your solution has an assembly, but the Shared Project (the one you write most of the code) doesn't.

The solution for this error is to simply remove the assembly=MyAssemblyName from the xmlns:local string. It should be xmlns:local="clr-namespace:MyNameSpace;"

Luccas Clezar
  • 1,032
  • 10
  • 18
  • It does not work. In fact a created à cross platform forms app when i created the project. When you are talking about PCL, do you have create an empty solution, then add a pcl library ? This is not the same thing ? Did you add individual projects for iOS and Android? In which cases should i go with forms apps and i which cases should i go with pcl ? Thanks – Bob5421 Feb 17 '17 at 16:56
  • When you create a Xamarin.Forms app you have to choose between the Shared Project and the PCL Project. Yes, you can add both types later in the solution but I think that it's not relevant with this problem. No, the projects were created with the entire solution. And to the last question, there's quite a debate about PCL vs. Shared. Personally, I find Shared project to be much better to code, without having to write a bunch of interfaces to invoke native code, but instead using Compiler Directives. Going back to your error, I use this type of xmlns very often without any problems. – Luccas Clezar Feb 17 '17 at 17:12
  • Check the namespaces of `Page1` and `Page2` because if they are inside another folder, the folder name is added to the namespace (e.g. `MyNamespace.Folder.Page1`). In this case, you have to add the local xmlns as `xmlns:local="clr-namespace:MyNameSpace.Folder"`. – Luccas Clezar Feb 17 '17 at 17:15
  • There is no folder – Bob5421 Feb 17 '17 at 17:23
  • There is no debate about PCL vs SAP anymore. Icanza likes SAP the rest of the world decided PCL was superior back in 2014. (Just a joke.. sort of) – ClintL Feb 17 '17 at 22:11