HarmonyOS实战开辟:@ohos.arkui.UIContext (UIContext)

打印 上一主题 下一主题

主题 1025|帖子 1025|积分 3075

在Stage模型中,WindowStage/Window可以通过loadContent接口加载页面并创建UI的实例,并将页面内容渲染到关联的窗口中,以是UI实例和窗口是逐一关联的。一些全局的UI接口是和具体UI实例的执行上下文干系的,在当前接口调用时,通过追溯调用链跟踪到UI的上下文,来确定具体的UI实例。若在非UI页面中或者一些异步回调中调用这类接口,大概无法跟踪到当前UI的上下文,导致接口执行失败。
@ohos.window在API version 10 新增getUIContext接口,获取UI上下文实例UIContext对象,使用UIContext对象提供的替代方法,可以直接作用在对应的UI实例上。
   阐明:
  本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
  示例效果请以真机运举动准,当前IDE预览器不支持。
  UICon文本

以下API需先使用ohos.window中的getUIContext()方法获取UIContext实例,再通过此实例调用对应方法。本文中UIContext对象以uiContext表示。
getFont的

getFont(): 字体
获取Font对象。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明字体返回Font实例对象。 示例:
  1. uiContext.getFont();
复制代码
getComponentUtils

getComponentUtils(): 组件实用程序
获取ComponentUtils对象。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明组件实用程序返回ComponentUtils实例对象。 示例:
  1. uiContext.getComponentUtils();
复制代码
getUIInspector

getUIInspector(): UIInspector
获取UIInspector对象。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明UIInspector(UI检查器)返回UIInspector实例对象。 示例:
  1. uiContext.getUIInspector();
复制代码
getMediaQuery

getMediaQuery(): MediaQuery
获取MediaQuery对象。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明MediaQuery(媒体查询)返回MediaQuery实例对象。 示例:
  1. uiContext.getMediaQuery();
复制代码
get路由器

getRouter(): 路由器
获取Router对象。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明路由器返回Router实例对象。 示例:
  1. uiContext.getRouter();
复制代码
getPromptAction

getPromptAction(): 提示操纵
获取PromptAction对象。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明提示操纵返回PromptAction实例对象。 示例:
  1. uiContext.getPromptAction();
复制代码
animate设置为

animateTo(value: AnimateParam, event: () => void): void
提供animateTo接口来指定由于闭包代码导致的状态变革插入过渡动效。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明价值AnimateParam是设置动画效果干系参数。变乱() => 无效是指定显示动效的闭包函数,在闭包函数中导致的状态变革系统会主动插入过渡动画。 示例:
  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct AnimateToExample {
  5.   @State widthSize: number = 250
  6.   @State heightSize: number = 100
  7.   @State rotateAngle: number = 0
  8.   private flag: boolean = true
  9.   build() {
  10.     Column() {
  11.       Button('change size')
  12.         .width(this.widthSize)
  13.         .height(this.heightSize)
  14.         .margin(30)
  15.         .onClick(() => {
  16.           if (this.flag) {
  17.             uiContext.animateTo({
  18.               duration: 2000,
  19.               curve: Curve.EaseOut,
  20.               iterations: 3,
  21.               playMode: PlayMode.Normal,
  22.               onFinish: () => {
  23.                 console.info('play end')
  24.               }
  25.             }, () => {
  26.               this.widthSize = 150
  27.               this.heightSize = 60
  28.             })
  29.           } else {
  30.             uiContext.animateTo({}, () => {
  31.               this.widthSize = 250
  32.               this.heightSize = 100
  33.             })
  34.           }
  35.           this.flag = !this.flag
  36.         })
  37.       Button('change rotate angle')
  38.         .margin(50)
  39.         .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
  40.         .onClick(() => {
  41.           uiContext.animateTo({
  42.             duration: 1200,
  43.             curve: Curve.Friction,
  44.             delay: 500,
  45.             iterations: -1, // 设置-1表示动画无限循环
  46.             playMode: PlayMode.Alternate,
  47.             onFinish: () => {
  48.               console.info('play end')
  49.             }
  50.           }, () => {
  51.             this.rotateAngle = 90
  52.           })
  53.         })
  54.     }.width('100%').margin({ top: 5 })
  55.   }
  56. }
复制代码
showAlertDialog

showAlertDialog(选项:AlertDialogParamWithConfirm |AlertDialogParamWithButtons |AlertDialogParamWithOptions):无效
显示警告弹窗组件,可设置文本内容与相应回调。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项警报对话框ParamWithConfirm |AlertDialogParamWithButtons |AlertDialogParamWithOptions是定义并显示AlertDialog组件。 示例:
  1. uiContext.showAlertDialog(
  2.   {
  3.     title: 'title',
  4.     message: 'text',
  5.     autoCancel: true,
  6.     alignment: DialogAlignment.Bottom,
  7.     offset: { dx: 0, dy: -20 },
  8.     gridCount: 3,
  9.     confirm: {
  10.       value: 'button',
  11.       action: () => {
  12.         console.info('Button-clicking callback')
  13.       }
  14.     },
  15.     cancel: () => {
  16.       console.info('Closed callbacks')
  17.     }
  18.   }
  19. )
