Quellcode durchsuchen

Message posting

Tero Paananen vor 4 Jahren
Ursprung
Commit
dc39e2f5c8

+ 11
- 0
windows/ReactNativeWebView/ReactWebView.cpp Datei anzeigen

@@ -91,10 +91,21 @@ namespace winrt::ReactNativeWebView::implementation {
91 91
           auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
92 92
 
93 93
           auto bridge = WebViewBridge::WebBridge(tag);
94
+          bridge.MessagePostedEvent(winrt::auto_revoke, [ref = get_weak()](const int32_t& message) {
95
+            if (auto self = ref.get()) {
96
+              self->OnMessagePosted(message);
97
+            }
98
+          });
99
+
94 100
           webView.AddWebAllowedObject(L"__RN_WEBVIEW_JS_BRIDGE", bridge);
95 101
         }
96 102
     }
97 103
 
104
+    void ReactWebView::OnMessagePosted(const int32_t& message)
105
+    {
106
+
107
+    }
108
+
98 109
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
99 110
         m_reactContext.DispatchEvent(
100 111
             *this,

+ 1
- 0
windows/ReactNativeWebView/ReactWebView.h Datei anzeigen

@@ -30,6 +30,7 @@ namespace winrt::ReactNativeWebView::implementation {
30 30
         void OnNavigationCompleted(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationCompletedEventArgs const& args);
31 31
         void OnNavigationFailed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationFailedEventArgs const& args);
32 32
         void OnScriptNotify(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args);
33
+        void OnMessagePosted(const int32_t& message);
33 34
     };
34 35
 } // namespace winrt::ReactNativeWebView::implementation
35 36
 

+ 15
- 3
windows/WebViewBridge/WebBridge.cpp Datei anzeigen

@@ -6,15 +6,27 @@
6 6
 using namespace WebViewBridge;
7 7
 using namespace Platform;
8 8
 
9
+using namespace Windows::UI::Core;
9 10
 
10 11
 // https://docs.microsoft.com/en-us/windows/uwp/winrt-components/create-a-windows-runtime-component-in-cppwinrt
11 12
 // https://github.com/microsoft/react-native-windows/blob/0.59-legacy/current/ReactWindows/ReactNativeWebViewBridge/WebViewBridge.cs
12
-
13
+// https://github.com/MicrosoftEdge/JSBrowser/blob/master/NativeListener
13 14
 
14 15
 WebBridge::WebBridge(int64 tag)
15 16
 {
16
-  //winrt::hstring w{ L"Hello, World!" };
17
+  m_tag = tag;
18
+  // Must run on App UI thread
19
+  m_dispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher;
17 20
 }
18 21
 
19
-void WebBridge::PostMessage() {
22
+void WebBridge::PostMessage(int message) {
23
+
24
+  
25
+  m_dispatcher->RunAsync(
26
+    CoreDispatcherPriority::Normal,
27
+    ref new DispatchedHandler([this, message]
28
+      {
29
+        MessagePostedEvent(message);
30
+      }));
31
+  
20 32
 }

+ 7
- 4
windows/WebViewBridge/WebBridge.h Datei anzeigen

@@ -4,14 +4,17 @@ using namespace Windows::Foundation;
4 4
 
5 5
 namespace WebViewBridge
6 6
 {
7
+  public delegate void MessagePosted(int message);
8
+
7 9
   [Windows::Foundation::Metadata::AllowForWebAttribute]
8 10
   public ref class WebBridge sealed
9 11
     {
10 12
     public:
11 13
       WebBridge(int64 tag);
12
-      void PostMessage();
14
+      void PostMessage(int message);
15
+      event MessagePosted^ MessagePostedEvent;
16
+    private:
17
+      Windows::UI::Core::CoreDispatcher^ m_dispatcher;
18
+      int64 m_tag;
13 19
     };
14 20
 }
15
-
16
-
17
-// https://github.com/MicrosoftEdge/JSBrowser/blob/master/NativeListener/KeyHandler.h