Mar*_*tin 5 c# xamarin.android mvvmcross
我正在开发一个跨平台的应用程序,在android中启动它.我找到了你的MVVMCross项目,我正试图进入它.现在我对它完全陌生,不知道如何将我的WebService-Results绑定到我的ListView.这里有一些XAML作为示例,我正在尝试它:
xmlns:mobsales="http://schemas.android.com/apk/res/MobSales.DroidUI"
...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
mobsales:MvxItemTemplate="@layout/listitem_customer"
mobsales:MvxBind="{'ItemSource':{'Path':'Customer'}}" />
...
Run Code Online (Sandbox Code Playgroud)
看起来像这样
<cirrious.mvvmcross.binding.android.views.MvxBindableListView
android:id="@+id/autocomplete"
android:layout_below="@id/txtfield"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
mobsales:MvxItemTemplate="@layout/listitem_customer"
mobsales:MvxBind="{'ItemSource':{'Path':'Customers'}}" />
Run Code Online (Sandbox Code Playgroud)
当我徘徊最后两行时,工具提示表示属性未声明.我真的不知道你是怎么做到的.你能给我一些建议吗?我想我必须在我的UI项目的值中编写一些xml,对吧?
另一个问题:我怎么能使用AutoCompleteTextViews?我必须首先编写自己的MvXBindables吗?有什么建议?:-)
要使这些属性绑定,您需要包含命名空间 - 它看起来就像您所做的那样.
您还需要将MvxBindingAttributes.xml文件包含在UI项目中 - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross.Binding/ResourcesToCopy/MvxBindingAttributes.xml - 您必须设置将此文件的操作构建到"AndroidResource"
有关示例,请参阅任何Android示例项目 - https://github.com/slodge/MvvmCross
对于关于添加绑定的问题的第二部分,绑定框架应该自动单向绑定(从ViewModel到View)到任何Monodroid View/widget上的现有公共属性.
如果公共属性的类型不正确(例如,它是一些Android枚举而不是View),那么您可以使用IMvxValueConverter进行转换.
如果要进行双向绑定,或者没有要绑定的公共属性,那么您可以轻松地进行自定义绑定.有关此示例,请参阅会议示例中的自定义IsFavorite 2方式绑定
此代码为每个Android Button添加了一个新的可绑定伪属性"IsFavorite".
...这是使用以下代码在Setup.cs中初始化的:
protected override void FillTargetFactories(MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterFactory(
new MvxCustomBindingFactory<Button>(
"IsFavorite",
(button) => new FavoritesButtonBinding(button)));
}
Run Code Online (Sandbox Code Playgroud)
......而绑定代码是:
public class FavoritesButtonBinding
: MvxBaseAndroidTargetBinding
{
private readonly Button _button;
private bool _currentValue;
public FavoritesButtonBinding(Button button)
{
_button = button;
_button.Click += ButtonOnClick;
}
private void ButtonOnClick(object sender, EventArgs eventArgs)
{
_currentValue = !_currentValue;
SetButtonBackground();
FireValueChanged(_currentValue);
}
public override void SetValue(object value)
{
var boolValue = (bool)value;
_currentValue = boolValue;
SetButtonBackground();
}
private void SetButtonBackground()
{
if (_currentValue)
{
_button.SetBackgroundResource(Resource.Drawable.star_gold_selector);
}
else
{
_button.SetBackgroundResource(Resource.Drawable.star_grey_selector);
}
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
_button.Click -= ButtonOnClick;
}
base.Dispose(isDisposing);
}
public override Type TargetType
{
get { return typeof(bool); }
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.TwoWay; }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7061 次 |
| 最近记录: |