2017-08-15 52 views
1

我是NativeScript的新手,但希望能夠阻止我從用戶點擊背景(特別是Android設備)時關閉而創建的自定義模式。Nativescript角度防止模式關閉背景水龍頭

當使用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屬性,但似乎無法獲取顯示的實際模態的句柄。

回答

2

好了,經過一些試驗和錯誤設法拿出一個解決方案(如果有更好的辦法,請讓我知道)

它,如果我們創建了一個模式對話框利用查看源似乎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); 
      } 
     } 

    }); 
}