我应该使用哪个命名空间和程序集在Xamarin中的共享项目中声明转换器.
对于这个资源
<TabbedPage.Resources>
<ResourceDictionary>
<local:WidthConverter x:Key="widthConverter"/>
</ResourceDictionary>
</TabbedPage.Resources>
Run Code Online (Sandbox Code Playgroud)
如果我选择windowsPhone作为启动项目这个声明工作:
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.WinPhone"
Run Code Online (Sandbox Code Playgroud)
如果我选择Android作为启动项目这个声明工作:
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam.Android"
Run Code Online (Sandbox Code Playgroud)
现在我应该使用什么来使它适用于MixedPlateform,我试过这个但没有成功:
xmlns:local="clr-namespace:LeMagXam;assembly=LeMagXam"
Run Code Online (Sandbox Code Playgroud)
先感谢您.
编辑有我的完整代码,以便更好地理解
<?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>
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
问问自己.WinPhone
和 的.Android
用途是什么。请记住,程序集是类必须引用的名称。LeMagXam
可能只包含通用元素,而不包含平台的具体信息。想想System.Collection
- 该命名空间没有List
或Dictionary
在,但System.Collection.Generics
有
宽度转换器有什么作用?如果它只是一个执行一些基本数学运算的属性(例如将英尺转换为米),则不需要将其放在特定于平台的命名空间中。如果它设置特定于平台的宽度,那么您也不需要使用特定于平台的命名空间,因为 Xam.Forms 已经允许您使用Device.OnPlatform
(我认为它被称为)方法来执行此类操作。
希望这可以帮助