日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
HarmonyOSDataBinding使用指南

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供漳浦網(wǎng)站建設(shè)、漳浦做網(wǎng)站、漳浦網(wǎng)站設(shè)計(jì)、漳浦網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、漳浦企業(yè)網(wǎng)站模板建站服務(wù),10多年漳浦做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.

在開始講DataBinding之前,我們不得不先說一下MVVM架構(gòu)模式,MVVM是MVP模式的改進(jìn)版,Model層跟View層與MVP模式類似,ViewModel層只做和邏輯處理相關(guān)的工作,在ViewModel中不會(huì)持有View層的引用,這時(shí)候就需要借助DataBinding,通過Binding方式通信,只需要在ViewModel層對(duì)數(shù)據(jù)進(jìn)行操作,View層就會(huì)自動(dòng)更新UI。

概述

? Databinding 顧名思義就是數(shù)據(jù)綁定,HarmonyOS為提供了Databinding庫(kù),該庫(kù)允許你使用聲明格式而不是以代碼的方式將數(shù)據(jù)綁定到UI上。Databinding庫(kù)會(huì)解析布局文件,自動(dòng)生成數(shù)據(jù)綁定代碼,實(shí)現(xiàn)數(shù)據(jù)源與UI組件之間的相互綁定。

? 自動(dòng)生成綁定代碼的基類,是用來實(shí)現(xiàn)ComponentContainer和ActiveData對(duì)象之間的綁定,ComponentContainer是指組件容器,相當(dāng)于Android的ViewGroup,ActiveData是一個(gè)可觀察數(shù)據(jù)類,同時(shí)也具有生命周期感知,作用類似于Android的LiveData。

? 當(dāng)ComponentContainer或ActiveData對(duì)象被修改時(shí),DataBinding對(duì)象會(huì)自動(dòng)修改綁定到ComponentContainer或ActiveData的對(duì)象。比如,如果你修改了某個(gè)ComponentContainer的屬性,DataBinding會(huì)將屬性值到綁定到該ComponentContainer的ActiveData對(duì)象。反之亦然,如果一個(gè)ActiveData對(duì)象的屬性值被更改,綁定的ComponentContainer的屬性值也將被更新。

開始使用

1.在使用DataBinding之前,首先要在應(yīng)用模塊下build.gradle中開啟dataBinding,代碼如下:

 
 
 
 
  1. ohos {
  2.     ...
  3.     buildTypes {
  4.         debug {
  5.             dataBindingEnabled true
  6.         }
  7.     }
  8. }

 2.使用DataBinding之前,首先使用ActiveData對(duì)象來定義要觀察的數(shù)據(jù),并實(shí)現(xiàn)其0get() 和set()方法:

創(chuàng)建一個(gè)Model類

 
 
 
 
  1. public class MainAbilityModel {
  2.     private ActiveData titile;
  3.     public ActiveData getTitile() {
  4.         return titile;
  5.     }
  6.     public void setTitile(ActiveData titile) {
  7.         this.titile = titile;
  8.     }
  9. }

 3.在我們的布局文件中,聲明DataBinding的綁定標(biāo)簽 ,并在 中定義剛創(chuàng)建的ActiveData Model類。

 
 
 
 
  1.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  2.     ohos:height="match_parent"
  3.     ohos:width="match_parent"
  4.     ohos:background_element="#1a1a1a"
  5.     ohos:orientation="vertical">
  6.     
  7.         ohos:id="$+id:title_text"
  8.         ohos:height="300"
  9.         ohos:width="match_parent"
  10.         ohos:text="${model.titile}"
  11.         ohos:text_alignment="center"
  12.         ohos:text_color="#FF555555"
  13.         ohos:text_size="50"/>
  14.     <....>
  15.     
  16.         
  17.             class="com.example.time.model.MainAbilityModel"
  18.             name="model"/>
  19.     

的ohos:text屬性,進(jìn)行數(shù)據(jù)綁定ohos:text=“${model.titile}”

4.在布局中聲明DataBinding后,系統(tǒng)會(huì)在編譯后自動(dòng)生成一個(gè)以布局文件命名的Binding類,比如我的布局文件名為ability_main,那么系統(tǒng)就會(huì)自動(dòng)生成一個(gè)AbilityMainBinding類。我們?cè)赟lice類中調(diào)用DataBindingUtil.createBinding方法來獲取AbilityMainBinding的對(duì)象,然后調(diào)用initComponent及setLifecycle來初始化對(duì)象,之后我們就可以調(diào)用在ActiveData對(duì)象設(shè)置數(shù)據(jù),調(diào)用MainAbilityModel中定義的方法,綁定到其中的ActiveData對(duì)象。

 
 
 
 
  1. public class MainAbilitySlice extends AbilitySlice {
  2.     private static HiLogLabel mLabel = new HiLogLabel(HiLog.LOG_APP, 00001, "suisui");
  3.     AbilityMainBinding binding;
  4.     @Override
  5.     public void onStart(Intent intent) {
  6.         super.onStart(intent);
  7.         ComponentContainer componentContainer =
  8.                 (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_main, null, false);
  9.         if (!(componentContainer instanceof ComponentContainer)) {
  10.             return;
  11.         }
  12.         super.setUIContent(componentContainer);
  13.         try {
  14.             binding = DataBindingUtil.createBinding(ResourceTable.Layout_ability_main, getContext(), "com.example.time");
  15.         } catch (IllegalArgumentException | IOException exception) {
  16.             HiLog.info(mLabel, exception.toString());
  17.         }
  18.         if (binding != null) {
  19.             binding.initComponent(componentContainer);
  20.             binding.setLifecycle(getLifecycle());
  21.             ActiveData price = new ActiveData<>();
  22.             price.setData("DataBinding Demo");
  23.             MainAbilityModel model = new MainAbilityModel();
  24.             model.setTitile(price);
  25.             binding.setModel(model);
  26.         }
  27.     }
  28. }

需要注意的是在調(diào)用DataBindingUtil.createBinding時(shí),要替換成自己的包名。

至此,我們大致的把DataBinding的簡(jiǎn)單使用梳理了,總體來說DataBinding可以為我們減少代碼量,也不需要再做findComponentById,設(shè)置數(shù)據(jù)等一些繁瑣的操作。但在實(shí)際業(yè)務(wù)開發(fā)當(dāng)中可能也會(huì)有一定的局限性,例如ActiveData的類型轉(zhuǎn)換問題,相信后續(xù)官方也會(huì)越來越完善,HarmonyOS也會(huì)越來越好,讓我們拭目以待。

最終效果

注:DevEco Studio版本過低可能會(huì)導(dǎo)致編譯生成Binding找不到包,可升級(jí)版本再試。

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.


新聞名稱:HarmonyOSDataBinding使用指南
路徑分享:http://www.5511xx.com/article/dpsodoi.html