复制代码
showActionSheet

showActionSheet(value: ActionSheetOptions): void
定义列表弹窗并弹出。
系统本领:SystemCapability.ArkUI.ArkUI.Full
ActionSheetOptions参数:
参数名类型必填阐明标题资源 |字符串是弹窗标题。消息资源 |字符串是弹窗内容。主动取消布尔否点击遮障层时,是否关闭弹窗。
默认值:true确认{
值:ResourceStr,
操纵:() => void
}否确认按钮的文本内容和点击回调。
默认值:
value:按钮文本内容。
action: 按钮选中时的回调。取消() => 无效否点击遮障层关闭dialog时的回调。对准对话框对齐否弹窗在竖直方向上的对齐方式。
默认值:DialogAlignment.Bottom抵消{
dx:长度,
dy:长度
}否弹窗相对alignment所在位置的偏移量。{
dx:0,dy
:0
}床单数组<SheetInfo>是设置选项内容,每个选择项支持设置图片、文本和选中的回调。 SheetInfo接口阐明:
参数名类型必填阐明标题资源链是选项的文本内容。图标资源链否选项的图标,默认无图标显示。行动()=>void是选项选中的回调。 示例:
  1. uiContext.showActionSheet({
  2.   title: 'ActionSheet title',
  3.   message: 'message',
  4.   autoCancel: true,
  5.   confirm: {
  6.     value: 'Confirm button',
  7.     action: () => {
  8.       console.log('Get Alert Dialog handled')
  9.     }
  10.   },
  11.   cancel: () => {
  12.     console.log('actionSheet canceled')
  13.   },
  14.   alignment: DialogAlignment.Bottom,
  15.   offset: { dx: 0, dy: -10 },
  16.   sheets: [
  17.     {
  18.       title: 'apples',
  19.       action: () => {
  20.         console.log('apples')
  21.       }
  22.     },
  23.     {
  24.       title: 'bananas',
  25.       action: () => {
  26.         console.log('bananas')
  27.       }
  28.     },
  29.     {
  30.       title: 'pears',
  31.       action: () => {
  32.         console.log('pears')
  33.       }
  34.     }
  35.   ]
  36. })
复制代码
showDatePicker对话框

showDatePickerDialog(选项:DatePickerDialogOptions):无效
定义日期滑动选择器弹窗并弹出。
系统本领:SystemCapability.ArkUI.ArkUI.Full
DatePickerDialogOptions参数:
参数名类型必填阐明开始日期否设置选择器的起始日期。
默认值:Date('1970-1-1')竣事日期否设置选择器的竣事日期。
默认值:Date('2100-12-31')选择日期否设置当前选中的日期。
默认值:当前系统日期月球布尔否日期是否显示为夏历。
默认值:falseshow时间布尔否是否展示时间项。
默认值:false使用MilitaryTime布尔否展示时间是否为24小时制。
默认值:false消失文本样式PickerTextStyle否设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。文本样式PickerTextStyle否设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。selectedTextStylePickerTextStyle否设置选中项的文本颜色、字号、字体粗细。onAccept(值:DatePickerResult) => void否点击弹窗中的“确定”按钮时触发该回调。on取消() => 无效否点击弹窗中的“取消”按钮时触发该回调。onChange(值:DatePickerResult) => void否滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。 示例:
  1. let selectedDate: Date = new Date("2010-1-1")
  2. uiContext.showDatePickerDialog({
  3.   start: new Date("2000-1-1"),
  4.   end: new Date("2100-12-31"),
  5.   selected: selectedDate,
  6.   onAccept: (value: DatePickerResult) => {
  7.     // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期
  8.     selectedDate.setFullYear(Number(value.year), Number(value.month), Number(value.day))
  9.     console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
  10.   },
  11.   onCancel: () => {
  12.     console.info("DatePickerDialog:onCancel()")
  13.   },
  14.   onChange: (value: DatePickerResult) => {
  15.     console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
  16.   }
  17. })
复制代码
showTimePicker对话框

showTimePickerDialog(选项:TimePickerDialogOptions):无效
定义时间滑动选择器弹窗并弹出。
系统本领:SystemCapability.ArkUI.ArkUI.Full
TimePickerDialogOptions参数:
参数名类型必填阐明选择日期否设置当前选中的时间。
默认值:当前系统时间使用MilitaryTime布尔否展示时间是否为24小时制,默认为12小时制。
默认值:false消失文本样式PickerTextStyle否设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。文本样式PickerTextStyle否设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。selectedTextStylePickerTextStyle否设置选中项的文本颜色、字号、字体粗细。onAccept(值:TimePickerResult) => void否点击弹窗中的“确定”按钮时触发该回调。on取消() => 无效否点击弹窗中的“取消”按钮时触发该回调。onChange(值:TimePickerResult) => void否滑动弹窗中的选择器使当前选中时间改变时触发该回调。 示例:
  1. class sethours{
  2.   selectTime: Date = new Date('2020-12-25T08:30:00')
  3.   hours(h:number,m:number){
  4.     this.selectTime.setHours(h,m)
  5.   }
  6. }
  7. uiContext.showTimePickerDialog({
  8.   selected: this.selectTime,
  9.   onAccept: (value: TimePickerResult) => {
  10.     // 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间
  11.     let time = new sethours()
  12.     if(value.hour&&value.minute){
  13.       time.hours(value.hour, value.minute)
  14.     }
  15.     console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
  16.   },
  17.   onCancel: () => {
  18.     console.info("TimePickerDialog:onCancel()")
  19.   },
  20.   onChange: (value: TimePickerResult) => {
  21.     console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
  22.   }
  23. })
