What namespace and assembly should be used to declare a converter in a common project

What namespace and assembly should be used to declare the converter in a common project in Xamarin.

for this resource

<TabbedPage.Resources> <ResourceDictionary> <local:WidthConverter x:Key="widthConverter"/> </ResourceDictionary> </TabbedPage.Resources> 

If I select WindowsPhone as the Startup Project, this ad works:

  xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.WinPhone" 

If I choose Android as the Startup Project, this declaration will work:

  xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.Android" 

Now, what should I use to make it work for MixedPlateform, I tried this, but without success:

  xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam" 

Thanks in advance.

EDIT is my complete 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 } } 
+6
source share
2 answers

Remember that a generic project does not have compiled outputs.

Therefore, there will be no LeMagXam assembly that will be generated.

Instead, the contents of the Collaborative project are compiled into a project that references it.

Therefore, the following, as you mentioned, will work in their own projects on the platform: -

 xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.WinPhone" xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.Android" 

To get around this, you can simply use: -

 xmlns:local="clr-namespace:LeMagXam" 

If this does not work, think about creating a PCL project and put a converter in it.

A PCL project has compiled output. Therefore, you can use the same ad on all XAML pages that use the same output assembly, for example: -

 xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXamPCL" 

Remember to reference the PCL Project from a platform-oriented project.

+2
source

Ask yourself what .WinPhone and .Android . Remember that assembly is the name that the class must reference. LeMagXam is likely to contain only common elements, not platform specifics. Think of System.Collection - that the namespace does not have a List or Dictionary , but System.Collection.Generics does

What does a width converter do? If this is just a property that performs some basic mathematical calculations (say, converts feet to meters), then there is no requirement to have this in the namespace of a particular platform. If it sets a specific platform width, then you no longer need to use a namespace for a specific platform, since Xam.Forms already allows you to do this anyway using the Device.OnPlatform method (I think it's called).

Hope this helps

0
source

Source: https://habr.com/ru/post/978805/


All Articles