-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from egvijayanand/working
HybridWebView sample updated to .NET MAUI 9 RC1
- Loading branch information
Showing
7 changed files
with
126 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<Window | ||
x:Class="HybridWebViewApp.MainWindow" | ||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:vw="clr-namespace:HybridWebViewApp.Views" | ||
Title="HybridWebViewApp" | ||
mc:Ignorable="d"> | ||
<Window.Page> | ||
<vw:MainPage /> | ||
</Window.Page> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace HybridWebViewApp | ||
{ | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public MainWindow(Page page) : base(page) | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 62 additions & 33 deletions
95
src/NET_9/HybridWebViewApp/Resources/Raw/wwwroot/scripts/hwv.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,78 @@ | ||
function HybridWebViewInit() | ||
{ | ||
function DispatchHybridWebViewMessage(message) { | ||
const event = new CustomEvent("HybridWebViewMessageReceived", { detail: { message: message } }); | ||
window.dispatchEvent(event); | ||
} | ||
window.HybridWebView = { | ||
"Init": function () { | ||
function DispatchHybridWebViewMessage(message) { | ||
const event = new CustomEvent("HybridWebViewMessageReceived", { detail: { message: message } }); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
if (window.chrome && window.chrome.webview) { | ||
// Windows WebView2 | ||
window.chrome.webview.addEventListener('message', arg => { | ||
DispatchHybridWebViewMessage(arg.data); | ||
}); | ||
} | ||
else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.webwindowinterop) { | ||
// iOS and MacCatalyst WKWebView | ||
window.external = { | ||
"receiveMessage": message => { | ||
DispatchHybridWebViewMessage(message); | ||
} | ||
}; | ||
} | ||
else { | ||
// Android WebView | ||
window.addEventListener('message', arg => { | ||
DispatchHybridWebViewMessage(arg.data); | ||
}); | ||
} | ||
} | ||
if (window.chrome && window.chrome.webview) { | ||
// Windows WebView2 | ||
window.chrome.webview.addEventListener('message', arg => { | ||
DispatchHybridWebViewMessage(arg.data); | ||
}); | ||
} | ||
else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.webwindowinterop) { | ||
// iOS and MacCatalyst WKWebView | ||
window.external = { | ||
"receiveMessage": message => { | ||
DispatchHybridWebViewMessage(message); | ||
} | ||
}; | ||
} | ||
else { | ||
// Android WebView | ||
window.addEventListener('message', arg => { | ||
DispatchHybridWebViewMessage(arg.data); | ||
}); | ||
} | ||
}, | ||
|
||
window.HybridWebView = | ||
{ | ||
"SendRawMessage": function (message) { | ||
window.HybridWebView.__SendMessageInternal('RawMessage', message); | ||
}, | ||
|
||
"__SendMessageInternal": function (type, message) { | ||
|
||
const messageToSend = type + '|' + message; | ||
|
||
if (window.chrome && window.chrome.webview) { | ||
// Windows WebView2 | ||
window.chrome.webview.postMessage(message); | ||
window.chrome.webview.postMessage(messageToSend); | ||
} | ||
else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.webwindowinterop) { | ||
// iOS and MacCatalyst WKWebView | ||
window.webkit.messageHandlers.webwindowinterop.postMessage(message); | ||
window.webkit.messageHandlers.webwindowinterop.postMessage(messageToSend); | ||
} | ||
else { | ||
// Android WebView | ||
hybridWebViewHost.sendRawMessage(message); | ||
hybridWebViewHost.sendMessage(messageToSend); | ||
} | ||
}, | ||
|
||
"InvokeMethod": function (taskId, methodName, args) { | ||
if (methodName[Symbol.toStringTag] === 'AsyncFunction') { | ||
// For async methods, we need to call the method and then trigger the callback when it's done | ||
const asyncPromise = methodName(...args); | ||
asyncPromise | ||
.then(asyncResult => { | ||
window.HybridWebView.__TriggerAsyncCallback(taskId, asyncResult); | ||
}) | ||
.catch(error => console.error(error)); | ||
} else { | ||
// For sync methods, we can call the method and trigger the callback immediately | ||
const syncResult = methodName(...args); | ||
window.HybridWebView.__TriggerAsyncCallback(taskId, syncResult); | ||
} | ||
}, | ||
|
||
"__TriggerAsyncCallback": function (taskId, result) { | ||
// Make sure the result is a string | ||
if (result && typeof (result) !== 'string') { | ||
result = JSON.stringify(result); | ||
} | ||
|
||
window.HybridWebView.__SendMessageInternal('InvokeMethodCompleted', taskId + '|' + result); | ||
} | ||
} | ||
|
||
HybridWebViewInit(); | ||
window.HybridWebView.Init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters