Reputation: 87
Which namespace and assembly should I use to declare a converter in a shared project in Xamarin.
for this ressource
<TabbedPage.Resources>
<ResourceDictionary>
<local:WidthConverter x:Key="widthConverter"/>
</ResourceDictionary>
</TabbedPage.Resources>
If I choose windowsPhone as Startup Project this declaration work :
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.WinPhone"
If I choose Android as Startup Project this declaration work :
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.Android"
Now what should I use to make it work for MixedPlateform, I tried this but with no success :
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam"
Thank you in advance.
EDIT there is my full code for a better understanding
<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="LeMagXam.View.HomePage"
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam"
Title="LeMag"
BackgroundImage="bg_light.png"
ItemsSource="{Binding CategoriesList}"
>
<TabbedPage.Resources>
<ResourceDictionary>
<local:WidthConverter x:Key="widthConverter"/>
</ResourceDictionary>
</TabbedPage.Resources>
<TabbedPage.ItemTemplate>
<DataTemplate>
<ContentPage Title="{Binding Name}">
<-- ... -->
<Image Source="{Binding Category.ImageArticleTitle.Source}" HorizontalOptions="Start"
WidthRequest="{Binding , Converter={StaticResource widthConverter},
ConverterParameter=150}"
<-- ... -->
</ContentPage>
</DataTemplate>
</TabbedPage.ItemTemplate>
</TabbedPage>
WidthConverter.cs
namespace LeMagXam
{
public class WidthConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (double)parameter * App.RatioWidth;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
#endregion
}
}
Upvotes: 6
Views: 1623
Reputation: 4746
Remember that a Shared Project has no compilable outputs.
Therefore there will be no assembly LeMagXam that would be generated.
Instead the contents of the Shared Project are compiled into the project that references it.
Therefore the following, like you mentioned, will both work in their own platform-specific projects:-
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.WinPhone"
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.Android"
To get around this you may be able to just use:-
xmlns:local="clr-namespace:LeMagXam"
Should this not work, consider creating a PCL project and put the converter within it.
A PCL Project has a compilable output. Hence you would be able to use the same declaration across all XAML pages that uses the same output assembly, like so:-
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXamPCL"
Remember to reference the PCL Project from the platform-specific project.
Upvotes: 2
Reputation: 1581
Ask yourself what the .WinPhone
and .Android
are there for. Remember, the assembly is the name that the class has to reference. LeMagXam
is likely to contain only the common elements, not the specifics for the platform. Think of System.Collection
- that namespace doesn't have List
or Dictionary
in, but System.Collection.Generics
does
What does the width converter do? If it simply a property that does some basic maths (say converts feet to meters), then there is no requirement to have this in a platform specific namespace. If it is setting a platform specific width, then again you don't need to use a platform specific namespace as Xam.Forms already allows you to do that sort of thing anyway with the Device.OnPlatform
(I think it's called) method.
Hope this helps
Upvotes: 0