复制代码
showTextPicker对话框

showTextPickerDialog(选项:TextPickerDialogOptions):无效
定义文本滑动选择器弹窗并弹出。
系统本领:SystemCapability.ArkUI.ArkUI.Full
TextPickerDialogOptions参数:
参数名类型必填阐明范围字符串[] |资源|TextPickerRangeContent[]是设置文本选择器的选择范围。不可设置为空数组,若设置为空数组,则不弹出弹窗。选择数否设置选中项的索引值。
默认值:0价值字符串否设置选中项的文本内容。当设置了selected参数时,该参数不生效。如果设置的value值不在range范围内,则默认取range第一个元素。defaultPickerItemHeight数字 |字符串否设置选择器中选项的高度。消失文本样式PickerTextStyle否设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。文本样式PickerTextStyle否设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。selectedTextStylePickerTextStyle否设置选中项的文本颜色、字号、字体粗细。onAccept(值:TextPickerResult) => void否点击弹窗中的“确定”按钮时触发该回调。on取消() => 无效否点击弹窗中的“取消”按钮时触发该回调。onChange(值:TextPickerResult) => void否滑动弹窗中的选择器使当前选中项改变时触发该回调。 示例:
  1. { class setvalue{
  2.   select: number = 2
  3.   set(val:number){
  4.     this.select = val
  5.   }
  6. }
  7. class setvaluearr{
  8.   select: number[] = []
  9.   set(val:number[]){
  10.     this.select = val
  11.   }
  12. }
  13. let fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
  14. uiContext.showTextPickerDialog({
  15.   range: this.fruits,
  16.   selected: this.select,
  17.   onAccept: (value: TextPickerResult) => {
  18.     // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
  19.     let setv = new setvalue()
  20.     let setvarr = new setvaluearr()
  21.     if(value.index){
  22.       value.index instanceof Array?setvarr.set(value.index) : setv.set(value.index)
  23.     }
  24.     console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
  25.   },
  26.   onCancel: () => {
  27.     console.info("TextPickerDialog:onCancel()")
  28.   },
  29.   onChange: (value: TextPickerResult) => {
  30.     console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
  31.   }
  32. })
复制代码
createAnimator

createAnimator(options: AnimatorOptions): AnimatorResult
定义Animator类。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项动画师选项是定义动画选项。 返回值:
类型阐明动画师结果Animator结果接口。 示例:
  1. import { AnimatorOptions } from '@ohos.animator';
  2. let options:AnimatorOptions = {
  3.   duration: 1500,
  4.   easing: "friction",
  5.   delay: 0,
  6.   fill: "forwards",
  7.   direction: "normal",
  8.   iterations: 3,
  9.   begin: 200.0,
  10.   end: 400.0
  11. };
  12. uiContext.createAnimator(options);
复制代码
runScopedTask

runScopedTask(callback: () => void): 无效
在当前UI上下文执行传入的回调函数。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明回调() => 无效是回调函数 示例:
  1. uiContext.runScopedTask(
  2.   () => {
  3.     console.log('Succeeded in runScopedTask');
  4.   }
  5. );
复制代码
字体

以下API需先使用UIContext中的getFont()方法获取到Font对象,再通过该对象调用对应方法。
注册字体

registerFont(options: font.FontOptions):无效
在字体管理中注册自定义字体。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项字体。字体选项是注册的自定义字体信息。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. let font:Font = uiContext.getFont();
  3. font.registerFont({
  4.   familyName: 'medium',
  5.   familySrc: '/font/medium.ttf'
  6. });
复制代码
getStstemFontList

getSystemFontList():数组<字符串>
获取系统支持的字体名称列表。
系统本领:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明数组<字符串>系统的字体名列表。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. let font:Font|undefined = uiContext.getFont();
  3. if(font){
  4.   font.getSystemFontList()
  5. }
复制代码
getFontByName

getFontByName(fontName: string): 字体。字体信息
根据传入的系统字体名称获取系统字体的干系信息。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明font名称字符串是系统的字体名。 返回值:
类型阐明字体信息字体的具体信息 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. let font:Font|undefined = uiContext.getFont();
  3. if(font){
  4.   font.getFontByName('Sans Italic')
  5. }
复制代码
组件实用程序

以下API需先使用UIContext中的getComponentUtils()方法获取到ComponentUtils对象,再通过该对象调用对应方法。
getRectangleById

getRectangleById(id:字符串):componentUtils.ComponentInfo
获取组件大小、位置、平移缩放旋转及仿射矩阵属性信息。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明同上字符串是组件唯一标识id。 返回值:
类型阐明ComponentInfo组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. let componentUtils:ComponentUtils = uiContext.getComponentUtils();
  3. let modePosition = componentUtils.getRectangleById("onClick");
  4. let localOffsetWidth = modePosition.size.width;
  5. let localOffsetHeight = modePosition.size.height;
复制代码
UIInspector(UI检查器)

以下API需先使用UIContext中的getUIInspector()方法获取到UIInspector对象,再通过该对象调用对应方法。
createComponentObserver

createComponentObserver(id: string):检查器。组件观察者
注册组件布局和绘制完成回调通知。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明同上字符串是指定组件id。 返回值:
类型阐明组件观察者组件回调变乱监听句柄,用于注册和取消注册监听回调。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. let inspector:UIInspector = uiContext.getUIInspector();
  3. let listener = inspector.createComponentObserver('COMPONENT_ID');
复制代码
MediaQuery(媒体查询)

以下API需先使用UIContext中的getMediaQuery()方法获取到MediaQuery对象,再通过该对象调用对应方法。
匹配媒体同步

matchMediaSync(条件:字符串): mediaQuery.MediaQueryListener
设置媒体查询的查询条件,并返回对应的监听句柄。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明条件字符串是媒体变乱的匹配条件,具体可参考媒体查询语法规则。 返回值:
类型阐明MediaQueryListener媒体变乱监听句柄,用于注册和去注册监听回调。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. let mediaquery: MediaQuery = uiContext.getMediaQuery();
  3. let listener = mediaquery.matchMediaSync('(orientation: landscape)'); //监听横屏事件
复制代码
路由器

以下API需先使用UIContext中的getRouter()方法获取到Router对象,再通过该对象调用对应方法。
pushUrl

pushUrl(options: router.RouterOptions):允许<无效>
跳转到应用内的指定页面。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项路由器。路由器选项是跳转页面形貌信息。 返回值:
类型阐明允许<无效>非常返回结果。 错误码:
以下错误码的具体介绍,请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001如果未找到 UI 执行上下文。100002如果 URI 不存在。100003如果页面推送过多。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. try {
  5.   router.pushUrl({
  6.     url: 'pages/routerpage2',
  7.     params: {
  8.       data1: 'message',
  9.       data2: {
  10.         data3: [123, 456, 789]
  11.       }
  12.     }
  13.   })
  14. } catch (err) {
  15.   let message = (err as BusinessError).message;
  16.   let code = (err as BusinessError).code;
  17.   console.error(`pushUrl failed, code is ${code}, message is ${message}`);
  18. }
复制代码
pushUrl

pushUrl(options: router.RouterOptions,回调:AsyncCallback<void>):void
跳转到应用内的指定页面。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项路由器。路由器选项是跳转页面形貌信息。回调AsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍,请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001如果未找到 UI 执行上下文。100002如果 URI 不存在。100003如果页面推送过多。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. router.pushUrl({
  5.   url: 'pages/routerpage2',
  6.   params: {
  7.     data1: 'message',
  8.     data2: {
  9.       data3: [123, 456, 789]
  10.     }
  11.   }
  12. }, (err: Error) => {
  13.   if (err) {
  14.     let message = (err as BusinessError).message;
  15.     let code = (err as BusinessError).code;
  16.     console.error(`pushUrl failed, code is ${code}, message is ${message}`);
  17.     return;
  18.   }
  19.   console.info('pushUrl success');
  20. })
复制代码
pushUrl

pushUrl(options: router.RouterOptions,模式:路由器。RouterMode):Promise<void>
跳转到应用内的指定页面。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项路由器。路由器选项是跳转页面形貌信息。模式路由器。路由器模式是跳转页面使用的模式。 返回值:
类型阐明允许<无效>非常返回结果。 错误码:
以下错误码的具体介绍,请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001如果未找到 UI 执行上下文。100002如果 URI 不存在。100003如果页面推送过多。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. try {
  10.   routerF.pushUrl({
  11.     url: 'pages/routerpage2',
  12.     params: {
  13.       data1: 'message',
  14.       data2: {
  15.         data3: [123, 456, 789]
  16.       }
  17.     }
  18.   }, rtm.Standard)
  19. } catch (err) {
  20.   let message = (err as BusinessError).message;
  21.   let code = (err as BusinessError).code;
  22.   console.error(`pushUrl failed, code is ${code}, message is ${message}`);
  23. }
复制代码
pushUrl

pushUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
跳转到应用内的指定页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.RouterOptions是跳转页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。callbackAsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found.100002if the uri is not exist.100003if the pages are pushed too much. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. routerF.pushUrl({
  10.   url: 'pages/routerpage2',
  11.   params: {
  12.     data1: 'message',
  13.     data2: {
  14.       data3: [123, 456, 789]
  15.     }
  16.   }
  17. }, rtm.Standard, (err) => {
  18.   if (err) {
  19.     let message = (err as BusinessError).message;
  20.     let code = (err as BusinessError).code;
  21.     console.error(`pushUrl failed, code is ${code}, message is ${message}`);
  22.     return;
  23.   }
  24.   console.info('pushUrl success');
  25. })
复制代码
替换 Url

replaceUrl(选项:路由器。RouterOptions):允许<无效>
用应用内的某个页面替换当前页面,并烧毁被替换的页面。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项路由器。路由器选项是替换页面形貌信息。 返回值:
类型阐明允许<无效>非常返回结果。 错误码:
以下错误码的具体介绍,请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001如果未找到UI执行上下文,则仅投入标准系统。200002如果 URI 不存在。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. try {
  5.   router.replaceUrl({
  6.     url: 'pages/detail',
  7.     params: {
  8.       data1: 'message'
  9.     }
  10.   })
  11. } catch (err) {
  12.   let message = (err as BusinessError).message;
  13.   let code = (err as BusinessError).code;
  14.   console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
  15. }
复制代码
替换 Url

replaceUrl(选项:路由器。RouterOptions,回调:AsyncCallback<void>):void
用应用内的某个页面替换当前页面,并烧毁被替换的页面。
系统本领:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明选项路由器。路由器选项是替换页面形貌信息。回调AsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍,请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001如果未找到UI执行上下文,则仅投入标准系统。200002如果 URI 不存在。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. router.replaceUrl({
  5.   url: 'pages/detail',
  6.   params: {
  7.     data1: 'message'
  8.   }
  9. }, (err: Error) => {
  10.   if (err) {
  11.     let message = (err as BusinessError).message;
  12.     let code = (err as BusinessError).code;
  13.     console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
  14.     return;
  15.   }
  16.   console.info('replaceUrl success');
  17. })
复制代码
替换 Url

replaceUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void>
用应用内的某个页面替换当前页面,并烧毁被替换的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.RouterOptions是替换页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。 返回值:
类型阐明Promise<void>非常返回结果。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if can not get the delegate, only throw in standard system.200002if the uri is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. try {
  10.   routerF.replaceUrl({
  11.     url: 'pages/detail',
  12.     params: {
  13.       data1: 'message'
  14.     }
  15.   }, rtm.Standard)
  16. } catch (err) {
  17.   let message = (err as BusinessError).message;
  18.   let code = (err as BusinessError).code;
  19.   console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
  20. }
复制代码
replaceUrl

replaceUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
用应用内的某个页面替换当前页面,并烧毁被替换的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.RouterOptions是替换页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。callbackAsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found, only throw in standard system.200002if the uri is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector,  MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. routerF.replaceUrl({
  10.   url: 'pages/detail',
  11.   params: {
  12.     data1: 'message'
  13.   }
  14. }, rtm.Standard, (err: Error) => {
  15.   if (err) {
  16.     let message = (err as BusinessError).message;
  17.     let code = (err as BusinessError).code;
  18.     console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
  19.     return;
  20.   }
  21.   console.info('replaceUrl success');
  22. });
复制代码
pushNamedRoute

pushNamedRoute(options: router.NamedRouterOptions): Promise<void>
跳转到指定的命名路由页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是跳转页面形貌信息。 返回值:
类型阐明Promise<void>非常返回结果。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found.100003if the pages are pushed too much.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. try {
  5.   router.pushNamedRoute({
  6.     name: 'myPage',
  7.     params: {
  8.       data1: 'message',
  9.       data2: {
  10.         data3: [123, 456, 789]
  11.       }
  12.     }
  13.   })
  14. } catch (err) {
  15.   let message = (err as BusinessError).message;
  16.   let code = (err as BusinessError).code;
  17.   console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
  18. }
复制代码
pushNamedRoute

pushNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void
跳转到指定的命名路由页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是跳转页面形貌信息。callbackAsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found.100003if the pages are pushed too much.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. router.pushNamedRoute({
  5.   name: 'myPage',
  6.   params: {
  7.     data1: 'message',
  8.     data2: {
  9.       data3: [123, 456, 789]
  10.     }
  11.   }
  12. }, (err: Error) => {
  13.   if (err) {
  14.     let message = (err as BusinessError).message;
  15.     let code = (err as BusinessError).code;
  16.     console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
  17.     return;
  18.   }
  19.   console.info('pushNamedRoute success');
  20. })
复制代码
pushNamedRoute

pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void>
跳转到指定的命名路由页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是跳转页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。 返回值:
类型阐明Promise<void>非常返回结果。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found.100003if the pages are pushed too much.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. try {
  10.   routerF.pushNamedRoute({
  11.     name: 'myPage',
  12.     params: {
  13.       data1: 'message',
  14.       data2: {
  15.         data3: [123, 456, 789]
  16.       }
  17.     }
  18.   }, rtm.Standard)
  19. } catch (err) {
  20.   let message = (err as BusinessError).message;
  21.   let code = (err as BusinessError).code;
  22.   console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
  23. }
复制代码
pushNamedRoute

pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
跳转到指定的命名路由页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是跳转页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。callbackAsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found.100003if the pages are pushed too much.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. routerF.pushNamedRoute({
  10.   name: 'myPage',
  11.   params: {
  12.     data1: 'message',
  13.     data2: {
  14.       data3: [123, 456, 789]
  15.     }
  16.   }
  17. }, rtm.Standard, (err: Error) => {
  18.   if (err) {
  19.     let message = (err as BusinessError).message;
  20.     let code = (err as BusinessError).code;
  21.     console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
  22.     return;
  23.   }
  24.   console.info('pushNamedRoute success');
  25. })
复制代码
replaceNamedRoute

replaceNamedRoute(options: router.NamedRouterOptions): Promise<void>
用指定的命名路由页面替换当前页面,并烧毁被替换的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是替换页面形貌信息。 返回值:
类型阐明Promise<void>非常返回结果。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found, only throw in standard system.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. try {
  5.   router.replaceNamedRoute({
  6.     name: 'myPage',
  7.     params: {
  8.       data1: 'message'
  9.     }
  10.   })
  11. } catch (err) {
  12.   let message = (err as BusinessError).message;
  13.   let code = (err as BusinessError).code;
  14.   console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
  15. }
复制代码
replaceNamedRoute

replaceNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void
用指定的命名路由页面替换当前页面,并烧毁被替换的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是替换页面形貌信息。callbackAsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found, only throw in standard system.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router:Router = uiContext.getRouter();
  4. router.replaceNamedRoute({
  5.   name: 'myPage',
  6.   params: {
  7.     data1: 'message'
  8.   }
  9. }, (err: Error) => {
  10.   if (err) {
  11.     let message = (err as BusinessError).message;
  12.     let code = (err as BusinessError).code;
  13.     console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
  14.     return;
  15.   }
  16.   console.info('replaceNamedRoute success');
  17. })
复制代码
replaceNamedRoute

replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void>
用指定的命名路由页面替换当前页面,并烧毁被替换的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是替换页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。 返回值:
类型阐明Promise<void>非常返回结果。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if can not get the delegate, only throw in standard system.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. try {
  10.   routerF.replaceNamedRoute({
  11.     name: 'myPage',
  12.     params: {
  13.       data1: 'message'
  14.     }
  15.   }, rtm.Standard)
  16. } catch (err) {
  17.   let message = (err as BusinessError).message;
  18.   let code = (err as BusinessError).code;
  19.   console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
  20. }
复制代码
replaceNamedRoute

replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
用指定的命名路由页面替换当前页面,并烧毁被替换的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.NamedRouterOptions是替换页面形貌信息。moderouter.RouterMode是跳转页面使用的模式。callbackAsyncCallback<void>是非常相应回调。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found, only throw in standard system.100004if the named route is not exist. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. import router from '@ohos.router';
  4. let routerF:Router = uiContext.getRouter();
  5. class routerTmp{
  6.   Standard:router.RouterMode = router.RouterMode.Standard
  7. }
  8. let rtm:routerTmp = new routerTmp()
  9. routerF.replaceNamedRoute({
  10.   name: 'myPage',
  11.   params: {
  12.     data1: 'message'
  13.   }
  14. }, rtm.Standard, (err: Error) => {
  15.   if (err) {
  16.     let message = (err as BusinessError).message;
  17.     let code = (err as BusinessError).code;
  18.     console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
  19.     return;
  20.   }
  21.   console.info('replaceNamedRoute success');
  22. });
复制代码
back

back(options?: router.RouterOptions ): void
返回上一页面或指定的页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.RouterOptions否返回页面形貌信息,此中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不相应该情况。如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会接纳,出栈后会被接纳。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. router.back({url:'pages/detail'});   
复制代码
clear

clear(): void
清空页面栈中的所有汗青页面,仅保留当前页面作为栈顶页面。
系统本领: SystemCapability.ArkUI.ArkUI.Full
示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. router.clear();   
复制代码
getLength

getLength(): string
获取当前在页面栈内的页面数量。
系统本领: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明string页面数量,页面栈支持最大数值是32。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. let size = router.getLength();        
  5. console.log('pages stack size = ' + size);   
复制代码
getState

getState(): router.RouterState
获取当前页面的状态信息。
系统本领: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明RouterState页面状态信息。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. let page = router.getState();
  5. console.log('current index = ' + page.index);
  6. console.log('current name = ' + page.name);
  7. console.log('current path = ' + page.path);
复制代码
showAlertBeforeBackPage

showAlertBeforeBackPage(options: router.EnableAlertOptions): void
开启页面返回扣问对话框。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionsrouter.EnableAlertOptions是文本弹窗信息形貌。 错误码:
以下错误码的具体介绍请拜见ohos.router(页面路由)错误码。
错误码ID错误信息100001if UI execution context not found. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. try {
  5.   router.showAlertBeforeBackPage({            
  6.     message: 'Message Info'        
  7.   });
  8. } catch(error) {
  9.   let message = (error as BusinessError).message;
  10.   let code = (error as BusinessError).code;
  11.   console.error(`showAlertBeforeBackPage failed, code is ${code}, message is ${message}`);
  12. }
复制代码
hideAlertBeforeBackPage

hideAlertBeforeBackPage(): void
禁用页面返回扣问对话框。
系统本领: SystemCapability.ArkUI.ArkUI.Full
示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. router.hideAlertBeforeBackPage();   
复制代码
getParams

getParams(): Object
获取发起跳转的页面往当前页传入的参数。
系统本领: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型阐明object发起跳转的页面往当前页传入的参数。 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let router: Router = uiContext.getRouter();
  4. router.getParams();
复制代码
PromptAction

以下API需先使用UIContext中的getPromptAction()方法获取到PromptAction对象,再通过该对象调用对应方法。
showToast

showToast(options: promptAction.ShowToastOptions): void
创建并显示文本提示框。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionspromptAction.ShowToastOptions是文本弹窗选项。 错误码:
以下错误码的具体介绍请拜见ohos.promptAction(弹窗)错误码。
错误码ID错误信息100001if UI execution context not found. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let promptAction: PromptAction = uiContext.getPromptAction();
  4. try {
  5.   promptAction.showToast({            
  6.     message: 'Message Info',
  7.     duration: 2000
  8.   });
  9. } catch (error) {
  10.   let message = (error as BusinessError).message;
  11.   let code = (error as BusinessError).code;
  12.   console.error(`showToast args error code is ${code}, message is ${message}`);
  13. };
复制代码
showDialog

showDialog(options: promptAction.ShowDialogOptions, callback: AsyncCallback<promptAction.ShowDialogSuccessResponse>): void
创建并显示对话框,对话框相应结果异步返回。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionspromptAction.ShowDialogOptions是页面显示对话框信息形貌。callbackAsyncCallback<promptAction.ShowDialogSuccessResponse>是对话框相应结果回调。 错误码:
以下错误码的具体介绍请拜见ohos.promptAction(弹窗)错误码。
错误码ID错误信息100001if UI execution context not found. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. class buttonsMoabl {
  4.   text: string = ""
  5.   color: string = ""
  6. }
  7. let promptAction: PromptAction = uiContext.getPromptAction();
  8. try {
  9.   promptAction.showDialog({
  10.     title: 'showDialog Title Info',
  11.     message: 'Message Info',
  12.     buttons: [
  13.       {
  14.         text: 'button1',
  15.         color: '#000000'
  16.       } as buttonsMoabl,
  17.       {
  18.         text: 'button2',
  19.         color: '#000000'
  20.       } as buttonsMoabl
  21.     ]
  22.   }, (err, data) => {
  23.     if (err) {
  24.       console.info('showDialog err: ' + err);
  25.       return;
  26.     }
  27.     console.info('showDialog success callback, click button: ' + data.index);
  28.   });
  29. } catch (error) {
  30.   let message = (error as BusinessError).message;
  31.   let code = (error as BusinessError).code;
  32.   console.error(`showDialog args error code is ${code}, message is ${message}`);
  33. };
复制代码
showDialog

showDialog(options: promptAction.ShowDialogOptions): Promise<promptAction.ShowDialogSuccessResponse>
创建并显示对话框,对话框相应后同步返回结果。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionspromptAction.ShowDialogOptions是对话框选项。 返回值:
类型阐明Promise<promptAction.ShowDialogSuccessResponse>对话框相应结果。 错误码:
以下错误码的具体介绍请拜见ohos.promptAction(弹窗)错误码。
错误码ID错误信息100001if UI execution context not found. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let promptAction: PromptAction = uiContext.getPromptAction();
  4. try {
  5.   promptAction.showDialog({
  6.     title: 'Title Info',
  7.     message: 'Message Info',
  8.     buttons: [
  9.       {
  10.         text: 'button1',
  11.         color: '#000000'
  12.       },
  13.       {
  14.         text: 'button2',
  15.         color: '#000000'
  16.       }
  17.     ],
  18.   })
  19.     .then(data => {
  20.       console.info('showDialog success, click button: ' + data.index);
  21.     })
  22.     .catch((err:Error) => {
  23.       console.info('showDialog error: ' + err);
  24.     })
  25. } catch (error) {
  26.   let message = (error as BusinessError).message;
  27.   let code = (error as BusinessError).code;
  28.   console.error(`showDialog args error code is ${code}, message is ${message}`);
  29. };
复制代码
showActionMenu

showActionMenu(options: promptAction.ActionMenuOptions, callback:promptAction.ActionMenuSuccessResponse):void
创建并显示操纵菜单,菜单相应结果异步返回。
系统本领: SystemCapability.ArkUI.ArkUI.Full。
参数:
参数名类型必填阐明optionspromptAction.ActionMenuOptions是操纵菜单选项。callbackpromptAction.ActionMenuSuccessResponse是菜单相应结果回调。 错误码:
以下错误码的具体介绍请拜见ohos.promptAction(弹窗)错误码。
错误码ID错误信息100001if UI execution context not found. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import promptAction from '@ohos.promptAction';
  3. import { BusinessError } from '@ohos.base';
  4. class buttonsMoabl {
  5.   text: string = ""
  6.   color: string = ""
  7. }
  8. class dataR{
  9.   err:Error = new Error;
  10.   data:promptAction.ActionMenuSuccessResponse | undefined = undefined;
  11. }
  12. let dataAMSR:dataR = new dataR()
  13. let promptActionF: PromptAction = uiContext.getPromptAction();
  14. try {
  15.   if(dataAMSR.data){
  16.     promptActionF.showActionMenu({
  17.       title: 'Title Info',
  18.       buttons: [
  19.         {
  20.           text: 'item1',
  21.           color: '#666666'
  22.         } as buttonsMoabl,
  23.         {
  24.           text: 'item2',
  25.           color: '#000000'
  26.         } as buttonsMoabl
  27.       ]
  28.     }, (dataAMSR.data))
  29.     if (dataAMSR.err) {
  30.       console.info('showActionMenu err: ' + dataAMSR.err);
  31.     }else{
  32.       console.info('showActionMenu success callback, click button: ' + dataAMSR.data.index);
  33.     }
  34.   }
  35. } catch (error) {
  36.   let message = (error as BusinessError).message;
  37.   let code = (error as BusinessError).code;
  38.   console.error(`showActionMenu args error code is ${code}, message is ${message}`);
  39. };
复制代码
showActionMenu

showActionMenu(options: promptAction.ActionMenuOptions): Promise<promptAction.ActionMenuSuccessResponse>
创建并显示操纵菜单,菜单相应后同步返回结果。
系统本领: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名类型必填阐明optionspromptAction.ActionMenuOptions是操纵菜单选项。 返回值:
类型阐明Promise<promptAction.ActionMenuSuccessResponse>菜单相应结果。 错误码:
以下错误码的具体介绍请拜见ohos.promptAction(弹窗)错误码。
错误码ID错误信息100001if UI execution context not found. 示例:
  1. import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
  2. import { BusinessError } from '@ohos.base';
  3. let promptAction: PromptAction = uiContext.getPromptAction();
  4. try {
  5.   promptAction.showActionMenu({
  6.     title: 'showActionMenu Title Info',
  7.     buttons: [
  8.       {
  9.         text: 'item1',
  10.         color: '#666666'
  11.       },
  12.       {
  13.         text: 'item2',
  14.         color: '#000000'
  15.       },
  16.     ]
  17.   })
  18.     .then(data => {
  19.       console.info('showActionMenu success, click button: ' + data.index);
  20.     })
  21.     .catch((err:Error) => {
  22.       console.info('showActionMenu error: ' + err);
  23.     })
  24. } catch (error) {
  25.   let message = (error as BusinessError).message;
  26.   let code = (error as BusinessError).code;
  27.   console.error(`showActionMenu args error code is ${code}, message is ${message}`);
  28. };
复制代码
末了

有许多小伙伴不知道学习哪些鸿蒙开辟技术?不知道必要重点把握哪些鸿蒙应用开辟知识点?而且学习时频繁踩坑,最终浪费大量时间。以是有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 
这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开辟必把握的核心知识要点,内容包含了ArkTS、ArkUI开辟组件、Stage模型、多端部署、分布式应用开辟、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开辟、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。
希望这一份鸿蒙学习资料可以或许给各人带来资助,有必要的小伙伴自行领取,限时开源,先到先得~无套路领取!!
获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料
鸿蒙(HarmonyOS NEXT)最新学习路线




  •  HarmonOS根本技能



  • HarmonOS就业必备技能 

  •  HarmonOS多媒体技术



  • 鸿蒙NaPi组件进阶



  • HarmonOS高级技能



  • 初识HarmonOS内核 

  • 实战就业级设备开辟

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开辟入门教学视频,内容包含:ArkTS、ArkUI、Web开辟、应用模型、资源分类…等知识点。
获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料
《鸿蒙 (OpenHarmony)开辟入门教学视频》


《鸿蒙生态应用开辟V2.0白皮书》


《鸿蒙 (OpenHarmony)开辟根本到实战手册》

OpenHarmony北向、南向开辟环境搭建

 《鸿蒙开辟根本》



  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开辟
  • .……

 《鸿蒙开辟进阶》



  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开辟
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开辟
  • 应用测试
  • DFX面向未来计划
  • 鸿蒙系统移植和裁剪定制
  • ……

《鸿蒙进阶实战》



  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料
总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个寻衅,也是一个时机。只有积极应对变革,不绝学习和提升自己,他们才华在这个厘革的时代中立于不败之地。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

老婆出轨

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表