我需要在下拉菜单中添加图片 在下拉列表中,我需要先看到图标和下面的文字。但出于某种原因,我无法看到图像,但我看到了空白的地方。
我的项目是Xamarin.Android和MVVMCross。我错过了一些东西,也许我需要一些插件?
我有<?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" />
:
item_city.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="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
在Core中,我有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; }
}
}
:
MypageViewModel.cs
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);
}
}
}
}
:
MypageView.cs
所以我将转换器添加到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
,Xam.Plugin.DeviceInfo
等。
构建成功,但我无法看到我的图像,只有空矩形。为什么呢?
UPD。
我安装了[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
并更改了我的代码。但我仍然无法看到图标。
在调试中它说:
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;
}
}
}
UPD2。
另外,我试着这样做:
{{1}}
但错误是一样的:
[0:] MvxBind:警告:9,54价值&#39;&#39;无法解析为有效的字符串标识符 [0:] MvxBind:警告:9,63价值&#39;&#39;无法解析为有效的字符串标识符 [0:] MvxBind:警告:9,64价值&#39;伦敦&#39;不是一个已知的可绘制名称 [0:] MvxBind:警告:9,65价值&#39;巴黎&#39;不是一个已知的可绘制名称
答案 0 :(得分:2)
我认为你ImageView
中缺少android:scaleType。尝试使用android:scaleType="fitXY"
。
如果不是这样,请尝试使用DrawableName
绑定而不是DrawableId
并且它更好,因为您可以在PCL中使用转换器,使其与{{1和一个像这样的辅助方法:
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");
}
}
或某些内容,告诉您enum
课程中的国家/地区,以便您不必比较转换器中的字符串您还可以在转换器中设置City
,其中哪个城市与哪个国家/地区图片匹配,以便您可以拨打Dictionary
,因此不会使用ifs
答案 1 :(得分:0)
绑定表达式DrawableName Name Converter=IconsConverter
是错误的。你错过了一个逗号:
DrawableName Name, Converter=IconsConverter
或者你可以这样写:
DrawableName Icons(Name)