1、HarmonyOS 如何获取到页面堆栈对象?
在通过 router 如何获取当前页面层级的情况,以及获取页面层级对象,例如 页面 A - B - C ,在C 页面是可以获取到当前页面层级的list,并且获取到对应页面对象
目前只能获取到router页面栈内当前页面的信息如index,路径,页面名称,和页面站内页面的数量,暂不支持获取到页面栈的list。
现在模块内和模块间的跳转都会能用navigation实现的都尽量用navigation实现,因为后续router会停止演进,后续可能不会新增能力。模块间的跳转,用navigation导致的耦合紧密的问题,可以通过动态import来解决。目前router不支持对页面栈进行精细操作,在清除栈方面,如A-B-B1不支持在B1页面时清除B页面,只支持使用claer清理掉当前页面以外的所有页面。router不支持跳过最大限度32.页面栈的最大容量为32个页面。router路由栈只支持获取栈顶页面属性,仅包含当前页面路径,页面文件名。navigation后续会继续演进,如果有能力为得到满足可以继续规划需求。navigation中可以通过getAllPathName 获取栈中所有NavDestination页面的名称,(详细可参考如下链接下的示例2:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#getallpathname10)
// Index.ets
@Entry
@Component
struct NavigationExample {
pageInfos: NavPathStack = new NavPathStack()
isUseInterception: boolean = false;
registerInterception() {
this.pageInfos.setInterception({
// 页面跳转前拦截,允许操作栈,在当前跳转中生效。
willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar",
operation: NavigationOperation, animated: boolean) => {
if (!this.isUseInterception) {
return;
}
if (typeof to === "string") {
console.log("target page is navigation home");
return;
}
// 重定向目标页面,更改为pageTwo页面到pageOne页面。
let target: NavDestinationContext = to as NavDestinationContext;
if (target.pathInfo.name === 'pageTwo') {
target.pathStack.pop();
target.pathStack.pushPathByName('pageOne', null);
}
},
// 页面跳转后回调,在该回调中操作栈在下一次跳转中刷新。
didShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar",
operation: NavigationOperation, isAnimated: boolean) => {
if (!this.isUseInterception) {
return;
}
if (typeof from === "string") {
console.log("current transition is from navigation home");
} else {
console.log(`current transition is from ${(from as NavDestinationContext).pathInfo.name}`)
}
if (typeof to === "string") {
console.log("current transition to is navBar");
} else {
console.log(`current transition is to ${(to as NavDestinationContext).pathInfo.name}`);
}
},
// Navigation单双栏显示状态发生变更时触发该回调。
modeChange: (mode: NavigationMode) => {
if (!this.isUseInterception) {
return;
}
console.log(`current navigation mode is ${mode}`);
}
})
}
build() {
Navigation(this.pageInfos) {
Column() {
Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.pushPath({ name: 'pageOne' }) //将name指定的NavDestination页面信息入栈
})
Button('use interception', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.isUseInterception = !this.isUseInterception;
if (this.isUseInterception) {
this.registerInterception();
} else {
this.pageInfos.setInterception(undefined);
}
})
}
}.title('NavIndex')
}
}
2、HarmonyOS 基于drawing.canvas实现的动画demo?
可参考:https://developer.huawei.com/consumer/cn/blog/topic/03797837883380062
3、HarmonyOS 安全按钮返回权限失败?
安全按钮返回权限失败,result 返回TEMPORARY_AUTHORIZATION_FAILED,如何解决
// 默认参数下,图标、文字、背景都存在
SaveButton().onClick(async (event:ClickEvent, result:SaveButtonOnClickResult) => {
if (result == SaveButtonOnClickResult.SUCCESS) {}
}
使用uri打开文件,可以持续写入内容,写入过程不受时间限制 let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);这个是需要对应权限的,需要在modules.json5中定义对应的权限 { "name": "ohos.permission.WRITE\_MEDIA", "reason": "$string:EntryAbility\_desc", "usedScene": { "abilities": [ "EntryAbility" ], "when": "always" } }
4、HarmonyOS Image组件设置了本地资源文件后,可以动态修改图片的颜色吗?
Image组件设置了本地资源文件后,可以动态修改图片的颜色吗
参考以下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-image-V5#colorfilter9
colorFilter(value: ColorFilter | DrawingColorFilter)
为图像设置颜色滤镜效果。
设置该属性时,renderMode属性设置不生效。
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | ColorFilter | DrawingColorFilter | 是 | 1. 给图像设置颜色滤镜效果,入参为一个的4x5的RGBA转换矩阵。 矩阵第一行表示R(红色)的向量值,第二行表示G(绿色)的向量值,第三行表示B(蓝色)的向量值,第四行表示A(透明度)的向量值,4行分别代表不同的RGBA的向量值。 当矩阵对角线值为1,其余值为0时,保持图片原有色彩。 计算规则: 如果输入的滤镜矩阵为: ![]() 像素点为 [R, G, B, A] 则过滤后的颜色为 [R’, G’, B’, A’] ![]() 2. 从API Version12开始支持 @ohos.graphics.drawing 的ColorFilter类型作为入参。说明: API Version 11及之前,svg类型图源不支持该属性。 从API version 12开始,该接口中的DrawingColorfilter类型支持在元服务中使用。其中,svg类型的图源需具有stroke属性。 |
5、HarmonyOS 如何设置popup 只在当前页面展示?
正在使用popup 作为一个收到通知一个弹层提示,A页面 show出popup, A页已经跳转到B页面了,在显示 popup时,popup会出现在B页面上,是否可以设置仅在A页面展示
popup目前行为确实如此应用可以使用CustomDialog来实现,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-common-components-custom-dialog-V5
示例demo:
import router from '@ohos.router';
@CustomDialog
export default struct UserPrivacyDialog {
controller: CustomDialogController = new CustomDialogController({ builder: '' });
cancel: Function = () => {
};
confirm: Function = () => {
};
visible: Visibility = Visibility.None
build() {
Column() {
Text('我是弹窗')
Button('jump')
.onClick(() => {
router.pushUrl({
url: 'pages/Second'
})
}).backgroundColor(0xffffff).fontColor(Color.Red)
}
.margin({ top: 22 })
.justifyContent(FlexAlign.SpaceEvenly)
}
}
@Entry
@Component
struct Index {
@State textValue: string = 'Hello World'
@State visible: Visibility = Visibility.None
build() {
Stack() {
Row() {
Column() {
Button('click')
.onClick(() => {
if (this.visible == Visibility.Visible) {
this.visible = Visibility.None
} else {
this.visible = Visibility.Visible
}
})
.backgroundColor(0x777474)
.fontColor(0x000000)
}
.width('100%')
}
.height('100%')
.backgroundColor(0x885555)
Text('')
.onClick(() => {
if (this.visible == Visibility.Visible) {
this.visible = Visibility.None
} else {
this.visible = Visibility.Visible
}
})
.width('100%')
.height('100%')// 透明度可以自己调节一下
.opacity(0.16)
.backgroundColor(0x000000)
.visibility(this.visible)
Column() {
GridRow({
columns: { xs: 1, sm: 4, md: 8, lg: 12 },
breakpoints: {
value: ["400vp", "600vp", "800vp"],
reference: BreakpointsReference.WindowSize
},
}) {
GridCol({
span: { xs: 1, sm: 2, md: 4, lg: 8 },
offset: { xs: 0, sm: 1, md: 2, lg: 2 }
}) {
Column() {
Flex({ justifyContent: FlexAlign.SpaceAround }) {
UserPrivacyDialog();
}.margin({ bottom: 10 })
}
.backgroundColor(0xffffff)
.visibility(this.visible)
.clip(true)
.borderRadius(20)
}
}
}.width('95%') //设置弹窗宽度
}
}
}
componentUtils.getRectangleById参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-componentutils-V5#componentutilsgetrectanglebyid
getRectangleById(id: string): ComponentInfo
根据组件ID获取组件实例对象, 通过组件实例对象将获取的坐标位置和大小同步返回给开发者。