deveco-harness 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +71 -0
- package/README.md +71 -0
- package/deveco-harness.example.jsonc +32 -0
- package/dist/index.js +758 -0
- package/package.json +48 -0
- package/prompts/build.txt +164 -0
- package/prompts/plan.txt +118 -0
- package/skills/arkts-error-fixes/README.md +151 -0
- package/skills/arkts-error-fixes/SKILL.md +150 -0
- package/skills/arkts-error-fixes/assets/AnyTypeError.ets +269 -0
- package/skills/arkts-error-fixes/assets/AppStorageError.ets +22 -0
- package/skills/arkts-error-fixes/assets/ArrowFunctionConversionError.ets +42 -0
- package/skills/arkts-error-fixes/assets/AvoidAreaTypeError.ets +43 -0
- package/skills/arkts-error-fixes/assets/BreakpointTypeError.ets +71 -0
- package/skills/arkts-error-fixes/assets/CatchClauseTypeError.ets +54 -0
- package/skills/arkts-error-fixes/assets/ColorConsistencyError.ets +88 -0
- package/skills/arkts-error-fixes/assets/ColorPropertyError.ets +39 -0
- package/skills/arkts-error-fixes/assets/ContextTypeError.ets +105 -0
- package/skills/arkts-error-fixes/assets/DecoratorStateError.ets +64 -0
- package/skills/arkts-error-fixes/assets/DisplayListenerTypeError.ets +129 -0
- package/skills/arkts-error-fixes/assets/DuplicateEntryError.ets +35 -0
- package/skills/arkts-error-fixes/assets/ESObjectTypeError.ets +137 -0
- package/skills/arkts-error-fixes/assets/FontColorPropertyError.ets +42 -0
- package/skills/arkts-error-fixes/assets/FunctionReturnTypeError.ets +41 -0
- package/skills/arkts-error-fixes/assets/IDataSourceError.ets +79 -0
- package/skills/arkts-error-fixes/assets/ImplementationNotAllowedError.ets +38 -0
- package/skills/arkts-error-fixes/assets/InterfaceMethodSignatureError.ets +43 -0
- package/skills/arkts-error-fixes/assets/NotificationError.ets +34 -0
- package/skills/arkts-error-fixes/assets/ObjectLiteralInterfaceError.ets +49 -0
- package/skills/arkts-error-fixes/assets/ObjectLiteralTypeError.ets +54 -0
- package/skills/arkts-error-fixes/assets/ObjectSpreadError.ets +29 -0
- package/skills/arkts-error-fixes/assets/PossiblyNullError.ets +32 -0
- package/skills/arkts-error-fixes/assets/ResourceConversionError.ets +159 -0
- package/skills/arkts-error-fixes/assets/StandaloneFunctionContext.ets +116 -0
- package/skills/arkts-error-fixes/assets/StandaloneFunctionError.ets +85 -0
- package/skills/arkts-error-fixes/assets/StorageLinkDefaultError.ets +41 -0
- package/skills/arkts-error-fixes/assets/TitleButtonRectTypeError.ets +86 -0
- package/skills/arkts-error-fixes/assets/UnusedVariableWarning.ets +102 -0
- package/skills/arkts-error-fixes/assets/UtilityTypeError.ets +176 -0
- package/skills/arkts-error-fixes/assets/WindowRectSizeError.ets +65 -0
- package/skills/arkts-error-fixes/assets/WindowTypeError.ets +37 -0
- package/skills/arkts-error-fixes/reference/any_type_errors.md +433 -0
- package/skills/arkts-error-fixes/reference/appstorage_errors.md +95 -0
- package/skills/arkts-error-fixes/reference/arrow_function_conversion_errors.md +163 -0
- package/skills/arkts-error-fixes/reference/avoid_area_type_errors.md +59 -0
- package/skills/arkts-error-fixes/reference/breakpoint_type_errors.md +122 -0
- package/skills/arkts-error-fixes/reference/catch_clause_type_errors.md +89 -0
- package/skills/arkts-error-fixes/reference/color_consistency_errors.md +209 -0
- package/skills/arkts-error-fixes/reference/color_property_errors.md +136 -0
- package/skills/arkts-error-fixes/reference/context_type_errors.md +260 -0
- package/skills/arkts-error-fixes/reference/decorator_state_errors.md +109 -0
- package/skills/arkts-error-fixes/reference/display_listener_type_errors.md +149 -0
- package/skills/arkts-error-fixes/reference/duplicate_entry_errors.md +193 -0
- package/skills/arkts-error-fixes/reference/esobject_type_errors.md +110 -0
- package/skills/arkts-error-fixes/reference/fontcolor_property_errors.md +104 -0
- package/skills/arkts-error-fixes/reference/function_return_type_errors.md +194 -0
- package/skills/arkts-error-fixes/reference/idata_source_errors.md +77 -0
- package/skills/arkts-error-fixes/reference/implementation_not_allowed_errors.md +191 -0
- package/skills/arkts-error-fixes/reference/interface_method_signature_errors.md +143 -0
- package/skills/arkts-error-fixes/reference/notification_errors.md +110 -0
- package/skills/arkts-error-fixes/reference/object_literal_interface_errors.md +156 -0
- package/skills/arkts-error-fixes/reference/object_literal_type_errors.md +110 -0
- package/skills/arkts-error-fixes/reference/object_spread_errors.md +105 -0
- package/skills/arkts-error-fixes/reference/possibly_null_errors.md +156 -0
- package/skills/arkts-error-fixes/reference/resource_conversion_errors.md +154 -0
- package/skills/arkts-error-fixes/reference/standalone_function_errors.md +68 -0
- package/skills/arkts-error-fixes/reference/storage_link_default_errors.md +124 -0
- package/skills/arkts-error-fixes/reference/title_button_rect_type_errors.md +167 -0
- package/skills/arkts-error-fixes/reference/unused_variable_warnings.md +295 -0
- package/skills/arkts-error-fixes/reference/utility_type_errors.md +290 -0
- package/skills/arkts-error-fixes/reference/window_rect_size_errors.md +70 -0
- package/skills/arkts-error-fixes/reference/window_type_errors.md +95 -0
- package/skills/arkts-grammar-standards/SKILL.md +71 -0
- package/skills/arkts-grammar-standards/references/basic-syntax.md +103 -0
- package/skills/arkts-grammar-standards/references/restrictions.md +100 -0
- package/skills/arkts-grammar-standards/references/topic-aliases.json +93 -0
- package/skills/arkts-grammar-standards/references/ts-diff.md +108 -0
- package/skills/arkts-runtime-fix/SKILL.md +152 -0
- package/skills/arkts-runtime-fix/SKILL_CN.md +123 -0
- package/skills/arkts-runtime-fix/evals/evals.json +17 -0
- package/skills/arkts-runtime-fix/scripts/collect-hilog.mjs +116 -0
- package/skills/arkts-runtime-fix/scripts/collect-hilog.ts +115 -0
- package/skills/arkts-runtime-fix/scripts/fetch-faultlog.mjs +117 -0
- package/skills/arkts-runtime-fix/scripts/fetch-faultlog.ts +116 -0
- package/skills/arkts-runtime-fix/scripts/jscrash-report.mjs +149 -0
- package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.mjs +126 -0
- package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.ts +126 -0
- package/skills/arkts-runtime-fix/scripts/probe-faultlogger.mjs +160 -0
- package/skills/arkts-runtime-fix/scripts/probe-faultlogger.ts +160 -0
- package/skills/arkts-runtime-fix/scripts/shared/hdc.mjs +107 -0
- package/skills/arkts-runtime-fix/scripts/shared/hdc.ts +58 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.mjs +143 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.ts +148 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.mjs +389 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.ts +409 -0
- package/skills/arkts-runtime-fix/scripts/shared/utils.mjs +27 -0
- package/skills/arkts-runtime-fix/scripts/shared/utils.ts +27 -0
- package/skills/arkui-knowledge/SKILL.md +90 -0
- package/skills/arkui-knowledge/references/api-guardrails.md +60 -0
- package/skills/arkui-knowledge/references/common-mistakes.md +119 -0
- package/skills/arkui-knowledge/references/component-cookbook.md +135 -0
- package/skills/arkui-knowledge/references/ui-quality-checklist.md +36 -0
- package/skills/deveco-create-project/SKILL.md +144 -0
- package/skills/deveco-create-project/application/AppScope/app.json5 +10 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/element/string.json +8 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/media/background.png +0 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/media/foreground.png +0 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/media/layered_image.json +7 -0
- package/skills/deveco-create-project/application/build-profile.json5 +42 -0
- package/skills/deveco-create-project/application/code-linter.json5 +32 -0
- package/skills/deveco-create-project/application/entry/build-profile.json5 +33 -0
- package/skills/deveco-create-project/application/entry/hvigorfile.ts +7 -0
- package/skills/deveco-create-project/application/entry/obfuscation-rules.txt +23 -0
- package/skills/deveco-create-project/application/entry/oh-package.json5 +10 -0
- package/skills/deveco-create-project/application/entry/src/main/ets/entryability/EntryAbility.ets +63 -0
- package/skills/deveco-create-project/application/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +31 -0
- package/skills/deveco-create-project/application/entry/src/main/ets/pages/Index.ets +38 -0
- package/skills/deveco-create-project/application/entry/src/main/module.json5 +50 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/element/color.json +8 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/element/float.json +8 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/element/string.json +16 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/background.png +0 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/foreground.png +0 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/layered_image.json +7 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/startIcon.png +0 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/backup_config.json +3 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/main_pages.json +5 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/dark/element/color.json +8 -0
- package/skills/deveco-create-project/application/hvigor/hvigor-config.json5 +23 -0
- package/skills/deveco-create-project/application/hvigorfile.ts +7 -0
- package/skills/deveco-create-project/application/oh-package.json5 +10 -0
- package/skills/deveco-create-project/scripts/copy-template.mjs +216 -0
- package/skills/deveco-create-project/scripts/copy-template.ts +214 -0
- package/skills/deveco-create-project/scripts/detect-sdk.mjs +82 -0
- package/skills/deveco-create-project/scripts/detect-sdk.ts +95 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { window } from '@kit.ArkUI';
|
|
2
|
+
import { common } from '@kit.AbilityKit';
|
|
3
|
+
|
|
4
|
+
@Entry
|
|
5
|
+
@Component
|
|
6
|
+
struct ContextTypeErrorExample {
|
|
7
|
+
@State windowWidth: number = 0;
|
|
8
|
+
@State windowHeight: number = 0;
|
|
9
|
+
|
|
10
|
+
async aboutToAppear() {
|
|
11
|
+
// ✅ 正确:使用 getUIContext().getHostContext() 并进行类型检查
|
|
12
|
+
const context = this.getUIContext().getHostContext();
|
|
13
|
+
if (context) {
|
|
14
|
+
await this.getWindowSize(context as common.UIAbilityContext);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private async getWindowSize(context: common.UIAbilityContext) {
|
|
19
|
+
try {
|
|
20
|
+
const win = await window.getLastWindow(context);
|
|
21
|
+
const properties = win.getWindowProperties();
|
|
22
|
+
this.windowWidth = properties.windowRect.width;
|
|
23
|
+
this.windowHeight = properties.windowRect.height;
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error('获取窗口大小失败:', err);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
build() {
|
|
30
|
+
Column() {
|
|
31
|
+
Text('Context 类型错误示例')
|
|
32
|
+
.fontSize(20)
|
|
33
|
+
.fontWeight(FontWeight.Bold)
|
|
34
|
+
.margin({ bottom: 20 })
|
|
35
|
+
|
|
36
|
+
Column({ space: 12 }) {
|
|
37
|
+
Text('❌ 错误示例 1: 直接使用可能为 undefined 的 Context')
|
|
38
|
+
.fontSize(14)
|
|
39
|
+
.fontColor('#666666')
|
|
40
|
+
|
|
41
|
+
Text('const context = this.getUIContext().getHostContext();')
|
|
42
|
+
.fontSize(12)
|
|
43
|
+
.fontColor('#FF5722')
|
|
44
|
+
.fontFamily('monospace')
|
|
45
|
+
.padding(8)
|
|
46
|
+
.backgroundColor('#FFF3E0')
|
|
47
|
+
.borderRadius(4)
|
|
48
|
+
|
|
49
|
+
Text('await window.getLastWindow(context); // 类型错误')
|
|
50
|
+
.fontSize(12)
|
|
51
|
+
.fontColor('#FF5722')
|
|
52
|
+
.fontFamily('monospace')
|
|
53
|
+
.padding(8)
|
|
54
|
+
.backgroundColor('#FFF3E0')
|
|
55
|
+
.borderRadius(4)
|
|
56
|
+
|
|
57
|
+
Text('✅ 正确示例 1: 添加 null 检查')
|
|
58
|
+
.fontSize(14)
|
|
59
|
+
.fontColor('#666666')
|
|
60
|
+
|
|
61
|
+
Text('const context = this.getUIContext().getHostContext();')
|
|
62
|
+
.fontSize(12)
|
|
63
|
+
.fontColor('#4CAF50')
|
|
64
|
+
.fontFamily('monospace')
|
|
65
|
+
.padding(8)
|
|
66
|
+
.backgroundColor('#E8F5E9')
|
|
67
|
+
.borderRadius(4)
|
|
68
|
+
|
|
69
|
+
Text('if (context) {')
|
|
70
|
+
.fontSize(12)
|
|
71
|
+
.fontColor('#4CAF50')
|
|
72
|
+
.fontFamily('monospace')
|
|
73
|
+
.padding(8)
|
|
74
|
+
.backgroundColor('#E8F5E9')
|
|
75
|
+
.borderRadius(4)
|
|
76
|
+
|
|
77
|
+
Text(' await window.getLastWindow(context as common.UIAbilityContext);')
|
|
78
|
+
.fontSize(12)
|
|
79
|
+
.fontColor('#4CAF50')
|
|
80
|
+
.fontFamily('monospace')
|
|
81
|
+
.padding(8)
|
|
82
|
+
.backgroundColor('#E8F5E9')
|
|
83
|
+
.borderRadius(4)
|
|
84
|
+
|
|
85
|
+
Text('}')
|
|
86
|
+
.fontSize(12)
|
|
87
|
+
.fontColor('#4CAF50')
|
|
88
|
+
.fontFamily('monospace')
|
|
89
|
+
.padding(8)
|
|
90
|
+
.backgroundColor('#E8F5E9')
|
|
91
|
+
.borderRadius(4)
|
|
92
|
+
}
|
|
93
|
+
.alignItems(HorizontalAlign.Start)
|
|
94
|
+
.margin({ bottom: 20 })
|
|
95
|
+
|
|
96
|
+
Text(`窗口大小: ${this.windowWidth} x ${this.windowHeight}`)
|
|
97
|
+
.fontSize(16)
|
|
98
|
+
.fontColor('#333333')
|
|
99
|
+
}
|
|
100
|
+
.width('100%')
|
|
101
|
+
.height('100%')
|
|
102
|
+
.padding(20)
|
|
103
|
+
.alignItems(HorizontalAlign.Center)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { window } from '@kit.ArkUI';
|
|
2
|
+
import { common } from '@kit.AbilityKit';
|
|
3
|
+
|
|
4
|
+
class WindowStateManager {
|
|
5
|
+
widthBreakpoint: string = 'sm';
|
|
6
|
+
isFolded: boolean = true;
|
|
7
|
+
|
|
8
|
+
updateState(width: number, height: number) {
|
|
9
|
+
const area = width * height;
|
|
10
|
+
if (area < 600 * 600) {
|
|
11
|
+
this.widthBreakpoint = 'sm';
|
|
12
|
+
this.isFolded = true;
|
|
13
|
+
} else if (area < 840 * 600) {
|
|
14
|
+
this.widthBreakpoint = 'md';
|
|
15
|
+
this.isFolded = false;
|
|
16
|
+
} else {
|
|
17
|
+
this.widthBreakpoint = 'lg';
|
|
18
|
+
this.isFolded = false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Component
|
|
24
|
+
struct DecoratorStateExample {
|
|
25
|
+
@State currentBreakpoint: string = 'sm';
|
|
26
|
+
@State isFolded: boolean = true;
|
|
27
|
+
|
|
28
|
+
aboutToAppear() {
|
|
29
|
+
try {
|
|
30
|
+
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
|
|
31
|
+
window.getLastWindow(context, (err, win) => {
|
|
32
|
+
if (err.code !== 0) {
|
|
33
|
+
console.error('Failed to get window:', err);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const properties = win.getWindowProperties();
|
|
37
|
+
this.updateState(properties.windowRect.width, properties.windowRect.height);
|
|
38
|
+
});
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.error('Failed to get window:', err);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private updateState(width: number, height: number) {
|
|
45
|
+
const area = width * height;
|
|
46
|
+
if (area < 600 * 600) {
|
|
47
|
+
this.currentBreakpoint = 'sm';
|
|
48
|
+
this.isFolded = true;
|
|
49
|
+
} else if (area < 840 * 600) {
|
|
50
|
+
this.currentBreakpoint = 'md';
|
|
51
|
+
this.isFolded = false;
|
|
52
|
+
} else {
|
|
53
|
+
this.currentBreakpoint = 'lg';
|
|
54
|
+
this.isFolded = false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
build() {
|
|
59
|
+
Column() {
|
|
60
|
+
Text(`Breakpoint: ${this.currentBreakpoint}`)
|
|
61
|
+
Text(`Folded: ${this.isFolded ? 'Yes' : 'No'}`)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { display } from '@kit.ArkUI';
|
|
2
|
+
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ❌ 错误示例:在 Display 对象上调用 on/off 方法
|
|
6
|
+
*
|
|
7
|
+
* 错误原因:display.on() 和 display.off() 是 display 模块的方法,
|
|
8
|
+
* 不是 Display 对象实例的方法
|
|
9
|
+
*/
|
|
10
|
+
@Entry
|
|
11
|
+
@Component
|
|
12
|
+
struct DisplayListenerTypeError {
|
|
13
|
+
@State displayId: number = 0;
|
|
14
|
+
private displayListener?: number;
|
|
15
|
+
|
|
16
|
+
aboutToAppear() {
|
|
17
|
+
try {
|
|
18
|
+
const displayClass = display.getDefaultDisplaySync();
|
|
19
|
+
this.displayId = displayClass.id;
|
|
20
|
+
|
|
21
|
+
// ❌ 错误:在 Display 对象上调用 on 方法
|
|
22
|
+
// Type 'void' is not assignable to type 'number'
|
|
23
|
+
this.displayListener = displayClass.on('change', () => {
|
|
24
|
+
hilog.info(0x0000, 'DisplayError', 'Display changed');
|
|
25
|
+
});
|
|
26
|
+
} catch (error) {
|
|
27
|
+
hilog.error(0x0000, 'DisplayError', `Failed to register listener: ${JSON.stringify(error)}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
aboutToDisappear() {
|
|
32
|
+
// ❌ 错误:在 Display 对象上调用 off 方法
|
|
33
|
+
// Argument of type 'number' is not assignable to parameter of type 'Callback<Rect>'
|
|
34
|
+
if (this.displayListener !== undefined) {
|
|
35
|
+
const displayClass = display.getDefaultDisplaySync();
|
|
36
|
+
displayClass.off('change', this.displayListener);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
build() {
|
|
41
|
+
Column() {
|
|
42
|
+
Text(`Display ID: ${this.displayId}`)
|
|
43
|
+
.fontSize(20)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* ✅ 正确示例:在 display 模块上调用 on/off 方法
|
|
50
|
+
*/
|
|
51
|
+
@Entry
|
|
52
|
+
@Component
|
|
53
|
+
struct DisplayListenerCorrect {
|
|
54
|
+
@State displayId: number = 0;
|
|
55
|
+
private displayListener?: (data: number) => void;
|
|
56
|
+
|
|
57
|
+
aboutToAppear() {
|
|
58
|
+
try {
|
|
59
|
+
const displayClass = display.getDefaultDisplaySync();
|
|
60
|
+
this.displayId = displayClass.id;
|
|
61
|
+
|
|
62
|
+
// ✅ 正确:在 display 模块上调用 on 方法
|
|
63
|
+
this.displayListener = (data: number) => {
|
|
64
|
+
hilog.info(0x0000, 'DisplayCorrect', `Display changed, ID: ${data}`);
|
|
65
|
+
};
|
|
66
|
+
display.on('change', this.displayListener);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
hilog.error(0x0000, 'DisplayCorrect', `Failed to register listener: ${JSON.stringify(error)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
aboutToDisappear() {
|
|
73
|
+
// ✅ 正确:在 display 模块上调用 off 方法
|
|
74
|
+
if (this.displayListener) {
|
|
75
|
+
display.off('change', this.displayListener);
|
|
76
|
+
this.displayListener = undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
build() {
|
|
81
|
+
Column() {
|
|
82
|
+
Text(`Display ID: ${this.displayId}`)
|
|
83
|
+
.fontSize(20)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* ✅ 正确示例:监听 Display 对象的 availableAreaChange
|
|
90
|
+
*/
|
|
91
|
+
@Entry
|
|
92
|
+
@Component
|
|
93
|
+
struct DisplayAvailableAreaCorrect {
|
|
94
|
+
@State availableArea: string = 'Unknown';
|
|
95
|
+
private areaListener?: (data: display.Rect) => void;
|
|
96
|
+
|
|
97
|
+
aboutToAppear() {
|
|
98
|
+
try {
|
|
99
|
+
const displayClass = display.getDefaultDisplaySync();
|
|
100
|
+
this.availableArea = `Width: ${displayClass.width}, Height: ${displayClass.height}`;
|
|
101
|
+
|
|
102
|
+
// ✅ 正确:在 Display 对象上调用 on 方法监听 availableAreaChange
|
|
103
|
+
this.areaListener = (data: display.Rect) => {
|
|
104
|
+
hilog.info(0x0000, 'DisplayArea', `Available area changed: ${JSON.stringify(data)}`);
|
|
105
|
+
this.availableArea = `Width: ${data.width}, Height: ${data.height}`;
|
|
106
|
+
};
|
|
107
|
+
displayClass.on('availableAreaChange', this.areaListener);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
hilog.error(0x0000, 'DisplayArea', `Failed to register listener: ${JSON.stringify(error)}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
aboutToDisappear() {
|
|
114
|
+
// ✅ 正确:在 Display 对象上调用 off 方法注销 availableAreaChange
|
|
115
|
+
if (this.areaListener) {
|
|
116
|
+
const displayClass = display.getDefaultDisplaySync();
|
|
117
|
+
displayClass.off('availableAreaChange', this.areaListener);
|
|
118
|
+
this.areaListener = undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
build() {
|
|
123
|
+
Column() {
|
|
124
|
+
Text(`Available Area: ${this.availableArea}`)
|
|
125
|
+
.fontSize(20)
|
|
126
|
+
.margin({ top: 10 })
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
@Entry
|
|
2
|
+
@Component
|
|
3
|
+
struct DuplicateEntryError {
|
|
4
|
+
@State message: string = 'Hello World';
|
|
5
|
+
|
|
6
|
+
build() {
|
|
7
|
+
Column() {
|
|
8
|
+
Text(this.message)
|
|
9
|
+
.fontSize(24)
|
|
10
|
+
.margin({ bottom: 20 })
|
|
11
|
+
|
|
12
|
+
ChildComponent()
|
|
13
|
+
}
|
|
14
|
+
.width('100%')
|
|
15
|
+
.height('100%')
|
|
16
|
+
.padding(20)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Component
|
|
21
|
+
struct ChildComponent {
|
|
22
|
+
@State count: number = 0;
|
|
23
|
+
|
|
24
|
+
build() {
|
|
25
|
+
Column() {
|
|
26
|
+
Text(`Count: ${this.count}`)
|
|
27
|
+
.fontSize(18)
|
|
28
|
+
|
|
29
|
+
Button('Increment')
|
|
30
|
+
.onClick(() => {
|
|
31
|
+
this.count++;
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
2
|
+
|
|
3
|
+
const DOMAIN = 0xFF00;
|
|
4
|
+
const TAG = 'ESObjectTypeError';
|
|
5
|
+
|
|
6
|
+
@Entry
|
|
7
|
+
@Component
|
|
8
|
+
struct ESObjectTypeError {
|
|
9
|
+
@State moduleLoaded: boolean = false;
|
|
10
|
+
@State moduleName: string = '';
|
|
11
|
+
@State errorMessage: string = '';
|
|
12
|
+
|
|
13
|
+
async loadNFCModule() {
|
|
14
|
+
try {
|
|
15
|
+
await import('@kit.ConnectivityKit');
|
|
16
|
+
this.moduleLoaded = true;
|
|
17
|
+
this.moduleName = 'NFC';
|
|
18
|
+
this.errorMessage = '';
|
|
19
|
+
hilog.info(DOMAIN, TAG, 'NFC module imported successfully');
|
|
20
|
+
} catch (err) {
|
|
21
|
+
hilog.error(DOMAIN, TAG, 'Failed to import NFC module: %{public}s', JSON.stringify(err));
|
|
22
|
+
this.moduleLoaded = false;
|
|
23
|
+
this.moduleName = 'NFC';
|
|
24
|
+
this.errorMessage = '模块导入失败';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async loadCameraModule() {
|
|
29
|
+
try {
|
|
30
|
+
await import('@kit.CameraKit');
|
|
31
|
+
this.moduleLoaded = true;
|
|
32
|
+
this.moduleName = 'Camera';
|
|
33
|
+
this.errorMessage = '';
|
|
34
|
+
hilog.info(DOMAIN, TAG, 'Camera module imported successfully');
|
|
35
|
+
} catch (err) {
|
|
36
|
+
hilog.error(DOMAIN, TAG, 'Failed to import Camera module: %{public}s', JSON.stringify(err));
|
|
37
|
+
this.moduleLoaded = false;
|
|
38
|
+
this.moduleName = 'Camera';
|
|
39
|
+
this.errorMessage = '模块导入失败';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
build() {
|
|
44
|
+
Column() {
|
|
45
|
+
Text('ESObject 类型限制错误示例')
|
|
46
|
+
.fontSize(20)
|
|
47
|
+
.fontWeight(FontWeight.Bold)
|
|
48
|
+
.margin({ top: 20, bottom: 20 })
|
|
49
|
+
|
|
50
|
+
Text('错误写法(不推荐):')
|
|
51
|
+
.fontSize(14)
|
|
52
|
+
.fontWeight(FontWeight.Bold)
|
|
53
|
+
.margin({ bottom: 10 })
|
|
54
|
+
|
|
55
|
+
Text(`let nfcModule: ESObject = null;`)
|
|
56
|
+
.fontSize(12)
|
|
57
|
+
.fontColor('#FF0000')
|
|
58
|
+
.fontFamily('monospace')
|
|
59
|
+
.padding(10)
|
|
60
|
+
.backgroundColor('#FFF0F0')
|
|
61
|
+
.borderRadius(4)
|
|
62
|
+
.margin({ bottom: 20 })
|
|
63
|
+
|
|
64
|
+
Text('正确写法(推荐):')
|
|
65
|
+
.fontSize(14)
|
|
66
|
+
.fontWeight(FontWeight.Bold)
|
|
67
|
+
.margin({ bottom: 10 })
|
|
68
|
+
|
|
69
|
+
Text(`let nfcModule: ESModule | null = null;`)
|
|
70
|
+
.fontSize(12)
|
|
71
|
+
.fontColor('#00AA00')
|
|
72
|
+
.fontFamily('monospace')
|
|
73
|
+
.padding(10)
|
|
74
|
+
.backgroundColor('#F0FFF0')
|
|
75
|
+
.borderRadius(4)
|
|
76
|
+
.margin({ bottom: 20 })
|
|
77
|
+
|
|
78
|
+
Row() {
|
|
79
|
+
Button('加载 NFC 模块')
|
|
80
|
+
.onClick(() => {
|
|
81
|
+
this.loadNFCModule();
|
|
82
|
+
})
|
|
83
|
+
.margin({ right: 10 })
|
|
84
|
+
|
|
85
|
+
Button('加载 Camera 模块')
|
|
86
|
+
.onClick(() => {
|
|
87
|
+
this.loadCameraModule();
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
.margin({ bottom: 20 })
|
|
91
|
+
|
|
92
|
+
if (this.moduleName) {
|
|
93
|
+
Column() {
|
|
94
|
+
Text(`模块名称: ${this.moduleName}`)
|
|
95
|
+
.fontSize(14)
|
|
96
|
+
.margin({ bottom: 5 })
|
|
97
|
+
|
|
98
|
+
if (this.moduleLoaded) {
|
|
99
|
+
Text('状态: 加载成功 ✓')
|
|
100
|
+
.fontSize(14)
|
|
101
|
+
.fontColor('#00AA00')
|
|
102
|
+
} else {
|
|
103
|
+
Text(`状态: ${this.errorMessage} ✗`)
|
|
104
|
+
.fontSize(14)
|
|
105
|
+
.fontColor('#FF0000')
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
.width('100%')
|
|
109
|
+
.padding(15)
|
|
110
|
+
.backgroundColor('#F5F5F5')
|
|
111
|
+
.borderRadius(8)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
Text('说明:')
|
|
115
|
+
.fontSize(14)
|
|
116
|
+
.fontWeight(FontWeight.Bold)
|
|
117
|
+
.margin({ top: 20, bottom: 10 })
|
|
118
|
+
|
|
119
|
+
Text('• ESObject 类型使用受限,不能直接用作变量类型')
|
|
120
|
+
.fontSize(12)
|
|
121
|
+
.fontColor('#666666')
|
|
122
|
+
.margin({ bottom: 5 })
|
|
123
|
+
|
|
124
|
+
Text('• 使用 ESModule 类型或具体类型来声明动态导入的模块')
|
|
125
|
+
.fontSize(12)
|
|
126
|
+
.fontColor('#666666')
|
|
127
|
+
.margin({ bottom: 5 })
|
|
128
|
+
|
|
129
|
+
Text('• 使用 import() 函数进行动态导入')
|
|
130
|
+
.fontSize(12)
|
|
131
|
+
.fontColor('#666666')
|
|
132
|
+
}
|
|
133
|
+
.width('100%')
|
|
134
|
+
.height('100%')
|
|
135
|
+
.padding(20)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
@Entry
|
|
2
|
+
@Component
|
|
3
|
+
struct FontColorPropertyError {
|
|
4
|
+
@State message: string = '点击按钮测试';
|
|
5
|
+
@State showToast: boolean = false;
|
|
6
|
+
|
|
7
|
+
build() {
|
|
8
|
+
Column() {
|
|
9
|
+
Text('fontColor 属性错误示例')
|
|
10
|
+
.fontSize(20)
|
|
11
|
+
.fontWeight(FontWeight.Bold)
|
|
12
|
+
.margin({ bottom: 16 })
|
|
13
|
+
|
|
14
|
+
Text('正确:Text 组件使用 fontColor')
|
|
15
|
+
.fontSize(16)
|
|
16
|
+
.fontColor('#333333')
|
|
17
|
+
.margin({ bottom: 8 })
|
|
18
|
+
|
|
19
|
+
Text('正确:使用 fontColor 替代 textColor')
|
|
20
|
+
.fontSize(16)
|
|
21
|
+
.fontColor('#666666')
|
|
22
|
+
.margin({ bottom: 8 })
|
|
23
|
+
|
|
24
|
+
Button('点击提示')
|
|
25
|
+
.onClick(() => {
|
|
26
|
+
this.showToast = true;
|
|
27
|
+
setTimeout(() => {
|
|
28
|
+
this.showToast = false;
|
|
29
|
+
}, 2000);
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
if (this.showToast) {
|
|
33
|
+
Text(this.message)
|
|
34
|
+
.fontSize(14)
|
|
35
|
+
.fontColor('#666666')
|
|
36
|
+
.margin({ top: 8 })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
.width('100%')
|
|
40
|
+
.padding(16)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
interface WindowInfo {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
@Entry
|
|
7
|
+
@Component
|
|
8
|
+
struct FunctionReturnTypeError {
|
|
9
|
+
@State windowWidth: number = 0;
|
|
10
|
+
@State windowHeight: number = 0;
|
|
11
|
+
|
|
12
|
+
aboutToAppear() {
|
|
13
|
+
this.updateWindowInfo();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private getWindowInfo(): WindowInfo {
|
|
17
|
+
return {
|
|
18
|
+
width: 1080,
|
|
19
|
+
height: 2340
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private updateWindowInfo(): void {
|
|
24
|
+
const info = this.getWindowInfo();
|
|
25
|
+
this.windowWidth = info.width;
|
|
26
|
+
this.windowHeight = info.height;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
build() {
|
|
30
|
+
Column() {
|
|
31
|
+
Text(`Window: ${this.windowWidth} x ${this.windowHeight}`)
|
|
32
|
+
.fontSize(20)
|
|
33
|
+
.margin({ bottom: 20 })
|
|
34
|
+
|
|
35
|
+
Button('Refresh')
|
|
36
|
+
.onClick(() => {
|
|
37
|
+
this.updateWindowInfo();
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// 错误示例 - LazyForEach 使用 string[] 直接报错
|
|
2
|
+
@Entry
|
|
3
|
+
@Component
|
|
4
|
+
struct IDataSourceErrorExample {
|
|
5
|
+
private data: string[] = ['Item 1', 'Item 2', 'Item 3'];
|
|
6
|
+
|
|
7
|
+
build() {
|
|
8
|
+
Column() {
|
|
9
|
+
List() {
|
|
10
|
+
LazyForEach(this.data, (item: string) => {
|
|
11
|
+
ListItem() {
|
|
12
|
+
Text(item)
|
|
13
|
+
}
|
|
14
|
+
}, (item: string) => item)
|
|
15
|
+
}
|
|
16
|
+
.width('100%')
|
|
17
|
+
.height('100%')
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 解决方案 - 实现 IDataSource 接口
|
|
23
|
+
class MyDataSource {
|
|
24
|
+
data: string[] = [];
|
|
25
|
+
private listeners: DataChangeListener[] = [];
|
|
26
|
+
|
|
27
|
+
totalCount(): number {
|
|
28
|
+
return this.data.length;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getData(index: number): string {
|
|
32
|
+
return this.data[index];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
registerDataChangeListener(listener: DataChangeListener): void {
|
|
36
|
+
this.listeners.push(listener);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
unregisterDataChangeListener(listener: DataChangeListener): void {
|
|
40
|
+
const index = this.listeners.indexOf(listener);
|
|
41
|
+
if (index > -1) {
|
|
42
|
+
this.listeners.splice(index, 1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pushData(data: string): void {
|
|
47
|
+
this.data.push(data);
|
|
48
|
+
this.listeners.forEach((listener: DataChangeListener) => {
|
|
49
|
+
listener.onDataAdd?.(this.data.length - 1);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
@Entry
|
|
55
|
+
@Component
|
|
56
|
+
struct IDataSourceCorrectExample {
|
|
57
|
+
private data: MyDataSource = new MyDataSource();
|
|
58
|
+
|
|
59
|
+
aboutToAppear() {
|
|
60
|
+
for (let i = 0; i < 100; i++) {
|
|
61
|
+
this.data.pushData(`Item ${i}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
build() {
|
|
66
|
+
Column() {
|
|
67
|
+
List() {
|
|
68
|
+
LazyForEach(this.data, (item: string) => {
|
|
69
|
+
ListItem() {
|
|
70
|
+
Text(item).fontSize(20).padding(10)
|
|
71
|
+
}
|
|
72
|
+
}, (item: string) => item)
|
|
73
|
+
}
|
|
74
|
+
.cachedCount(5)
|
|
75
|
+
.width('100%')
|
|
76
|
+
.height('100%')
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
@Entry
|
|
2
|
+
@Component
|
|
3
|
+
struct ImplementationNotAllowedError {
|
|
4
|
+
@State count: number = 0;
|
|
5
|
+
|
|
6
|
+
build() {
|
|
7
|
+
Column() {
|
|
8
|
+
Text('实现不允许错误示例')
|
|
9
|
+
.fontSize(20)
|
|
10
|
+
.fontWeight(FontWeight.Bold)
|
|
11
|
+
.margin({ bottom: 16 })
|
|
12
|
+
|
|
13
|
+
Text(`计数: ${this.count}`)
|
|
14
|
+
.fontSize(16)
|
|
15
|
+
.fontColor('#333333')
|
|
16
|
+
.margin({ bottom: 16 })
|
|
17
|
+
|
|
18
|
+
Button('增加')
|
|
19
|
+
.onClick(() => {
|
|
20
|
+
this.count++;
|
|
21
|
+
})
|
|
22
|
+
.margin({ bottom: 16 })
|
|
23
|
+
|
|
24
|
+
Row() {
|
|
25
|
+
Text('Row 内容')
|
|
26
|
+
.fontSize(14)
|
|
27
|
+
.fontColor('#666666')
|
|
28
|
+
}
|
|
29
|
+
.width('100%')
|
|
30
|
+
.padding(12)
|
|
31
|
+
.backgroundColor('#F5F5F5')
|
|
32
|
+
.borderRadius(4)
|
|
33
|
+
}
|
|
34
|
+
.width('100%')
|
|
35
|
+
.height('100%')
|
|
36
|
+
.padding(16)
|
|
37
|
+
}
|
|
38
|
+
}
|