Nativescript Angular 防止在后台点击时关闭模态

Bre*_*all 1 angularjs nativescript

我对 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);
});
Run Code Online (Sandbox Code Playgroud)

但是当使用选项显示我的自定义模式时,ModalDialogService没有可以在选项中设置的属性,目前我有

let modalOptions: ModalDialogOptions = {
    context: {},
    fullscreen: false,
    viewContainerRef: this.vcRef,
};

this.modalService.showModal(DeviceRegistrationComponent, modalOptions)
     .then(result => {
         if (result != null) {
            this.handleDeviceOtpEntry(result.otp);
         }
     });
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?我不介意必须设置特定的原生 android 属性,但似乎无法获得显示的实际模式的句柄。

Bre*_*all 5

好的,经过一些试验和错误设法想出了一个解决方案(如果有更好的方法请告诉我)

从源代码看来,如果我们使用ModalDialogService没有方便的方法来cancelable为 android设置模式对话框,那么在我的模式组件中,我会监听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);
            }
        }

    });
}
Run Code Online (Sandbox Code Playgroud)