Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which namespace and assembly should I use to declare a converter in a shared project

Tags:

c#

xaml

xamarin

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
    }
}
like image 747
Maxime Avatar asked Nov 27 '14 18:11

Maxime


1 Answers

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.

like image 83
Pete Avatar answered Nov 14 '22 23:11

Pete