Skip to content

Commit

Permalink
HybridWebView update (#540)
Browse files Browse the repository at this point in the history
* Call .NET methods from JS

* Simplify code.
  • Loading branch information
davidbritch authored Nov 14, 2024
1 parent c4d697f commit 086ffb4
Showing 1 changed file with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class MainPage : ContentPage
public MainPage()
{
InitializeComponent();
hybridWebView.SetInvokeJavaScriptTarget<DotNetMethods>(new DotNetMethods(this));
hybridWebView.SetInvokeJavaScriptTarget(this);
}

private void OnSendMessageButtonClicked(object sender, EventArgs e)
Expand Down Expand Up @@ -61,6 +61,32 @@ private void hybridWebView_RawMessageReceived(object sender, HybridWebViewRawMes
Dispatcher.Dispatch(() => editor.Text += Environment.NewLine + e.Message);
}

public void DoSyncWork()
{
Debug.WriteLine("DoSyncWork");
}

public void DoSyncWorkParams(int i, string s)
{
Debug.WriteLine($"DoSyncWorkParams: {i}, {s}");
}

public string DoSyncWorkReturn()
{
Debug.WriteLine("DoSyncWorkReturn");
return "Hello from C#!";
}

public SyncReturn DoSyncWorkParamsReturn(int i, string s)
{
Debug.WriteLine($"DoSyncWorkParamReturn: {i}, {s}");
return new SyncReturn
{
Message = "Hello from C#!" + s,
Value = i
};
}

public class ComputationResult
{
public double result { get; set; }
Expand All @@ -78,42 +104,6 @@ internal partial class HybridSampleJSContext : JsonSerializerContext
// for trimmed builds.
}

private class DotNetMethods
{
MainPage _mainPage;

public DotNetMethods(MainPage mainPage)
{
_mainPage = mainPage;
}

public void DoSyncWork()
{
Debug.WriteLine("DoSyncWork");
}

public void DoSyncWorkParams(int i, string s)
{
Debug.WriteLine($"DoSyncWorkParams: {i}, {s}");
}

public string DoSyncWorkReturn()
{
Debug.WriteLine("DoSyncWorkReturn");
return "Hello from C#!";
}

public SyncReturn DoSyncWorkParamsReturn(int i, string s)
{
Debug.WriteLine($"DoSyncWorkParamReturn: {i}, {s}");
return new SyncReturn
{
Message = "Hello from C#!" + s,
Value = i
};
}
}

public class SyncReturn
{
public string? Message { get; set; }
Expand Down

0 comments on commit 086ffb4

Please sign in to comment.