我对NativeScript很陌生,但希望能够阻止我创建的自定义模式在用户点击背景时关闭,特别是对于Android设备。
使用NativeScript提供的对话框时,我可以非常轻松地完成此操作,即操作对话框只需在提供的选项中将cancelable
设置为false即可。
let options: ActionOptions = {
title: "Sticky Dialog",
message: "Will not disappear if background tapped",
cancelable: false,
actions: ["Cancel", "OK"]
};
dialogs.action(options).then(result => {
console.log(result);
});
但是当使用ModalDialogService
显示我的自定义模态时,没有可以在选项中设置的此类属性,目前我有
let modalOptions: ModalDialogOptions = {
context: {},
fullscreen: false,
viewContainerRef: this.vcRef,
};
this.modalService.showModal(DeviceRegistrationComponent, modalOptions)
.then(result => {
if (result != null) {
this.handleDeviceOtpEntry(result.otp);
}
});
有没有办法实现这个目标?我不介意设置特定的本机android属性,但似乎无法获得显示的实际模式的句柄。
答案 0 :(得分:4)
好的,经过一番试验和错误设法提出解决方案(如果有更好的方法请告诉我)
从源头看来,如果我们使用ModalDialogService
创建一个模态对话框,没有方便的方法为android设置cancelable
,所以相反,在我的模态组件中我会听取shownModally
事件,一旦触发,我们就可以尝试查找当前显示的片段,该片段由NativeScript使用标记dialog
创建。
一旦我们有了,我们就可以简单地设置原生属性。
import {Component} from "@angular/core";
import {Page} from "tns-core-modules/ui/page";
import {ModalDialogParams} from "nativescript-angular";
import * as application from "tns-core-modules/application";
import {isAndroid} from "tns-core-modules/platform";
@Component({
moduleId: module.id,
selector: "app-device-registration",
templateUrl: "./device.registration.component.html",
styleUrls: ["./device.registration.component.css"]
})
export class DeviceRegistrationComponent {
constructor(private params: ModalDialogParams, private page: Page) {
this.page.on("shownModally", data => {
if (isAndroid) {
let fragmentManger = application.android.foregroundActivity.getFragmentManager();
let dialogFragment = fragmentManger.findFragmentByTag("dialog");
if (dialogFragment !== null) {
dialogFragment.setCancelable(false);
}
}
});
}
答案 1 :(得分:1)
在使用NativeScript 5.0时,我已使用此代码,将事件处理程序添加到html文件中
const Blazy = require('blazy')
并将此代码添加到您的打字稿文件中
<GridLayout (loaded)="onLoaded($event)">
答案 2 :(得分:0)
快速前进到2018年并在NS 4上发布,Brendon的答案不再起作用,因此,这里是更新的代码段,执行相同的操作。
export class ModalViewComponent implements OnInit {
constructor(
private _routerExtensions: RouterExtensions,
private _activeRoute: ActivatedRoute,
private params: ModalDialogParams,
private frame: Frame) { }
ngOnInit(): void {
if (this.frame.android) {
setTimeout(_ => {
let activity = this.frame.android.currentActivity, fm = activity.getFragmentManager(), fragment = fm.findFragmentById(0);
if (fragment) fragment.setCancelable(false)
},10)
}
}
}