From 39b5262d7f4a04978369cf91501674c8364ea2bf Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 14 Jun 2025 20:13:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E5=BC=83API=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/common/network/ConnectionUtils.ets | 9 ++++----- .../entry/src/main/ets/entryability/EntryAbility.ets | 3 ++- .../03_Complete/entry/src/main/ets/pages/LoginPage.ets | 6 ++---- .../03_Complete/entry/src/main/ets/view/Home.ets | 2 +- .../03_Complete/entry/src/main/ets/view/test.ets | 2 +- .../src/main/ets/common/network/ConnectionUtils.ets | 9 ++++----- .../entry/src/main/ets/entryability/EntryAbility.ets | 3 ++- .../04_Complete/entry/src/main/ets/pages/GoalPage.ets | 2 +- .../04_Complete/entry/src/main/ets/pages/LoginPage.ets | 4 ++-- .../04_Complete/entry/src/main/ets/view/Home.ets | 2 +- .../src/main/ets/common/network/ConnectionUtils.ets | 9 ++++----- .../entry/src/main/ets/entryability/EntryAbility.ets | 3 ++- .../entry/src/main/ets/pages/LoginPage.ets | 6 ++---- .../04_StartingPoint/entry/src/main/ets/view/Home.ets | 2 +- .../04_StartingPoint/entry/src/main/ets/view/test.ets | 2 +- 15 files changed, 30 insertions(+), 34 deletions(-) diff --git a/PageAndData03/03_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets b/PageAndData03/03_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets index e2ca4d9..8197948 100644 --- a/PageAndData03/03_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets +++ b/PageAndData03/03_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets @@ -15,7 +15,6 @@ import { connection } from '@kit.NetworkKit'; import { BusinessError } from '@kit.BasicServicesKit'; -import { promptAction } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG: string = 'ConnectionUtils'; @@ -71,23 +70,23 @@ export class ConnectionUtils { /** *The method of listening to the network status. */ - registerNetworkAvailableStatus() { + registerNetworkAvailableStatus(context: UIContext) { this.netConnection.on('netAvailable', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Available'), duration: 2000 }); }); this.netConnection.on('netUnavailable', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Unavailable'), duration: 2000 }); }); this.netConnection.on('netLost', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Lost'), duration: 2000 }); diff --git a/PageAndData03/03_Complete/entry/src/main/ets/entryability/EntryAbility.ets b/PageAndData03/03_Complete/entry/src/main/ets/entryability/EntryAbility.ets index 4a69eff..c822fc3 100644 --- a/PageAndData03/03_Complete/entry/src/main/ets/entryability/EntryAbility.ets +++ b/PageAndData03/03_Complete/entry/src/main/ets/entryability/EntryAbility.ets @@ -49,7 +49,8 @@ export default class entryAbility extends UIAbility { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } - ConnectionUtils.registerNetworkAvailableStatus(); + let context: UIContext = windowStage.getMainWindowSync().getUIContext(); + ConnectionUtils.registerNetworkAvailableStatus(context); hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); diff --git a/PageAndData03/03_Complete/entry/src/main/ets/pages/LoginPage.ets b/PageAndData03/03_Complete/entry/src/main/ets/pages/LoginPage.ets index e736b5a..c2325aa 100644 --- a/PageAndData03/03_Complete/entry/src/main/ets/pages/LoginPage.ets +++ b/PageAndData03/03_Complete/entry/src/main/ets/pages/LoginPage.ets @@ -13,9 +13,7 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; import ConnectionUtils from '../common/network/ConnectionUtils'; -import { MainPage } from './MainPage'; @Extend(TextInput) function inputStyle() { @@ -65,7 +63,7 @@ struct LoginPage { login(result: boolean): void { if (this.account === '' || this.password === '') { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.input_empty_tips') }); } else { @@ -78,7 +76,7 @@ struct LoginPage { if (result) { this.pathStack.pushPathByName('MainPage', null); } else { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.Network_Lost') }); } diff --git a/PageAndData03/03_Complete/entry/src/main/ets/view/Home.ets b/PageAndData03/03_Complete/entry/src/main/ets/view/Home.ets index f5fbc66..221ac1a 100644 --- a/PageAndData03/03_Complete/entry/src/main/ets/view/Home.ets +++ b/PageAndData03/03_Complete/entry/src/main/ets/view/Home.ets @@ -28,7 +28,7 @@ export default struct Home { @State httpGridItems: Array = []; @State pictureUri: string = ''; private swiperController: SwiperController = new SwiperController(); - private context = getContext(this) as common.UIAbilityContext; + private context = this.getUIContext().getHostContext() as common.UIAbilityContext; private applicationContext = this.context.getApplicationContext(); private cacheDir = this.applicationContext.filesDir; diff --git a/PageAndData03/03_Complete/entry/src/main/ets/view/test.ets b/PageAndData03/03_Complete/entry/src/main/ets/view/test.ets index 74041da..a120ae7 100644 --- a/PageAndData03/03_Complete/entry/src/main/ets/view/test.ets +++ b/PageAndData03/03_Complete/entry/src/main/ets/view/test.ets @@ -25,7 +25,7 @@ export default struct Home { @State httpGridItems: Array = []; @State pictureUri: string = ''; private swiperController: SwiperController = new SwiperController(); - private context = getContext(this) as common.UIAbilityContext; + private context = this.getUIContext().getHostContext() as common.UIAbilityContext; private applicationContext = this.context.getApplicationContext(); private cacheDir = this.applicationContext.filesDir; diff --git a/PageAndData04/04_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets b/PageAndData04/04_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets index e2ca4d9..8197948 100644 --- a/PageAndData04/04_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets +++ b/PageAndData04/04_Complete/entry/src/main/ets/common/network/ConnectionUtils.ets @@ -15,7 +15,6 @@ import { connection } from '@kit.NetworkKit'; import { BusinessError } from '@kit.BasicServicesKit'; -import { promptAction } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG: string = 'ConnectionUtils'; @@ -71,23 +70,23 @@ export class ConnectionUtils { /** *The method of listening to the network status. */ - registerNetworkAvailableStatus() { + registerNetworkAvailableStatus(context: UIContext) { this.netConnection.on('netAvailable', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Available'), duration: 2000 }); }); this.netConnection.on('netUnavailable', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Unavailable'), duration: 2000 }); }); this.netConnection.on('netLost', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Lost'), duration: 2000 }); diff --git a/PageAndData04/04_Complete/entry/src/main/ets/entryability/EntryAbility.ets b/PageAndData04/04_Complete/entry/src/main/ets/entryability/EntryAbility.ets index d113d5a..b3294e9 100644 --- a/PageAndData04/04_Complete/entry/src/main/ets/entryability/EntryAbility.ets +++ b/PageAndData04/04_Complete/entry/src/main/ets/entryability/EntryAbility.ets @@ -53,7 +53,8 @@ export default class entryAbility extends UIAbility { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } - ConnectionUtils.registerNetworkAvailableStatus(); + let context: UIContext = windowStage.getMainWindowSync().getUIContext(); + ConnectionUtils.registerNetworkAvailableStatus(context); hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); diff --git a/PageAndData04/04_Complete/entry/src/main/ets/pages/GoalPage.ets b/PageAndData04/04_Complete/entry/src/main/ets/pages/GoalPage.ets index e09d53b..de1dddc 100644 --- a/PageAndData04/04_Complete/entry/src/main/ets/pages/GoalPage.ets +++ b/PageAndData04/04_Complete/entry/src/main/ets/pages/GoalPage.ets @@ -333,7 +333,7 @@ export struct GoalPage { .width('40vp') .margin({ right: '16vp' }) .onClick(() => { - AlertDialog.show( + this.getUIContext().showAlertDialog( { message: $r('app.string.delete_question'), autoCancel: true, diff --git a/PageAndData04/04_Complete/entry/src/main/ets/pages/LoginPage.ets b/PageAndData04/04_Complete/entry/src/main/ets/pages/LoginPage.ets index 2f543a4..62ff5c8 100644 --- a/PageAndData04/04_Complete/entry/src/main/ets/pages/LoginPage.ets +++ b/PageAndData04/04_Complete/entry/src/main/ets/pages/LoginPage.ets @@ -64,7 +64,7 @@ struct LoginPage { login(result: boolean): void { if (this.account === '' || this.password === '') { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.input_empty_tips') }); } else { @@ -77,7 +77,7 @@ struct LoginPage { if (result) { this.pathStack.pushPathByName('MainPage', null); } else { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.Network_Lost') }); } diff --git a/PageAndData04/04_Complete/entry/src/main/ets/view/Home.ets b/PageAndData04/04_Complete/entry/src/main/ets/view/Home.ets index 1f23a39..e0b9aee 100644 --- a/PageAndData04/04_Complete/entry/src/main/ets/view/Home.ets +++ b/PageAndData04/04_Complete/entry/src/main/ets/view/Home.ets @@ -30,7 +30,7 @@ export default struct Home { @State pictureUri: string = ''; @Link pathStack: NavPathStack; private swiperController: SwiperController = new SwiperController(); - context = getContext(this) as common.UIAbilityContext; + private context = this.getUIContext().getHostContext() as common.UIAbilityContext; applicationContext = this.context.getApplicationContext(); cacheDir = this.applicationContext.filesDir; diff --git a/PageAndData04/04_StartingPoint/entry/src/main/ets/common/network/ConnectionUtils.ets b/PageAndData04/04_StartingPoint/entry/src/main/ets/common/network/ConnectionUtils.ets index e2ca4d9..8197948 100644 --- a/PageAndData04/04_StartingPoint/entry/src/main/ets/common/network/ConnectionUtils.ets +++ b/PageAndData04/04_StartingPoint/entry/src/main/ets/common/network/ConnectionUtils.ets @@ -15,7 +15,6 @@ import { connection } from '@kit.NetworkKit'; import { BusinessError } from '@kit.BasicServicesKit'; -import { promptAction } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG: string = 'ConnectionUtils'; @@ -71,23 +70,23 @@ export class ConnectionUtils { /** *The method of listening to the network status. */ - registerNetworkAvailableStatus() { + registerNetworkAvailableStatus(context: UIContext) { this.netConnection.on('netAvailable', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Available'), duration: 2000 }); }); this.netConnection.on('netUnavailable', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Unavailable'), duration: 2000 }); }); this.netConnection.on('netLost', () => { - promptAction.showToast({ + context.getPromptAction().showToast({ message: $r('app.string.Network_Lost'), duration: 2000 }); diff --git a/PageAndData04/04_StartingPoint/entry/src/main/ets/entryability/EntryAbility.ets b/PageAndData04/04_StartingPoint/entry/src/main/ets/entryability/EntryAbility.ets index 4a69eff..c822fc3 100644 --- a/PageAndData04/04_StartingPoint/entry/src/main/ets/entryability/EntryAbility.ets +++ b/PageAndData04/04_StartingPoint/entry/src/main/ets/entryability/EntryAbility.ets @@ -49,7 +49,8 @@ export default class entryAbility extends UIAbility { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } - ConnectionUtils.registerNetworkAvailableStatus(); + let context: UIContext = windowStage.getMainWindowSync().getUIContext(); + ConnectionUtils.registerNetworkAvailableStatus(context); hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); diff --git a/PageAndData04/04_StartingPoint/entry/src/main/ets/pages/LoginPage.ets b/PageAndData04/04_StartingPoint/entry/src/main/ets/pages/LoginPage.ets index e736b5a..c2325aa 100644 --- a/PageAndData04/04_StartingPoint/entry/src/main/ets/pages/LoginPage.ets +++ b/PageAndData04/04_StartingPoint/entry/src/main/ets/pages/LoginPage.ets @@ -13,9 +13,7 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; import ConnectionUtils from '../common/network/ConnectionUtils'; -import { MainPage } from './MainPage'; @Extend(TextInput) function inputStyle() { @@ -65,7 +63,7 @@ struct LoginPage { login(result: boolean): void { if (this.account === '' || this.password === '') { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.input_empty_tips') }); } else { @@ -78,7 +76,7 @@ struct LoginPage { if (result) { this.pathStack.pushPathByName('MainPage', null); } else { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.Network_Lost') }); } diff --git a/PageAndData04/04_StartingPoint/entry/src/main/ets/view/Home.ets b/PageAndData04/04_StartingPoint/entry/src/main/ets/view/Home.ets index f5fbc66..221ac1a 100644 --- a/PageAndData04/04_StartingPoint/entry/src/main/ets/view/Home.ets +++ b/PageAndData04/04_StartingPoint/entry/src/main/ets/view/Home.ets @@ -28,7 +28,7 @@ export default struct Home { @State httpGridItems: Array = []; @State pictureUri: string = ''; private swiperController: SwiperController = new SwiperController(); - private context = getContext(this) as common.UIAbilityContext; + private context = this.getUIContext().getHostContext() as common.UIAbilityContext; private applicationContext = this.context.getApplicationContext(); private cacheDir = this.applicationContext.filesDir; diff --git a/PageAndData04/04_StartingPoint/entry/src/main/ets/view/test.ets b/PageAndData04/04_StartingPoint/entry/src/main/ets/view/test.ets index 74041da..a120ae7 100644 --- a/PageAndData04/04_StartingPoint/entry/src/main/ets/view/test.ets +++ b/PageAndData04/04_StartingPoint/entry/src/main/ets/view/test.ets @@ -25,7 +25,7 @@ export default struct Home { @State httpGridItems: Array = []; @State pictureUri: string = ''; private swiperController: SwiperController = new SwiperController(); - private context = getContext(this) as common.UIAbilityContext; + private context = this.getUIContext().getHostContext() as common.UIAbilityContext; private applicationContext = this.context.getApplicationContext(); private cacheDir = this.applicationContext.filesDir; -- Gitee