HarmonyOS鸿蒙Next中卡片使用formBindingData.createFormBindingData(FormData)如何获取

HarmonyOS鸿蒙Next中卡片使用formBindingData.createFormBindingData(FormData)如何获取 在FormAbility的onAddForm中

let FormData: Record<string, string> = {
  'VideoPath': VideoPath,
};
return formBindingData.createFormBindingData(FormData);

其中VideoPath为一个字符串

在WidgetCard.ets中

let localStorage = new LocalStorage();
@Entry(localStorage)
@Component
struct WidgetCard {
  /*
   * The title.
   */
  readonly title: string = 'Hello World';
  /*
   * The action type.
   */
  readonly actionType: string = 'router';
  /*
   * The ability name.
   */
  readonly abilityName: string = 'EntryAbility';
  /*
   * The message.
   */
  readonly message: string = 'add detail';
  /*
   * The width percentage setting.
   */
  readonly fullWidthPercent: string = '100%';
  /*
   * The height percentage setting.
   */
  readonly fullHeightPercent: string = '100%';

  @LocalStorageProp('VideoPath') formData: string = '';
  aboutToAppear(): void {
    LogUtil.info(TAG, `Received VideoPath: ${this.formData}`);

  }

  build() {
    Row() {
      
    }
    .height(this.fullHeightPercent)
    .backgroundColor($r('sys.color.comp_background_primary'))
  }
}

这样使用得到的formData为空我该如何得到这个formData


更多关于HarmonyOS鸿蒙Next中卡片使用formBindingData.createFormBindingData(FormData)如何获取的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

【背景知识】

如下图,元服务卡片提供方可以使用updateForm接口更新卡片,卡片UI代码内通过LocalStorageProp可以获得卡片提供方推送的需要刷新的卡片数据,接收需要刷新的卡片数据时,卡片数据会被转换成string类型。另外卡片框架还会通过开发者声明的定时信息按需通知卡片提供方进行卡片刷新。

formProvider:提供卡片提供方相关的接口能力,可通过该模块提供接口实现更新卡片、设置卡片更新时间、获取卡片信息、请求发布卡片等。 updateForm:卡元服务片提供方调用,更新指定的卡片,使用Promise异步回调,其参数如下:

参数名 类型 必填 说明
formId string 请求更新的卡片标识。
formBindingData formBindingData.FormBindingData 用于更新的数据。

formBindingData:提供卡片数据绑定的能力,包括FormBindingData对象的创建、相关信息的描述。

【解决方案】

可参考以下方法将数据传输到元服务卡片: 导入formBindingData、formProvider等相关模块,通过createFormBindingData创建FormBindingData对象绑定数据,调用formProvider.updateForm更新表单数据,并处理Promise的成功和失败情况,示例代码如下:

private VideoPath:string='VideoPath';
onAddForm(want: Want): formBindingData.FormBindingData {
  let formId: string
  // 卡片使用方创建卡片时触发,提供方需要返回卡片数据绑定类
  let FormData: Record<string, string> = {
    'VideoPath': this.VideoPath,
  };
  if(!want || !want.parameters) {
    hilog.error(0x0000, TAG, `FormAbility onAddForm want or want.parameters is undefined`);
    return formBindingData.createFormBindingData('');
  }
  try {
    formId= want.parameters[formInfo.FormParam.IDENTITY_KEY] as string;
    let  FormData2 = formBindingData.createFormBindingData(FormData)
    formProvider.updateForm(formId, FormData2).then(() => {
      console.log(`formProvider updateForm success`);
    }).catch((error: BusinessError) => {
      console.error(`promise error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message})`);
    });
  } catch (error) {
    console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message})`);
  }
  return formBindingData.createFormBindingData(FormData);

WidgetCard.ets

let localStorage = new LocalStorage();
@Entry(localStorage)
@Component
struct WidgetCard {
  /*
   * The title.
   */
  readonly title: string = 'Hello World';
  /*
   * The action type.
   */
  readonly actionType: string = 'router';
  /*
   * The ability name.
   */
  readonly abilityName: string = 'EntryAbility';
  /*
   * The message.
   */
  readonly message: string = 'add detail';
  /*
   * The width percentage setting.
   */
  readonly fullWidthPercent: string = '100%';
  /*
   * The height percentage setting.
   */
  readonly fullHeightPercent: string = '100%';

  @LocalStorageProp('VideoPath') formData: string = '';
  aboutToAppear(): void {
      
  }

  build() {
    Row() {
      Text(this.formData)
    }
    .height(this.fullHeightPercent)
    .backgroundColor($r('sys.color.comp_background_primary'))
  }
}

更多关于HarmonyOS鸿蒙Next中卡片使用formBindingData.createFormBindingData(FormData)如何获取的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


@LocalStorageProp('键名')中的键名必须与FormBindingData中定义的属性名称完全一致,包括大小写和特殊字符。根据代码示例,FormData中的键为’VideoPath’,而@LocalStorageProp('VideoPath')中键名匹配正确

可以尝试如下方式

//在onAddForm中定义明确的数据对象类型
class FormData {
  VideoPath: string = VideoPath; // 确保VideoPath已正确赋值
}
return formBindingData.createFormBindingData(new FormData());


// 在WidgetCard.ets中,通过@LocalStorageProp绑定数据时,需确保LocalStorage实例与卡片入口关联

let localStorage = new LocalStorage();
@Entry(localStorage)
@Component
struct WidgetCard {
  @LocalStorageProp('VideoPath') videoPath: string = '';
}

在onAddForm方法中,需要将卡片ID与业务数据一起绑定

onAddForm(want: Want) {
  let formId = want.parameters['ohos.extra.param.key.form_identity'];
  let FormData: Record<string, string> = {
    'formId': formId, // 这里要传递的卡片标识
    'VideoPath': VideoPath
  };
  return formBindingData.createFormBindingData(FormData);
}

创建LocalStorage实例时需与卡片上下文关联。要使用context参数初始化:

let localStorage = new LocalStorage(context);

使用@LocalStorageLink代替Prop装饰器实现双向绑定:

[@LocalStorageLink](/user/LocalStorageLink)('VideoPath') formData: string = '';

在form_config.json中注册使用的数据字段:

{
  "data": {
    "VideoPath": {
      "type": "string"
    }
  }
}

在HarmonyOS Next中,使用formBindingData.createFormBindingData(FormData)获取FormBindingData对象。FormData需通过构造方法初始化,例如new formBindingData.FormData(),并调用其方法设置键值对数据。该API返回的FormBindingData对象用于卡片数据绑定,实现动态内容更新。具体数据操作需参照FormData提供的接口方法。

在HarmonyOS Next中,通过formBindingData.createFormBindingData()创建的FormData需要通过@LocalStorageLink@LocalStorageProp正确绑定到卡片组件。从代码看,问题可能出在LocalStorage的初始化和数据传递方式上。

确保在FormAbility的onAddForm中正确创建并返回FormBindingData对象:

let formData = {
  'VideoPath': VideoPath
};
return formBindingData.createFormBindingData(formData);

在WidgetCard中,需要使用@LocalStorageLink而不是@LocalStorageProp来接收动态数据:

[@LocalStorageLink](/user/LocalStorageLink)('VideoPath') videoPath: string = '';

同时检查LocalStorage的初始化是否与卡片实例匹配。在aboutToAppear中访问数据时,确保LocalStorage已完成数据注入。如果问题依旧,验证FormAbility中VideoPath的值是否确实被正确设置。

回到顶部