Lynx

GlobalPropsUpdatedObserver

一个协议(或接口),用于接收 GlobalProps 的更新。

语法

iOS

LynxBaseInspectorOwner.h
@protocol GlobalPropsUpdatedObserver <NSObject>

- (void)onGlobalPropsUpdated:(NSDictionary *)props;

@end

Android

GlobalPropsObserver.java
public interface GlobalPropsObserver {
  void onGlobalPropsUpdated(Map globalProps);
}

参数说明

  • globalProps: 更新后的 GlobalProps,包含所有属性的键值对。

使用示例

使用步骤

  1. 实现 GlobalPropsUpdatedObserver
  2. 获取 LynxBaseInspectorOwner 实例
  3. 注册 GlobalPropsUpdatedObserver

代码示例

iOS

TestGlobalPropsUpdatedObserver.h
#import <Lynx/LynxBaseInspectorOwner.h>

@interface TestGlobalPropsUpdatedObserver : NSObject <GlobalPropsUpdatedObserver>
@end
TestGlobalPropsUpdatedObserver.m
#import "TestGlobalPropsUpdatedObserver.h"

@implementation TestGlobalPropsUpdatedObserver

- (void)onGlobalPropsUpdated:(NSDictionary *)props {
  NSLog(@"onGlobalPropsUpdated: %@", props);
}

@end
ViewController.m
#import <Lynx/LynxView.h>
#import <Lynx/LynxBaseInspectorOwner.h>

#import "TestGlobalPropsUpdatedObserver.h"

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // ...

  [lynxView updateGlobalPropsWithDictionary:@{@"key1": @"value1"}];
  id<LynxBaseInspectorOwner> owner = lynxView.baseInspectorOwner;
  TestGlobalPropsUpdatedObserver* observer =
      [[TestGlobalPropsUpdatedObserver alloc] init];
  [owner setGlobalPropsUpdatedObserver:observer];
  [lynxView updateGlobalPropsWithDictionary:@{@"key2": @"value2"}];
}

@end

Android

MainActivity.java
import android.util.Log;

import com.lynx.devtoolwrapper.GlobalPropsObserver;
import com.lynx.devtoolwrapper.LynxBaseInspectorOwner;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...

    registerGlobalPropsUpdatedObserver(lynxView);
  }

  private void registerGlobalPropsUpdatedObserver(LynxView lynxView) {
    LynxBaseInspectorOwner owner = lynxView.getBaseInspectorOwner();
    if (owner == null) {
      return;
    }
    lynxView.updateGlobalProps(new HashMap<String, Object>() {{
      put("key1", "value1");
    }});
    GlobalPropsObserver observer = new GlobalPropsObserver() {
      @Override
      public void onGlobalPropsUpdated(Map globalProps) {
        Log.i("MainActivity", "onGlobalPropsUpdated: " + globalProps.toString());
      }
    };
    owner.registerGlobalPropsUpdatedObserver(observer);
    lynxView.updateGlobalProps(new HashMap<String, Object>() {{
      put("key2", "value2");
    }});
  }
}

兼容性

LCD tables only load in the browser

除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。