如何在Page中获取WindowStage实例
=======================

方式一：在onWindowStageCreate方法中获取，此方式适用于Ability生命周期内需要持久化WindowStage实例的场景。

import { UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';

export default class EntryAbility extends UIAbility {
  _// ..._
  onWindowStageCreate(windowStage: window.WindowStage): void {
  _  // Main window is created, set main page for this ability_
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
    });
    console.info('windowStage', JSON.stringify(windowStage))
   _ // Store windowStage instance globally for cross-page access_
    AppStorage.setAndLink('windowStage', windowStage)
  }

 _ // ..._
}

方式二：UIAbilityContext提供了获取WindowStage实例的接口，此方式适用于需要动态获取WindowStage的页面级场景，无需持久化存储。

_// Index.ets_
import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {
  @State showAbility: string = 'get windowStage'

  build() {
    Row() {
      Column() {
        Text(this.showAbility)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
            console.info('Obtained WindowStage instance:',JSON.stringify(context.windowStage))
          });
      }
      .width('100%')
    }
    .height('100%')
  }
}

参考链接

[onWindowStageCreate](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability#onwindowstagecreate)

[UIAbilityContext](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inner-application-uiabilitycontext)