소스 검색

Bribge works

Tero Paananen 4 년 전
부모
커밋
a08df15ac8

+ 5
- 10
windows/ReactNativeWebView/ReactWebView.cpp 파일 보기

@@ -35,7 +35,7 @@ namespace winrt::ReactNativeWebView::implementation {
35 35
     ReactWebView::~ReactWebView()
36 36
     {
37 37
       if (m_messagingEnabled) {
38
-        //m_webBridge.MessagePostedEvent(m_message_token);
38
+        m_webBridge.MessagePostEvent(m_message_token);
39 39
       }
40 40
     }
41 41
 
@@ -86,16 +86,11 @@ namespace winrt::ReactNativeWebView::implementation {
86 86
             });
87 87
 
88 88
         if (m_messagingEnabled) {
89
-          auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
90
-          /*
91
-          m_webBridge = WebViewBridge::WebBridge(tag);
92
-          m_message_token = m_webBridge.MessagePostedEvent([this](hstring const& message) {
93
-            this->OnMessagePosted(message);
94
-          });
95
-          webView.AddWebAllowedObject(L"__REACT_WEB_VIEW_BRIDGE", m_webBridge);
96
-          */
97
-
98 89
           m_webBridge = WebBridge();
90
+          m_message_token = m_webBridge.MessagePostEvent([this](const auto&, hstring const& message)
91
+            {
92
+              this->OnMessagePosted(message);
93
+            });
99 94
           webView.AddWebAllowedObject(L"__REACT_WEB_VIEW_BRIDGE", m_webBridge);
100 95
         }
101 96
     }

+ 15
- 1
windows/WebViewBridgeComponent/WebBridge.cpp 파일 보기

@@ -4,5 +4,19 @@
4 4
 
5 5
 namespace winrt::WebViewBridgeComponent::implementation
6 6
 {
7
-  
7
+  winrt::event_token WebBridge::MessagePostEvent(Windows::Foundation::EventHandler<winrt::hstring> const& handler)
8
+  {
9
+    return m_messageEvent.add(handler);
10
+  }
11
+
12
+  void WebBridge::MessagePostEvent(winrt::event_token const& token) noexcept
13
+  {
14
+    m_messageEvent.remove(token);
15
+  }
16
+
17
+  void WebBridge::PostMessage(winrt::hstring message)
18
+  {
19
+    m_messageEvent(*this, message);
20
+  }
21
+
8 22
 }

+ 8
- 0
windows/WebViewBridgeComponent/WebBridge.h 파일 보기

@@ -6,8 +6,16 @@ namespace winrt::WebViewBridgeComponent::implementation
6 6
 {
7 7
     struct WebBridge : WebBridgeT<WebBridge>
8 8
     {
9
+    public:  
9 10
       WebBridge() = default;
11
+      void PostMessage(winrt::hstring message);
12
+      winrt::event_token MessagePostEvent(Windows::Foundation::EventHandler<winrt::hstring> const& handler);
13
+      void MessagePostEvent(winrt::event_token const& token) noexcept;
14
+    private:
15
+      winrt::event<Windows::Foundation::EventHandler<winrt::hstring>> m_messageEvent;
10 16
     };
17
+
18
+    
11 19
 }
12 20
 
13 21
 namespace winrt::WebViewBridgeComponent::factory_implementation

+ 2
- 0
windows/WebViewBridgeComponent/WebBridge.idl 파일 보기

@@ -5,5 +5,7 @@ namespace WebViewBridgeComponent
5 5
     runtimeclass WebBridge
6 6
     {
7 7
         WebBridge();
8
+        void PostMessage(String message);
9
+        event Windows.Foundation.EventHandler<String> MessagePostEvent;
8 10
     }
9 11
 }

+ 0
- 1
windows/WebViewBridgeComponent/WebViewBridgeComponent.vcxproj.filters 파일 보기

@@ -11,7 +11,6 @@
11 11
   </ItemGroup>
12 12
   <ItemGroup>
13 13
     <ClCompile Include="pch.cpp" />
14
-    <ClCompile Include="Class.cpp" />
15 14
     <ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
16 15
   </ItemGroup>
17 16
   <ItemGroup>