在MVVMCross Xamarin.Android中绑定下拉列表中可绘制图标的问题
我需要将图片添加到下拉列表中。在MVVMCross Xamarin.Android中绑定下拉列表中可绘制图标的问题,xamarin.android,mvvmcross,mvxbind,Xamarin.android,Mvvmcross,Mvxbind,我需要将图片添加到下拉列表中。 在下拉列表中,我需要先看到图标和下面的文本。但由于某种原因,我看不到图像,尽管我看到的是空的地方 我的项目是Xamarin.Android和MVVMCross。我遗漏了一些东西,也许我需要一些插件 我有MypageView.axml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an
在下拉列表中,我需要先看到图标和下面的文本。但由于某种原因,我看不到图像,尽管我看到的是空的地方 我的项目是Xamarin.Android和MVVMCross。我遗漏了一些东西,也许我需要一些插件 我有
MypageView.axml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar" />
<TextView
android:id="@+id/city"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:textSize="12sp"
android:layout_below="@id/toolbar"
local:MvxBind="Text Strings[CityTextView]" />
<MvxSpinner
android:id="@+id/select_city"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="24dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:spinnerMode="dropdown"
android:layout_below="@id/city"
local:MvxItemTemplate="@layout/item_city"
local:MvxDropDownItemTemplate="@layout/item_city"
local:MvxBind="ItemsSource Cities; SelectedItem SelectedCity" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/cityImage"
android:layout_width="35dp"
android:layout_height="20dp"
android:layout_marginLeft="16dp"
android:scaleType="fitXY"
local:MvxBind="DrawableName Name, Converter=IconsConverter" /> />
<TextView
android:id="@+id/cityName"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/cityImage"
android:textSize="16sp"
local:MvxBind="Text Name" />
</RelativeLayout>
在核心部分,我有City.cs
:
namespace My.cities.Core.Models
{
public class City
{
public City(string Id, string Name, string Flag)
{
this.Id = Id;
this.Name = Name;
this.Flag = Flag;
}
public string Id { get; set; }
public string Name { get; set; }
public string Flag { get; set; }
}
}
using MvvmCross.Core.ViewModels;
using System.Collections.Generic;
using My.cities.Core.Models;
namespace My.cities.Core.ViewModels
{
public class MypageViewModel : BaseViewModel
{
private City _selectedCity;
private List<City> _cities = new List<City>()
{
new City("1", "London", "England")
new City("2", "Paris", "France")
};
public List<City> Cities
{
get { return _cities; }
}
public City SelectedCity
{
get
{
return _selectedCity;
}
set
{
_selectedCity = value;
RaisePropertyChanged(() => SelectedCity);
}
}
}
}
using Android.App;
using MvvmCross.Platform.Converters;
using System;
using System.Globalization;
namespace My.cities.Droid.Views
{ [Activity]
class MypageView : BaseView
{
protected override int LayoutResource => Resource.Layout.MypageView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
public static class CrossDeviceInfoHelper
{
public static string GetLocalImageUrlByPlatform(string name)
{
return CrossDeviceInfo.Current.Platform == Platform.Android ? $"@drawable/{name}" : name;
}
}
public class IconsConverter : MvxValueConverter<string, string>
{
protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
{
if (value == "London")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("england");
if (value == "Paris")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("france");
}
}
和MypageViewModel.cs
:
namespace My.cities.Core.Models
{
public class City
{
public City(string Id, string Name, string Flag)
{
this.Id = Id;
this.Name = Name;
this.Flag = Flag;
}
public string Id { get; set; }
public string Name { get; set; }
public string Flag { get; set; }
}
}
using MvvmCross.Core.ViewModels;
using System.Collections.Generic;
using My.cities.Core.Models;
namespace My.cities.Core.ViewModels
{
public class MypageViewModel : BaseViewModel
{
private City _selectedCity;
private List<City> _cities = new List<City>()
{
new City("1", "London", "England")
new City("2", "Paris", "France")
};
public List<City> Cities
{
get { return _cities; }
}
public City SelectedCity
{
get
{
return _selectedCity;
}
set
{
_selectedCity = value;
RaisePropertyChanged(() => SelectedCity);
}
}
}
}
using Android.App;
using MvvmCross.Platform.Converters;
using System;
using System.Globalization;
namespace My.cities.Droid.Views
{ [Activity]
class MypageView : BaseView
{
protected override int LayoutResource => Resource.Layout.MypageView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
public static class CrossDeviceInfoHelper
{
public static string GetLocalImageUrlByPlatform(string name)
{
return CrossDeviceInfo.Current.Platform == Platform.Android ? $"@drawable/{name}" : name;
}
}
public class IconsConverter : MvxValueConverter<string, string>
{
protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
{
if (value == "London")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("england");
if (value == "Paris")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("france");
}
}
图片England.png
和France.png
在这里直播:My.sities.Droid\Resources\drawable hdpi
,My.sities.Droid\Resources\drawable mdpi
,等等
构建成功,但我看不到我的图像,只有空矩形。为什么?
UPD
我安装了Xam.Plugin.DeviceInfo
并更改了代码。但我还是看不到图标
在debug中,它说:
[0:] MvxBind:Warning: 36,05 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 36,06 Value '' could not be parsed as a valid string identifier
[0:] MvxBind:Warning: 36,06 Value 'London' was not a known drawable name
[0:] MvxBind:Warning: 36,08 Value 'Paris' was not a known drawable name
UPD2.另外,我也试着这样做:
public class IconsConverter : MvxValueConverter<string, int>
{
protected override int Convert(string value, Type targetType,
object parameter, CultureInfo culture)
{
switch (value)
{
case "London":
return Resource.Mipmap.England;
case "Paris":
return Resource.Mipmap.France;
}
}
}
公共类IConConverter:MvxValueConverter
{
受保护的重写int Convert(字符串值,类型targetType,
对象参数,CultureInfo(区域性)
{
开关(值)
{
“伦敦”案:
return Resource.Mipmap.England;
“巴黎”案:
return Resource.Mipmap.France;
}
}
}
但错误是一样的:
[0:]MvxBind:警告:无法将9,54值“”分析为有效的字符串标识符
[0:]MvxBind:警告:无法将9,63值“”分析为有效的字符串标识符
[0:]MvxBind:警告:9,64值“London”不是已知的可绘图名称
[0:]MvxBind:Warning:9,65值“Paris”不是已知的可绘制名称我认为您的
图像视图中缺少。尝试使用android:scaleType=“fitXY”
如果不是这样,请尝试使用DrawableName
绑定而不是DrawableId
,这样会更好,因为您可以在PCL中使用转换器,使其与Xam.Plugin.DeviceInfo
和类似以下的帮助器方法跨平台:
public static class CrossDeviceInfoHelper
{
public static string GetLocalImageUrlByPlatform(string name)
{
// Assuming we are working with Android and iOS
return CrossDeviceInfo.Current.Platform == Platform.Android ? $"@drawable/{name}" : name;
}
}
public class StringToIntValueConverter : MvxValueConverter<string, string>
{
protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
{
if (value == "London")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("england");
if (value == "Paris")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("france");
}
}
因此,您将得到如下转换器:
public static class CrossDeviceInfoHelper
{
public static string GetLocalImageUrlByPlatform(string name)
{
// Assuming we are working with Android and iOS
return CrossDeviceInfo.Current.Platform == Platform.Android ? $"@drawable/{name}" : name;
}
}
public class StringToIntValueConverter : MvxValueConverter<string, string>
{
protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
{
if (value == "London")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("england");
if (value == "Paris")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("france");
}
}
公共类StringToIntValueConverter:MvxValueConverter
{
受保护的重写字符串转换(字符串值、类型targetType、对象参数、CultureInfo区域性)
{
如果(值=“伦敦”)
返回CrossDeviceInfoHelper.getLocalimagerByPlatform(“英格兰”);
如果(值=“巴黎”)
返回CrossDeviceInfoHelper.getLocalimagerByPlatform(“法国”);
}
}
另外,如果我是你,我会在你的
城市
类中添加一个枚举
或一些告诉你国家的东西,这样你就不必在转换器中比较字符串,你也可以在转换器中有一个字典
,其中包含哪个城市与哪个国家的图像,这样你就可以直接调用CrossDeviceInfoHelper.getLocalimagerByPlatform(myDictionary[value])代码>因此不使用ifs我认为您的图像视图中缺少。尝试使用android:scaleType=“fitXY”
如果不是这样,请尝试使用DrawableName
绑定而不是DrawableId
,这样会更好,因为您可以在PCL中使用转换器,使其与Xam.Plugin.DeviceInfo
和类似以下的帮助器方法跨平台:
public static class CrossDeviceInfoHelper
{
public static string GetLocalImageUrlByPlatform(string name)
{
// Assuming we are working with Android and iOS
return CrossDeviceInfo.Current.Platform == Platform.Android ? $"@drawable/{name}" : name;
}
}
public class StringToIntValueConverter : MvxValueConverter<string, string>
{
protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
{
if (value == "London")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("england");
if (value == "Paris")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("france");
}
}
因此,您将得到如下转换器:
public static class CrossDeviceInfoHelper
{
public static string GetLocalImageUrlByPlatform(string name)
{
// Assuming we are working with Android and iOS
return CrossDeviceInfo.Current.Platform == Platform.Android ? $"@drawable/{name}" : name;
}
}
public class StringToIntValueConverter : MvxValueConverter<string, string>
{
protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
{
if (value == "London")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("england");
if (value == "Paris")
return CrossDeviceInfoHelper.GetLocalImageUrlByPlatform("france");
}
}
公共类StringToIntValueConverter:MvxValueConverter
{
受保护的重写字符串转换(字符串值、类型targetType、对象参数、CultureInfo区域性)
{
如果(值=“伦敦”)
返回CrossDeviceInfoHelper.getLocalimagerByPlatform(“英格兰”);
如果(值=“巴黎”)
返回CrossDeviceInfoHelper.getLocalimagerByPlatform(“法国”);
}
}
另外,如果我是你,我会在你的城市
类中添加一个枚举
或一些告诉你国家的东西,这样你就不必在转换器中比较字符串,你也可以在转换器中有一个字典
,其中包含哪个城市与哪个国家的图像,这样你就可以直接调用CrossDeviceInfoHelper.getLocalimagerByPlatform(myDictionary[value])因此,不使用ifs的绑定表达式DrawableName Converter=IConConverter
是错误的。您缺少一个逗号:
DrawableName Name, Converter=IconsConverter
或者,你可以这样写:
DrawableName Icons(Name)
绑定表达式DrawableName Converter=IConConverter
错误。您缺少一个逗号:
DrawableName Name, Converter=IconsConverter
或者,你可以这样写:
DrawableName Icons(Name)
再次感谢你!我安装了Xam.Plugin.DeviceInfo
并测试了这个解决方案。我更改了item\u city
和View.cs
。但由于某些原因,我仍然看不到图标。我编辑了我的帖子,并添加了错误消息。它可以工作,但值之间可以互换。怎么可能呢?你有没有检查过图像的名称是否正确?项目设置是否正确?再次感谢!我安装了Xam.Plugin.DeviceInfo
并测试了这个解决方案。我更改了item\u city
和View.cs
。但由于某些原因,我仍然看不到图标。我编辑了我的帖子,并添加了错误消息。它可以工作,但值之间可以互换。怎么可能呢?你有没有检查过图像的名称是否正确?项目设置是否正确?我尝试使用DrawableName图标(名称)
,但错误是MvxBind:error:10,09未能找到图标的组合器或转换器MvxBind:Warning:10,09值“”无法解析为有效的字符串标识符
,之后我尝试了DrawableName,converter=IConConverter
,但也有一个错误。我好像在名字上漏掉了什么。我将编辑我试图使用DrawableName图标(Name)
的帖子,但错误是MvxBind:Erro