Android MvvmCross:如何设置微调器(MvxSpinner)的样式?
所以,问题很简单。如何应用MvxSpinner的样式 关于设置普通微调器的样式,有很多答案,但大多数都涉及以下代码行:Android MvvmCross:如何设置微调器(MvxSpinner)的样式?,android,xamarin,spinner,mvvmcross,mvxspinner,Android,Xamarin,Spinner,Mvvmcross,Mvxspinner,所以,问题很简单。如何应用MvxSpinner的样式 关于设置普通微调器的样式,有很多答案,但大多数都涉及以下代码行: var adapter = new ArrayAdapter<string>(SupportActionBar.ThemedContext, Resource.Layout.event_selector, events); adapter.SetDropDownViewResource(global::Android.Resource.Layout.Simple
var adapter = new ArrayAdapter<string>(SupportActionBar.ThemedContext, Resource.Layout.event_selector, events);
adapter.SetDropDownViewResource(global::Android.Resource.Layout.SimpleSpinnerDropDownItem);
_eventSelector.Adapter = adapter;
var adapter=new ArrayAdapter(SupportActionBar.Context、Resource.Layout.event\u选择器、事件);
SetDropDownViewResource(全局::Android.Resource.Layout.SimpleSpinnerDropDownItem);
_eventSelector.Adapter=适配器;
然而,我不知道在MvxSpinner的情况下如何处理这个问题。它可能使用一些MvvmCross-specific自己的适配器。我的约束力如下:
<Mvx.MvxSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstCategoryGroups"
local:MvxBind="ItemsSource CategoryGroups; SelectedItem CategoryGroupdSelected"
/>
有什么想法吗?
这两种方法都是:将页面上特定微调器元素的样式设置为任何微调器的公共样式。谢谢 您可以通过两个属性直接在axml中更改微调器的布局(从而更改样式):
:设置所选项目的布局local:MvxItemTemplate
:设置下拉列表中每个项目的布局local:MvxDropDownItemTemplate
CategoryGroups
和selectedcategorygroups
的视图模型,即:
public class MyViewModel : MvxViewModel
{
...
public ObservableCollection<CategoryGroup> CategoryGroups { get; set; }
public CategoryGroup CategoryGroupdSelected { get; set; }
...
}
可以将集合绑定到微调器,如下所示:
<MvxSpinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:theme="@style/Spinner"
local:MvxItemTemplate="@layout/my_spinner_item"
local:MvxDropDownItemTemplate="@layout/my_spinner_dropdown_item"
local:MvxBind="ItemsSource CategoryGroups; SelectedItem CategoryGroupdSelected" />
我的微调器下拉列表\u item.axml:
<?xml version="1.0" encoding="utf-8"?>
<TextView 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="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="@android:color/black"
android:textSize="25sp"
local:MvxBind="Text Name" />
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="@android:color/black"
android:alpha="0.8"
local:MvxBind="Text Name" />
另一种方法是使用wrappedetimviewmodel
传递要位于微调器中的对象T
,使用Func
返回微调器显示的内容(描述),使用description
属性仅调用Func
。这种方法的优点是,它不会使您总是从VM继承,而且您可以将几乎任何对象包装到VM中,并将行为添加到微调器中
public class WrappedItemViewModel<T> : MvxNotifyPropertyChanged
{
private readonly Func<T, string> descriptionFunc;
public WrappedItemViewModel(T item, Func<T, string> descriptionFunc)
{
this.MyItem = item;
this.descriptionFunc = descriptionFunc;
}
public T MyItem { get; }
public string Description => this.descriptionFunc.Invoke(this.MyItem);
}
公共类WrappedItemViewModel:MvxNotifyPropertyChanged
{
私有只读函数descriptionFunc;
公共WrappedItemViewModel(T项,函数描述函数)
{
this.MyItem=项目;
this.descriptionFunc=descriptionFunc;
}
公共T MyItem{get;}
公共字符串描述=>this.descriptionFunc.Invoke(this.MyItem);
}
您可以通过两个属性直接在axml中更改微调器的布局(从而更改样式):
:设置所选项目的布局local:MvxItemTemplate
:设置下拉列表中每个项目的布局local:MvxDropDownItemTemplate
CategoryGroups
和selectedcategorygroups
的视图模型,即:
public class MyViewModel : MvxViewModel
{
...
public ObservableCollection<CategoryGroup> CategoryGroups { get; set; }
public CategoryGroup CategoryGroupdSelected { get; set; }
...
}
可以将集合绑定到微调器,如下所示:
<MvxSpinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:theme="@style/Spinner"
local:MvxItemTemplate="@layout/my_spinner_item"
local:MvxDropDownItemTemplate="@layout/my_spinner_dropdown_item"
local:MvxBind="ItemsSource CategoryGroups; SelectedItem CategoryGroupdSelected" />
我的微调器下拉列表\u item.axml:
<?xml version="1.0" encoding="utf-8"?>
<TextView 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="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="@android:color/black"
android:textSize="25sp"
local:MvxBind="Text Name" />
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="@android:color/black"
android:alpha="0.8"
local:MvxBind="Text Name" />
另一种方法是使用wrappedetimviewmodel
传递要位于微调器中的对象T
,使用Func
返回微调器显示的内容(描述),使用description
属性仅调用Func
。这种方法的优点是,它不会使您总是从VM继承,而且您可以将几乎任何对象包装到VM中,并将行为添加到微调器中
public class WrappedItemViewModel<T> : MvxNotifyPropertyChanged
{
private readonly Func<T, string> descriptionFunc;
public WrappedItemViewModel(T item, Func<T, string> descriptionFunc)
{
this.MyItem = item;
this.descriptionFunc = descriptionFunc;
}
public T MyItem { get; }
public string Description => this.descriptionFunc.Invoke(this.MyItem);
}
公共类WrappedItemViewModel:MvxNotifyPropertyChanged
{
私有只读函数descriptionFunc;
公共WrappedItemViewModel(T项,函数描述函数)
{
this.MyItem=项目;
this.descriptionFunc=descriptionFunc;
}
公共T MyItem{get;}
公共字符串描述=>this.descriptionFunc.Invoke(this.MyItem);
}
感谢您的快速回复。但是,您是否也可以提供(答案中可能有更新)my_spinner_项目模板的详细信息。我的意思是,例如,TextView也应该以某种方式绑定到数据。我想,如果没有额外的操作,它是不会工作的?(特别是,如果我想让它成为任何微调器的通用模板)。@Agat那里我用一些方法更新了答案,以实现你想要的。Hiht关于要绑定的ItemViewModel和TextValue属性,还不完全清楚。仍然无法让它工作。你能澄清一下吗?比如说,如果我有MyCurrentViewModel和一组产品(例如,具有自己属性的简单字符串或产品类),还有一个ProductSelected属性。因此,我无法理解所有这些例程中的TextValue属性是什么。我已根据您问题的类别组更新了答案。对于产品
,文本值
将是产品
中的一个属性。我希望现在更清楚了。谢谢你的快速回复。但是,您是否也可以提供(答案中可能有更新)my_spinner_项目模板的详细信息。我的意思是,例如,TextView也应该以某种方式绑定到数据。我想,如果没有额外的操作,它是不会工作的?(特别是,如果我想让它成为任何微调器的通用模板)。@Agat那里我用一些方法更新了答案,以实现你想要的。Hiht关于要绑定的ItemViewModel和TextValue属性,还不完全清楚。仍然无法让它工作。你能澄清一下吗?比如说,如果我有MyCurrentViewModel和一组产品(例如,具有自己属性的简单字符串或产品类),还有一个ProductSelected属性。因此,我无法理解所有这些例程中的TextValue属性是什么。我已根据您问题的类别组更新了答案。对于产品
,文本值
将是产品
中的一个属性。我希望现在更清楚了。