Skip to content

Commit 74326b6

Browse files
Copilotmattleibow
andcommitted
Add demo exception handling to HybridWebView sample
- Added ThrowException and ThrowExceptionAsync methods to demonstrate new functionality - Added JavaScript functions to test and showcase exception catching - Added UI buttons to trigger exception handling tests - Updated sample to show both error and success scenarios Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
1 parent 177dc53 commit 74326b6

File tree

19 files changed

+66
-46
lines changed

19 files changed

+66
-46
lines changed

src/Controls/samples/Controls.Sample/Pages/Controls/HybridWebViewPage.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ public async Task<SyncReturn> DoAsyncWorkParamsReturn(int i, string s)
153153
Value = i,
154154
};
155155
}
156+
157+
// Demo method that throws an exception to showcase error handling
158+
public void ThrowException()
159+
{
160+
Debug.WriteLine("ThrowException called - about to throw");
161+
throw new InvalidOperationException("This is a test exception thrown from C# code!");
162+
}
163+
164+
// Demo async method that throws an exception
165+
public async Task<string> ThrowExceptionAsync()
166+
{
167+
Debug.WriteLine("ThrowExceptionAsync called - about to throw");
168+
await Task.Delay(100);
169+
throw new ArgumentException("This is an async test exception thrown from C# code!");
170+
}
156171
}
157172

158173
public class SyncReturn

src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@
9393
LogMessage("Invoked DoAsyncWorkParamsReturn, return value: message=" + retValue.Message + ", value=" + retValue.Value);
9494
}
9595

96+
// Demo functions to test exception handling
97+
async function InvokeThrowException() {
98+
LogMessage("Invoking ThrowException");
99+
try {
100+
await window.HybridWebView.InvokeDotNet('ThrowException');
101+
LogMessage("ThrowException unexpectedly succeeded!");
102+
} catch (ex) {
103+
LogMessage("Caught exception from ThrowException: " + ex.message);
104+
LogMessage("Exception type: " + (ex.dotNetErrorType || "Unknown"));
105+
if (ex.dotNetStackTrace) {
106+
LogMessage("Stack trace: " + ex.dotNetStackTrace.substring(0, 200) + "...");
107+
}
108+
}
109+
}
110+
111+
async function InvokeThrowExceptionAsync() {
112+
LogMessage("Invoking ThrowExceptionAsync");
113+
try {
114+
const result = await window.HybridWebView.InvokeDotNet('ThrowExceptionAsync');
115+
LogMessage("ThrowExceptionAsync unexpectedly succeeded with result: " + result);
116+
} catch (ex) {
117+
LogMessage("Caught async exception from ThrowExceptionAsync: " + ex.message);
118+
LogMessage("Exception type: " + (ex.dotNetErrorType || "Unknown"));
119+
}
120+
}
121+
96122
</script>
97123
</head>
98124
<body>
@@ -114,6 +140,11 @@
114140
<button onclick="InvokeDoAsyncWorkReturn()">Call C# async method (no params) and get simple return value</button>
115141
<button onclick="InvokeDoAsyncWorkParamsReturn()">Call C# async method (params) and get complex return value</button>
116142
</div>
143+
<div>
144+
<strong>Exception Handling Tests:</strong>
145+
<button onclick="InvokeThrowException()">Test C# Exception Handling</button>
146+
<button onclick="InvokeThrowExceptionAsync()">Test C# Async Exception Handling</button>
147+
</div>
117148
<div>
118149
Log: <textarea readonly id="messageLog" style="width: 80%; height: 10em;"></textarea>
119150
</div>

src/Core/src/Handlers/Button/ButtonHandler.Android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ static void OnClick(IButton? button, AView? v)
153153

154154
void OnNativeViewFocusChange(object? sender, AView.FocusChangeEventArgs e)
155155
{
156-
if (VirtualView != null)
157-
VirtualView.IsFocused = e.HasFocus;
156+
VirtualView?.IsFocused = e.HasFocus;
158157
}
159158

160159
void OnPlatformViewLayoutChange(object? sender, AView.LayoutChangeEventArgs e)

src/Core/src/Handlers/CheckBox/CheckBoxHandler.Android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public static partial void MapForeground(ICheckBoxHandler handler, ICheckBox che
4646

4747
void OnCheckedChange(object? sender, CompoundButton.CheckedChangeEventArgs e)
4848
{
49-
if (VirtualView != null)
50-
VirtualView.IsChecked = e.IsChecked;
49+
VirtualView?.IsChecked = e.IsChecked;
5150
}
5251
}
5352
}

src/Core/src/Handlers/DatePicker/DatePickerHandler.Android.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ protected virtual DatePickerDialog CreateDatePickerDialog(int year, int month, i
7878
{
7979
var dialog = new DatePickerDialog(Context!, (o, e) =>
8080
{
81-
if (VirtualView is not null)
82-
{
83-
VirtualView.Date = e.Date;
84-
}
81+
VirtualView?.Date = e.Date;
8582
}, year, month, day);
8683

8784
dialog.DismissEvent += OnDialogDismiss;

src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ internal class WindowsListener : Java.Lang.Object, IOnApplyWindowInsetsListener
281281
layoutParams.TopMargin = topInset;
282282
v.LayoutParameters = layoutParams;
283283
}
284-
284+
285285
return WindowInsetsCompat.Consumed;
286286
}
287287
}

src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void MessageReceived(string rawMessage)
197197
catch (Exception ex)
198198
{
199199
MauiContext?.CreateLogger<HybridWebViewHandler>()?.LogError(ex, "An error occurred while invoking a .NET method from JavaScript: {ErrorMessage}", ex.Message);
200-
200+
201201
// Return error response instead of null so JavaScript can handle the error
202202
var errorResult = CreateErrorResult(ex);
203203
var errorJson = JsonSerializer.Serialize(errorResult, HybridWebViewHandlerJsonContext.Default.DotNetInvokeResult);

src/Core/src/Handlers/ImageButton/ImageButtonHandler.Android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public static void MapPadding(IImageButtonHandler handler, IImageButton imageBut
6969

7070
void OnFocusChange(object? sender, View.FocusChangeEventArgs e)
7171
{
72-
if (VirtualView != null)
73-
VirtualView.IsFocused = e.HasFocus;
72+
VirtualView?.IsFocused = e.HasFocus;
7473
}
7574

7675
void OnTouch(object? sender, View.TouchEventArgs e)

src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ public void OnFocusChange(View? v, bool hasFocus)
165165

166166
var virtualView = Handler.VirtualView;
167167

168-
if (virtualView != null)
169-
virtualView.IsFocused = hasFocus;
168+
virtualView?.IsFocused = hasFocus;
170169
}
171170
}
172171
}

src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ void ShowPickerDialog(TimeSpan? time)
133133
_dialog = CreateTimePickerDialog(hour, minute);
134134
_dialog.Show();
135135

136-
if (VirtualView is not null)
137-
{
138-
VirtualView.IsOpen = true;
139-
}
136+
VirtualView?.IsOpen = true;
140137
}
141138

142139
void HidePickerDialog()

0 commit comments

Comments
 (0)