Android数据绑定(类似于WPF)? [英] Android data-binding (similar to WPF)?

查看:91
本文介绍了Android数据绑定(类似于WPF)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布之前,我已经搜索了一些类似的问题 - 但是我在Android和数据绑定方面有一个一般性的问题(而其他的答案我没有真正让我进一步...)。假设你有一个类车辆:

I have searched for some similar questions before posting - however I have a general question when it comes to Android and data binding (and the other answers I check did not really get me much further...). Assume you have a class Vehicle:

public class Vehicle {

private Owner owner;
private String brand;

//getter and setter for above attributes...

}

,这里是所有者类....

and here is the Owner class ....

public class Owner {

private String name;

}

现在 - 我刚刚在调查MVVM(ModelView- ViewModel)模式由Microsoft WPF使用。这让我想知道:假设我想绑定作为Vehicle对象的一个​​孩子的所有者对象的name属性 - Android中有没有一些标准的方法来实现这一点?另外假设我可能需要验证输入,才能使模型更新?

Now - I was just recently looking into MVVM (ModelView-ViewModel) pattern as employed by Microsofts WPF. Which got me wondering: Assuming I would want to bind the name property of my owner object which is a child of the Vehicle object - would there be some standard way in Android to achieve this? Also presuming, that I might have to validate input before I can have the Model updated?

我想像下面的组件(假设MVVM):

I was imagining the following components (assuming MVVM):


  • 视图(一个活动)不包含应用程序逻辑 - 所以它或多或少是空的

  • 一个ViewModel将处理该实例的车辆对象并对其执行操作

  • 模型本身将看起来像以前发布的代码 - 完全忘记了View
    和ViewModel

现在,当我将EditTexts,TextViews等添加到视图中时,我希望它们绑定到我的上下文对象的某些属性(在这种情况下为Vehicle )...嗯,如果我的问题不清楚或者你需要进一步的信息,请告诉我:)提前感谢。

Now when I add my EditTexts, TextViews and so on to the view, I want them to bind to certain the properties of my context object (Vehicle in this case) ... Mhhh if my question is not clear or you need further informatio do let me know :) thanks in advance.

Ps我认为熟悉WPF的人可能现在是我的意思?我自己刚刚阅读了关于WPF,发现它是一个很好的处理方式。

P.s. I think people familiar with WPF might now what I mean? I myself just read about WPF and found it's a nice way to handle stuff.

PP我知道 android绑定项目,但我想知道Android中是否有一种内置方法,或者也许某个惯例有人正在关注:)这真的是更多的约束理论问题我猜...

P.P.s I am aware of the android binding project but I was wondering if there is a sort of build-in approach in Android or maybe some convention someone is following :) this really is more of a binding-theory question I guess ...

推荐答案

Android SDK中没有任何烘焙 MS WPF中的数据绑定功能。 Google正在为运行Android的各种设备提供一个较低级别的界面。到目前为止,Android开发社区还没有出现更高级别的应用程序框架。 (IMHO,由于所有各种Android设备的框架都有不同的限制,所以像Google这样的怪物公司就会创建这样的事情。)

There is nothing "baked" into the Android SDK which provides equivalent databinding functionality found in MS WPF. Google is providing a lower level interface for the various devices running Android. To date, higher level application frameworks have not emerged from the Android development community. (IMHO, it would take a monster company like Google to create such a thing, given all the different constraints on a framework for all the various Android devices.)

你可以在您自己的应用程序中创建一组数据绑定类,以处理您对MVVM模式的需求。 数据绑定器将在Android View对象上设置相关的事件处理程序,并在您的ViewModel对象上运行一些适当的方法作为响应。他们还会将ViewModel上的更改事件(您定义的)转换为View对象上的相应属性分配。

You could create a set of databinding classes in your own application to handle your needs for the MVVM pattern. The "Databinders" would set the relevant event handlers on Android View objects and run some appropriate method on your ViewModel objects in response. They would also translate change events (that you define) on the ViewModel into the appropriate property assignments on the View objects.

是否对您的应用程序来说是值得的一个判断电话。我的猜测是,编写Databinder类需要更多的努力,而不仅仅是直接钩住较低级别的View事件处理程序。框架很有用,主要是当您不必自己编写它们时。

Whether that turns out to be worthwhile for your application is a judgment call. My guess is that it would require more effort to write the Databinder classes than just to hook the lower level View event handlers directly. Frameworks are useful primarily when you didn't have to write them yourself.

这篇关于Android数据绑定(类似于WPF)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