1、代码示例
实现效果:

- /*
- * Copyright (c) 2023 Huawei Device Co., Ltd.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';
- import { window } from '@kit.ArkUI';
- import { BusinessError, deviceInfo } from '@kit.BasicServicesKit';
- import Logger from '../common/utils/Logger';
- import CommonConstants from '../common/constants/CommonConstants';
- import { GlobalContext } from '../common/utils/GlobalContext';
- /**
- * Lift cycle management of Ability.
- */
- export default class EntryAbility extends UIAbility {
- onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
- GlobalContext.getContext().setObject('abilityWant', want);
- GlobalContext.getContext().setObject('launchParam', launchParam);
- if (deviceInfo.deviceType !== CommonConstants.TABLET_DEVICE_TYPE) {
- window.getLastWindow(this.context, (err, data) => {
- if (err.code) {
- Logger.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
- return;
- }
- let orientation = window.Orientation.PORTRAIT;
- data.setPreferredOrientation(orientation, (err) => {
- if (err.code) {
- Logger.error('Failed to set window orientation. Cause: ' + JSON.stringify(err));
- return;
- }
- Logger.info('Succeeded in setting window orientation.');
- });
- });
- }
- }
- onWindowStageCreate(windowStage: window.WindowStage) {
- // Main window is created, set main page for this ability.
- Logger.info(CommonConstants.TAG_ABILITY, 'Ability onWindowStageCreate');
- windowStage.loadContent('pages/LoginPage', (err: BusinessError<void>, data) => {
- if (err.code) {
- Logger.error(CommonConstants.TAG_ABILITY, 'Load the content to failed ' + JSON.stringify(err));
- return;
- }
- Logger.info(CommonConstants.TAG_ABILITY, 'Loading the content to succeeded ' + JSON.stringify(data));
- });
- }
- }
复制代码 2、代码分析
2.1代码:
通过module.json5文件红框处查看入口代码文件。运行就跳入到登录页。
2.2代码:
程序实行onCreate函数先实行,其次onWindowStageCreate函数后实行。
- onCreate:在 Ability 创建时被调用,用于实行初始化和设置业务逻辑。
- onDestroy:在 Ability 销毁时触发,用于实行资源清理和其他清理操作。
- onWindowStageCreate:在 WindowStage 创建完成后触发。
- onWindowStageDestroy:在 WindowStage 销毁后触发。
- onForeground:Ability 的生命周期回调,当应用从背景切换到前台时调用。
- onBackground:Ability 的生命周期回调,当应用从前台切换到背景时调用。
生命周期
2.3代码:
- public register(): void {
- this.smListener = mediaquery.matchMediaSync(CommonConstants.WIDTH_CONDITION_SM);
- this.smListener.on('change', this.isDeviceSizeSM);
- this.mdListener = mediaquery.matchMediaSync(CommonConstants.WIDTH_CONDITION_MD);
- this.mdListener.on('change', this.isDeviceSizeMD);
- this.lgListener = mediaquery.matchMediaSync(CommonConstants.WIDTH_CONDITION_LG);
- this.lgListener.on('change', this.isDeviceSizeLG);
- }
复制代码
在页面初始化时实行注册方法时内里使用监听器来监听屏幕的尺寸变化,并改变AppStorage内里的设备尺寸值。
2.4代码:
- GridRow 为栅格容器组件,需与栅格子组件 GridCol 团结使用。
- 设置一个GridCol占栅格布局的列数
代码先容
2.5代码:
2.6代码:
直线绘制组件。
2.7代码:
oh-package-lock.json5文件中可以去引入包的依赖。
2.8代码:
页面跳转的router.pushUrl()方法。
3、注解分析
- @StorageProp('currentDeviceSize') currentDeviceSize: string = CommonConstants.SM;
复制代码
- @StorageProp
StorageProp(key) 和AppStorage中key对应的属性建立单向数据同步
- @StorageLink
StorageLink(key) 和AppStorage中key对应的属性建立双向数据同步
注解的作用
页面生命周期:
即被@Entry装饰的组件生命周期。提供以下生命周期接口:
- onPageShow: 页面每次体现时触发一次,包罗路由过程、应用进入前台等场景,仅@Entry装饰的自界说组件生效。
- onPageHide: 页面每次隐蔽时触发一次,包罗路由过程、应用进入前背景等场景,仅@Entry装饰的自界说组件生效。
- onBackPress:当用户点击返回按时触发,仅@Entry装饰的自界说组件生效。
- @Preview({
- title: 'Component', //预览组件的名称
- deviceType: 'phone', //指定当前组件预览渲染的设备类型,默认为Phone
- width: 1080, //预览设备的宽度,单位:px
- height: 2340, //预览设备的长度,单位:px
- colorMode: 'light', //显示的亮暗模式,当前支持取值为light
- dpi: 480, //预览设备的屏幕DPI值
- locale: 'zh_CN', //预览设备的语言,如zh_CN、en_US等
- orientation: 'portrait', //预览设备的横竖屏状态,取值为portrait或landscape
- roundScreen: false //设备的屏幕形状是否为圆形
- })
复制代码 ArkTS应用/服务支持组件预览,要求compileSdkVersion为8或以上。组件预览支持实时预览,不支持动态图和动态预览。组件预览通过在组件前添加注解@Preview实现,在单个源文件中,最多可以使用10个@Preview装饰自界说组件。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |