从RadioGroup捕获选定的RadioButton [英] Capture selected RadioButton from the RadioGroup
问题描述
我有radiobutton group
,其中有两个radiobuttons
,如下所示.我能够看到他们两个.我从以下示例代码中使用了相同的方法
https://github.com/MvvmCross /MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Core/ViewModels/ViewModels.cs
I have radiobutton group
, which has two radiobuttons
as follows. I could able to see both of them. I have used the same approach from the following sample code
https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Core/ViewModels/ViewModels.cs
当我调试代码以查看选择了哪个单选按钮时,我在SelectedItem
中放置了一个调试点,但是当我更改单选按钮时,它既没有设置也没有进入SelectedItem
中.如何捕获选择了哪个radiobutton
When I debug the code in order to see which radio button is selected, I put a debug point in the SelectedItem
but when I change radio button it does hit neither set or get in the SelectedItem
. How do I capture which radiobutton
is selected
ViewModel.axml
<MvxRadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textSize="40dp"
local:MvxItemTemplate="@layout/item_radio"
local:MvxBind="ItemsSource Items;SelectedItem SelectedItem" />
Item_Radio.axml
<?xml version="1.0" encoding="utf-8"?>
<RadioButton 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:buttonTint="@color/primary"
local:MvxBind="Text Caption" />
ViewModel.cs
private List<Thing> _items = new List<Thing>()
{
new Thing("Open"),
new Thing("Close"),
};
public List<Thing> Items
{
get { return _items; }
set { _items = value; RaisePropertyChanged(() => Items); }
}
private Thing _selectedItem = new Thing("Open");
public Thing SelectedItem
{
get { return _selectedItem; }
set { _selectedItem = value; RaisePropertyChanged(() => SelectedItem); }
}
Thing.cs
public class Thing
{
public Thing(string caption)
{
Caption = caption;
}
public string Caption { get; private set; }
public override string ToString()
{
return Caption;
}
public override bool Equals(object obj)
{
var rhs = obj as Thing;
if (rhs == null)
return false;
return rhs.Caption == Caption;
}
public override int GetHashCode()
{
if (Caption == null)
return 0;
return Caption.GetHashCode();
}
}
推荐答案
如果在输出窗口中收到MvxBind警告:
If you are getting an MvxBind Warning in your output window:
MvxBind:警告:无法创建用于绑定的目标绑定 SelectedItem的SelectedItem
MvxBind:Warning: Failed to create target binding for binding SelectedItem for SelectedItem
您有两种解决方法:
选项1:
Option 1:
更新到Mvvmcross版本4.1.6.此版本将自动为您注册MvxAppCompatSetupHelper.FillTargetFactories
.
Update to version 4.1.6 of Mvvmcross. This version will automatically register MvxAppCompatSetupHelper.FillTargetFactories
for you.
选项2:
Option 2:
在setup.cs中,您需要手动注册MvxAppCompatSetupHelper.FillTargetFactories
:
In you setup.cs you need to register MvxAppCompatSetupHelper.FillTargetFactories
manually:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
MvxAppCompatSetupHelper.FillTargetFactories(registry);
base.FillTargetFactories(registry);
}
这篇关于从RadioGroup捕获选定的RadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!