-
-
Notifications
You must be signed in to change notification settings - Fork 26
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 #97 from egvijayanand/working
HybridWebView - JS Interop
- Loading branch information
Showing
7 changed files
with
91 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
namespace HybridWebViewApp | ||
namespace HybridWebViewApp; | ||
|
||
public partial class App : Application | ||
{ | ||
public partial class App : Application | ||
public App() | ||
{ | ||
public App() | ||
{ | ||
InitializeComponent(); | ||
UserAppTheme = PlatformAppTheme; | ||
} | ||
|
||
protected override Window CreateWindow(IActivationState? activationState) | ||
=> new MainWindow(); | ||
InitializeComponent(); | ||
UserAppTheme = PlatformAppTheme; | ||
} | ||
|
||
protected override Window CreateWindow(IActivationState? activationState) | ||
=> new MainWindow(); | ||
} |
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 |
---|---|---|
@@ -1,15 +1,8 @@ | ||
namespace HybridWebViewApp | ||
namespace HybridWebViewApp; | ||
|
||
public partial class MainWindow : Window | ||
{ | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
public MainWindow() => InitializeComponent(); | ||
|
||
public MainWindow(Page page) : base(page) | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace HybridWebViewApp.Models; | ||
|
||
//[JsonSourceGenerationOptions(WriteIndented = true)] | ||
[JsonSerializable(typeof(int))] | ||
internal partial class IntContext : JsonSerializerContext; |
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
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,34 +1,51 @@ | ||
using System.Reflection; | ||
|
||
namespace HybridWebViewApp.Views | ||
namespace HybridWebViewApp.Views; | ||
|
||
public partial class MainPage : ContentPage | ||
{ | ||
public partial class MainPage : ContentPage | ||
public MainPage() | ||
{ | ||
public MainPage() | ||
{ | ||
InitializeComponent(); | ||
message.ReturnCommand = new Command(SendMessage); | ||
var version = typeof(MauiApp).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; | ||
versionLabel.Text = $".NET MAUI ver. {version?[..version.IndexOf('+')]}"; | ||
} | ||
InitializeComponent(); | ||
message.ReturnCommand = new Command(SendMessage); | ||
var version = typeof(MauiApp).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; | ||
versionLabel.Text = $".NET MAUI ver. {version?[..version.IndexOf('+')]}"; | ||
} | ||
|
||
private void OnSendClicked(object sender, EventArgs e) => SendMessage(); | ||
private void OnSendClicked(object sender, EventArgs e) => SendMessage(); | ||
|
||
private async void OnRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e) | ||
{ | ||
await DisplayAlert("Raw Message Received", e.Message, "OK"); | ||
} | ||
private async void OnRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e) | ||
=> await DisplayAlert("Raw Message Received", e.Message, "OK"); | ||
|
||
private void OnMessageChanged(object sender, TextChangedEventArgs e) | ||
=> send.IsEnabled = message.Text.Length > 0; | ||
|
||
private void OnMessageChanged(object sender, TextChangedEventArgs e) | ||
private async void OnAddClicked(object sender, EventArgs e) | ||
{ | ||
if (int.TryParse(operand1.Text, out var num1) | ||
&& int.TryParse(operand2.Text, out var num2)) | ||
{ | ||
send.IsEnabled = message.Text.Length > 0; | ||
} | ||
var result = await hwv.InvokeJavaScriptAsync<int>( | ||
"addNumbers", // Method name | ||
IntContext.Default.Int32, // Return type | ||
[num1, num2], // Input parameter values | ||
[IntContext.Default.Int32, IntContext.Default.Int32]); // Input parameter types | ||
await DisplayAlert("Result", $"{result}", "OK"); | ||
|
||
private void SendMessage() | ||
operand1.Text = string.Empty; | ||
operand2.Text = string.Empty; | ||
operand1.Focus(); | ||
} | ||
else | ||
{ | ||
hwv.SendRawMessage(message.Text); | ||
message.Text = string.Empty; | ||
message.Focus(); | ||
await DisplayAlert("Error", "Invalid input.", "OK"); | ||
} | ||
} | ||
|
||
private void SendMessage() | ||
{ | ||
hwv.SendRawMessage(message.Text); | ||
message.Text = string.Empty; | ||
message.Focus(); | ||
} | ||
} |