当前位置: 首页 > news >正文

自由流转--实例(二)

1、使用组件迁移数据

@Entry 
@Component
struct Index {private arr: number[] = [0,1,2,3,4,5,6,7,8,9,10]build() {Column() {// 部分组件支持分布式迁移List({space: 20}) {ForEach(this.arr, (item: number) => {...},(item: number) => (item.toString()))}// 给需要迁移的组件配置restoreId,不同组件不同的值.restoreId(1)}}
}

2、页面栈迁移

onContinue(wantParam: Record<string, Object | undefined>): AbilityConstant.OnContinueResult {...return AbilityConstant.OnContinueResult.AGREE  // 同意迁移
}onCreate(want:Want, launchParam: AbilityConstant.LaunchParam): void {this.checkPermissions()if (launchParam.launchReason === AbilityConstant.LaunchParam.CONTINUATION) {this.context.restoreWindowStage(new LocalStorage));}
}

3、少量状态数据迁移

// 源端:实现源端回调
onContinue(wantParam: {[key:string]: any}) {let sessionId: string = AppStore.get('sessionId') as string;...wantParam['sessionId'] = AppStore.get<string>('sessionId')
}// 对端:恢复数据
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {...if (launchParam.launchReason === AbilityConstant.LaunchReason.CONTINUATION) {AppStorage.setOrCreate<string>('sessionId', want.parameters?.sessionId)this.context.restoreWindowStorage(new LocalStorage())}
}

4、内存数据迁移

        创建分布式数据对象        

private localObject: distributedDataObject.DataObject | undefined = undefined
...
onWindowStageCreate(windowStage: window.windowStage) {...if(!this.localObject) {let mailInfo: MailInfo = new MailInfo(undefined,undefined,undefined,undefined)this.localObject = distributedDataObject.create(this.context, mailInfo)}...
}

         向分布式数据对象写 

onContinue(wantParam: Record<string, Object | undefined>): AbilityConstant.OnContinueResult {try {let sessionId: string = AppStorage.get('sessionId') as stringif (!sessionId) {sessionId = distributedDataObject.getSessionId()AppStorage.setOrCreate('sessionId',sessionId)}if (this.loacalObject) {this.localObject.setSessionId(sessionId)this.localObject['recipient'] = AppStorage.get('recipient')this.localObject['sender'] = AppStorage.get('sender')this.localObject['subject'] = AppStorage.get('subject')this.localObject['emailContent'] = AppStorage.get('emailContent')this.localObject.save(wantParam.targetDevice as string)wantParam.siatributedSessionId = sessionId}}
}

