Files
Common-VS2019/SacsMobileMaui/Maui/Controls/SamWebView.cs
T

105 lines
2.5 KiB
C#

using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sarand.Net.Mobile.Maui.Controls
{
public class SamWebView : WebView, ISamControl
{
// --- Data Members ---
public String html = "";
private readonly HtmlWebViewSource htmlSource;
private readonly UrlWebViewSource aboutBlankSource = new UrlWebViewSource() { Url = "about:blank" };
// --- Construction ---
public SamWebView()
{
// --- Events ---
Navigating += WebView_Navigating;
Navigated += WebView_Navigated;
// --- HTML source ---
htmlSource = new HtmlWebViewSource() { Html = html };
Source = aboutBlankSource;
}
// --- Properties ---
public static String AppActionPrefix => "App:";
/// <summary>Gets or sets the HTML displayed by the web view.</summary>
public String Html
{
get { return html; }
set
{
Source = aboutBlankSource;
htmlSource.Html = null;
html = value;
htmlSource.Html = html;
Source = htmlSource;
}
}
// --- Navigation ---
public class AppNavigatingEventArgs : EventArgs
{
public AppNavigatingEventArgs(String appAction)
{
this.AppAction = appAction;
}
public String AppAction { get; }
}
public delegate void AppNavigatingEventHandler(object? sender, AppNavigatingEventArgs e);
public event AppNavigatingEventHandler? AppNavigating;
private void OnAppNavigating(String appAction)
{
if (AppNavigating != null) AppNavigating.Invoke(this, new AppNavigatingEventArgs(appAction));
}
private String GetAppAction(String location)
{
if (String.IsNullOrEmpty(location) || !location.StartsWithCI(AppActionPrefix)) return "";
return location.Right(location.Length - AppActionPrefix.Length);
}
// --- Control events ---
private void WebView_Navigating(object? sender, WebNavigatingEventArgs e)
{
String appAction = GetAppAction(e.Url);
SaLogger.Debug("SamWebView_Navigating URL: " + e.Url + ". App action: " + appAction);
if (!StringUtil.IsEmpty(appAction))
{
e.Cancel = true;
OnAppNavigating(appAction);
}
}
private void WebView_Navigated(object? sender, WebNavigatedEventArgs e)
{
SaLogger.Debug("SamWebView_Navigated URL: " + e.Url);
String appAction = GetAppAction(e.Url);
if (!StringUtil.IsEmpty(appAction))
{
Source = htmlSource;
}
}
// --- Component state ---
public void SetControlActivity(bool enabled)
{
if (IsEnabled != enabled) IsEnabled = enabled;
}
// --- The End ---
}
}