Skip to content

Commit ed9618c

Browse files
committed
HybridWebView - JS Interop
1 parent 050db1a commit ed9618c

File tree

7 files changed

+91
-46
lines changed

7 files changed

+91
-46
lines changed
+9-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
namespace HybridWebViewApp
1+
namespace HybridWebViewApp;
2+
3+
public partial class App : Application
24
{
3-
public partial class App : Application
5+
public App()
46
{
5-
public App()
6-
{
7-
InitializeComponent();
8-
UserAppTheme = PlatformAppTheme;
9-
}
10-
11-
protected override Window CreateWindow(IActivationState? activationState)
12-
=> new MainWindow();
7+
InitializeComponent();
8+
UserAppTheme = PlatformAppTheme;
139
}
10+
11+
protected override Window CreateWindow(IActivationState? activationState)
12+
=> new MainWindow();
1413
}

src/NET_9/HybridWebViewApp/Imports.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
global using HybridWebViewApp;
2+
global using HybridWebViewApp.Models;
23
global using HybridWebViewApp.Views;
34

45
// Static
+5-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
namespace HybridWebViewApp
1+
namespace HybridWebViewApp;
2+
3+
public partial class MainWindow : Window
24
{
3-
public partial class MainWindow : Window
4-
{
5-
public MainWindow()
6-
{
7-
InitializeComponent();
8-
}
5+
public MainWindow() => InitializeComponent();
96

10-
public MainWindow(Page page) : base(page)
11-
{
12-
InitializeComponent();
13-
}
14-
}
7+
public MainWindow(Page page) : base(page) => InitializeComponent();
158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace HybridWebViewApp.Models;
4+
5+
//[JsonSourceGenerationOptions(WriteIndented = true)]
6+
[JsonSerializable(typeof(int))]
7+
internal partial class IntContext : JsonSerializerContext;

src/NET_9/HybridWebViewApp/Resources/Raw/wwwroot/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ <h3>HybridWebView App!</h3>
2121
Messages from .NET: <textarea readonly id="msgFromCS" style="width: 80%; height: 300px;"></textarea>
2222
</div>
2323
<script>
24+
function addNumbers(num1, num2) {
25+
return num1 + num2;
26+
}
27+
2428
window.addEventListener(
2529
"HybridWebViewMessageReceived",
2630
function(e) {

src/NET_9/HybridWebViewApp/Views/MainPage.xaml

+27-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,35 @@
1010
mc:Ignorable="d">
1111
<ContentPage.Content>
1212
<ScrollView>
13-
<Grid RowDefinitions="Auto,*,40">
13+
<Grid RowDefinitions="Auto,Auto,*,40">
1414
<HorizontalStackLayout
15+
Margin="0,20,0,0"
16+
HorizontalOptions="Center">
17+
<Label
18+
FontAttributes="Bold"
19+
Text="JS Interop:"
20+
VerticalOptions="Center" />
21+
<Entry
22+
x:Name="operand1"
23+
Placeholder="Number 1"
24+
WidthRequest="100" />
25+
<Entry
26+
x:Name="operand2"
27+
Placeholder="Number 2"
28+
WidthRequest="100" />
29+
<Button
30+
Clicked="OnAddClicked"
31+
Text="Add" />
32+
</HorizontalStackLayout>
33+
<HorizontalStackLayout
34+
Grid.Row="1"
1535
Margin="0,20,0,0"
1636
HorizontalOptions="Center"
1737
VerticalOptions="Center">
38+
<Label
39+
FontAttributes="Bold"
40+
Text="Messaging:"
41+
VerticalOptions="Center" />
1842
<Entry
1943
x:Name="message"
2044
ClearButtonVisibility="WhileEditing"
@@ -30,10 +54,10 @@
3054
</HorizontalStackLayout>
3155
<HybridWebView
3256
x:Name="hwv"
33-
Grid.Row="1"
57+
Grid.Row="2"
3458
RawMessageReceived="OnRawMessageReceived" />
3559
<Grid
36-
Grid.Row="2"
60+
Grid.Row="3"
3761
BackgroundColor="{AppThemeBinding Dark={StaticResource BackgroundDark},
3862
Light={StaticResource Primary}}">
3963

Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
using System.Reflection;
22

3-
namespace HybridWebViewApp.Views
3+
namespace HybridWebViewApp.Views;
4+
5+
public partial class MainPage : ContentPage
46
{
5-
public partial class MainPage : ContentPage
7+
public MainPage()
68
{
7-
public MainPage()
8-
{
9-
InitializeComponent();
10-
message.ReturnCommand = new Command(SendMessage);
11-
var version = typeof(MauiApp).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
12-
versionLabel.Text = $".NET MAUI ver. {version?[..version.IndexOf('+')]}";
13-
}
9+
InitializeComponent();
10+
message.ReturnCommand = new Command(SendMessage);
11+
var version = typeof(MauiApp).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
12+
versionLabel.Text = $".NET MAUI ver. {version?[..version.IndexOf('+')]}";
13+
}
1414

15-
private void OnSendClicked(object sender, EventArgs e) => SendMessage();
15+
private void OnSendClicked(object sender, EventArgs e) => SendMessage();
1616

17-
private async void OnRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
18-
{
19-
await DisplayAlert("Raw Message Received", e.Message, "OK");
20-
}
17+
private async void OnRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
18+
=> await DisplayAlert("Raw Message Received", e.Message, "OK");
19+
20+
private void OnMessageChanged(object sender, TextChangedEventArgs e)
21+
=> send.IsEnabled = message.Text.Length > 0;
2122

22-
private void OnMessageChanged(object sender, TextChangedEventArgs e)
23+
private async void OnAddClicked(object sender, EventArgs e)
24+
{
25+
if (int.TryParse(operand1.Text, out var num1)
26+
&& int.TryParse(operand2.Text, out var num2))
2327
{
24-
send.IsEnabled = message.Text.Length > 0;
25-
}
28+
var result = await hwv.InvokeJavaScriptAsync<int>(
29+
"addNumbers", // Method name
30+
IntContext.Default.Int32, // Return type
31+
[num1, num2], // Input parameter values
32+
[IntContext.Default.Int32, IntContext.Default.Int32]); // Input parameter types
33+
await DisplayAlert("Result", $"{result}", "OK");
2634

27-
private void SendMessage()
35+
operand1.Text = string.Empty;
36+
operand2.Text = string.Empty;
37+
operand1.Focus();
38+
}
39+
else
2840
{
29-
hwv.SendRawMessage(message.Text);
30-
message.Text = string.Empty;
31-
message.Focus();
41+
await DisplayAlert("Error", "Invalid input.", "OK");
3242
}
3343
}
44+
45+
private void SendMessage()
46+
{
47+
hwv.SendRawMessage(message.Text);
48+
message.Text = string.Empty;
49+
message.Focus();
50+
}
3451
}

0 commit comments

Comments
 (0)