        从分布式数据对象读 

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {if (launchParam.launchReason === AbilityConstant.LaunchReason.CONTINUATION) {if (!this.loacalObject) {let mailInfo: MailInfo = new MailInfo(undefined,undefined,undefined,undefined)this.localObject = distributedDataObject.create(this.context, mailInfo)this.localObject.on('change', this.changeCall)}if (sessionId && this.localObject) {this.localObject.setSessionId(sessionId)AppStorage.setOnCreat('recipient',this.localObject['recipient'])AppStorage.setOnCreat('sender',this.localObject['sender'])AppStorage.setOnCreat('subject',this.localObject['subject'])AppStorage.setOnCreate('emailContent',this.localObject['emailContent'])}this.context.restoreWindowStage(new LocalStorage())}
}

5、使用分布式文件传输数据

@Entry
@Component
struct MailHomePage {aboutToAppear() {if ((this.isContinuation === CommonConstants.CAN_CONTINUATION) && (this.appendix.length >= 1)) {this.readFile();}}build() {// ...}/*** Reading file from distributed file systems.*/readFile(): void {this.appendix.forEach((item: AppendixBean) => {let filePath: string = this.distributedPath + item.fileName;let savePath: string = getContext().filesDir + '/' + item.fileName;try {while (fileIo.accessSync(filePath)) {let saveFile = fileIo.openSync(savePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE);let buf: ArrayBuffer = new ArrayBuffer(CommonConstants.FILE_BUFFER_SIZE);let readSize = 0;let readLen = fileIo.readSync(file.fd, buf, { offset: readSize });while (readLen > 0) {readSize += readLen;fileIo.writeSync(saveFile.fd, buf);readLen = fileIo.readSync(file.fd, buf, { offset: readSize });}fileIo.closeSync(file);fileIo.closeSync(saveFile);}} catch (error) {let err: BusinessError = error as BusinessError;Logger.error(`DocumentViewPicker failed with err: ${JSON.stringify(err)}`);}});}/*** Add appendix from file manager.** @param fileType*/documentSelect(fileType: number): void {try {let DocumentSelectOptions = new picker.DocumentSelectOptions();let documentPicker = new picker.DocumentViewPicker();documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array<string>) => {for (let documentSelectResultElement of DocumentSelectResult) {let buf = new ArrayBuffer(CommonConstants.FILE_BUFFER_SIZE);let readSize = 0;let file = fileIo.openSync(documentSelectResultElement, fileIo.OpenMode.READ_ONLY);let readLen = fileIo.readSync(file.fd, buf, { offset: readSize });// File name is not supported chinese name.let fileName = file.name;if (!fileName.endsWith(imageIndex[fileType].fileType) ||new RegExp("\[\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0]", "gi").test(fileName)) {promptAction.showToast({message: $r('app.string.alert_message_chinese')})return;}let destination = fileIo.openSync(getContext().filesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);let destinationDistribute = fileIo.openSync(getContext().distributedFilesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);while (readLen > 0) {readSize += readLen;fileIo.writeSync(destination.fd, buf);fileIo.writeSync(destinationDistribute.fd, buf);console.info(destinationDistribute.path);readLen = fileIo.readSync(file.fd, buf, { offset: readSize });}fileIo.closeSync(file);fileIo.closeSync(destination);fileIo.closeSync(destinationDistribute);this.appendix.push({ iconIndex: fileType, fileName: fileName });}Logger.info(`DocumentViewPicker.select successfully, DocumentSelectResult uri: ${JSON.stringify(DocumentSelectResult)}`);}).catch((err: BusinessError) => {Logger.error(`DocumentViewPicker.select failed with err: ${JSON.stringify(err)}`);});} catch (error) {let err: BusinessError = error as BusinessError;Logger.error(`DocumentViewPicker failed with err: ${JSON.stringify(err)}`);}}
}


http://www.mrgr.cn/news/27865.html

相关文章:

  • WEB服务器实现(药品商超)
  • stm32教程:OLED屏显示字母、汉字、图片工程讲解
  • 使用 Python 和 OpenCV 实现摄像头人脸检测并截图
  • P1197 星球大战(并查集+逆向思维)
  • java排序算法汇总
  • 搜维尔科技:Haption力触觉交互,虚拟机械装配验证
  • 高级java每日一道面试题-2024年9月12日-安全篇[加密篇]-有哪些加密算法, 加密算法都有哪些分类?
  • Kubernetes Pod的3种重启策略
  • java中init()函数(JAVA基础)
  • NISP 一级 | 5.3 电子邮件安全
  • 【人工智能】AI创业的前沿思考 | 从垂直领域到通用智能模型AGI的崛起
  • uniapp js修改数组某个下标以外的所有值
  • 2020-11-04 求最小与均值输入0结束
  • 代码随想录算法训练营第四十四天| LeetCode322. 零钱兑换、LeetCode279.完全平方数、LeetCode139.单词拆分
  • python画图|同时输出二维和三维图
  • C++——哈希unordered_set/unordered_map的封装
  • 火语言RPA流程组件介绍--下拉框选择
  • 你可能遗漏的一些C#/.NET/.NET Core知识点
  • 高效网络爬虫设计:多线程抓取网页内容
  • AI学习指南深度学习篇-RMSprop算法流程
  • [产品管理-21]:NPDP新产品开发 - 19 - 产品设计与开发工具 - 详细设计与规格定义
  • linux服务器配置及服务器资源命令使用查看
  • UDP_SOCKET编程实现
  • Vue3 Day4-计算、监视属性
  • 松材线虫多光谱数据集
  • InputDispatcher的调试日志isLoggable动态开放logcat实战使用