1/* 2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import router from '@ohos.router'; 17import image from '@ohos.multimedia.image'; 18import Logger from '../../../util/Logger'; 19import testNapi, { testNapiValue } from 'libentry.so'; 20import { TitleBar } from '../../../common/TitleBar' 21import hilog from '@ohos.hilog'; 22 23const TAG: string = 'napi_get_named_property'; 24 25interface myObjVal { 26 key: string 27} 28 29interface myObj { 30 key1: string; 31 key2: number; 32 key3: boolean; 33 key4: Array<string>; 34 key5: Function; 35 key6: myObjVal; 36} 37 38@Entry 39@Component 40struct napiGetPropertyNames { 41 private btnFontColor: Resource = $r('app.color.white'); 42 private pixelMapFormat: image.PixelMapFormat = 3; 43 private textcont: Resource = $r('app.string.napi_get_named_property_description'); 44 @State isSetInstance: Boolean = false; 45 @State imagePixelMap: PixelMap | undefined = undefined; 46 @State testcont: string = ' // 测试 N-API napi_get_named_property \n' 47 + 'let obj = {key1: "value",key2: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' 48 + ' const myData = testNapi.testNapiGetNamedProperty(obj, "key1"); \n' 49 + ' // 使用获取的自定义数据 \n' 50 + ' console.log(myData); // 输出自定义数据 \n' 51 + ' const myData2 = testNapi.testNapiGetNamedProperty(obj, "key2"); \n' 52 + ' console.log(myData2); // 输出自定义数据 \n' 53 + ' const myData3 = testNapi.testNapiGetNamedProperty(obj, "key3"); \n' 54 + ' console.log(myData3); // 输出自定义数据 \n' 55 + ' const myData4 = testNapi.testNapiGetNamedProperty(obj, "key4"); \n' 56 + ' console.log(myData4); // 输出自定义数据 \n' 57 + ' const myData5 = testNapi.testNapiGetNamedProperty(obj, "key5"); \n' 58 + ' console.log(myData5); // 输出自定义数据 \n' 59 + ' const myData6 = testNapi.testNapiGetNamedProperty(obj, "key6"); \n' 60 + ' console.log(myData6); // 输出自定义数据 \n'; 61 62 controller: TextAreaController = new TextAreaController() 63 64 build() { 65 Column() { 66 // 标题 67 TitleBar({ title: $r('app.string.napi_get_named_property') }) 68 69 Scroll() { 70 Column() { 71 Column() { 72 TextArea({ 73 text: this.textcont, 74 placeholder: '', 75 }) 76 .placeholderFont({ size: 16, weight: 400 }) 77 .width('90%') 78 .margin(10) 79 .fontSize(16) 80 .fontColor($r('app.color.sub_title_color')) 81 .backgroundColor($r('app.color.white')) 82 .enabled(false) 83 84 TextArea({ 85 text: this.testcont, 86 placeholder: '', 87 }) 88 .placeholderFont({ size: 16, weight: 400 }) 89 .width('90%') 90 .margin(10) 91 .fontSize(16) 92 .fontColor($r('app.color.textarea_font_color')) 93 .backgroundColor($r('app.color.textarea_background_color')) 94 .enabled(false) 95 } 96 .width('100%') 97 .alignItems(HorizontalAlign.Center) 98 .justifyContent(FlexAlign.Start) 99 100 Row() { 101 102 Button($r('app.string.napi_get_named_property'), { type: ButtonType.Capsule }) 103 .backgroundColor(Color.Blue) 104 .width('80%') 105 .height(48) 106 .fontSize(16) 107 .fontWeight(500) 108 .fontColor(this.btnFontColor) 109 .margin({ left: 24 }) 110 .id('napi_get_named_property') 111 .onClick(() => { 112 let obj: myObj = { 113 key1: "value", 114 key2: 1, 115 key3: false, 116 key4: ["a", "b", "c"], 117 key5: () => { 118 return "function"; 119 }, 120 key6: { key: "value" } 121 } 122 let ret = testNapi.testNapiGetNamedProperty(obj, "key1"); 123 this.testcont = this.testcont.replace('log(myData)', 'log(## ' + ret + ' ##)'); 124 let ret2 = testNapi.testNapiGetNamedProperty(obj, "key2"); 125 this.testcont = this.testcont.replace('log(myData2)', 'log(## ' + ret2 + ' ##)'); 126 let ret3 = testNapi.testNapiGetNamedProperty(obj, "key3"); 127 this.testcont = this.testcont.replace('log(myData3)', 'log(## ' + ret3 + ' ##)'); 128 let ret4 = testNapi.testNapiGetNamedProperty(obj, "key4"); 129 this.testcont = this.testcont.replace('log(myData4)', 'log(## ' + ret4 + ' ##)'); 130 let ret5 = testNapi.testNapiGetNamedProperty(obj, "key5"); 131 this.testcont = this.testcont.replace('log(myData5)', 'log(## ' + ret5 + ' ##)'); 132 let ret6 = testNapi.testNapiGetNamedProperty(obj, "key6"); 133 this.testcont = this.testcont.replace('log(myData6)', 'log(## ' + JSON.stringify(ret6) + ' ##)'); 134 }) 135 } 136 .width('100%') 137 .height(48) 138 .alignItems(VerticalAlign.Center) 139 .justifyContent(FlexAlign.SpaceBetween) 140 } 141 .width('100%') 142 } 143 } 144 .height('100%') 145 .width('100%') 146 .backgroundColor($r('app.color.background_shallow_grey')) 147 } 148} 149