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

104 lines
3.1 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
{
//-------------------------------------------------------------------
// Definitions.
//-------------------------------------------------------------------
// Data members.
public String html = "";
private HtmlWebViewSource htmlSource;
private UrlWebViewSource aboutBlankSource = new UrlWebViewSource() { Url = "about:blank" };
// Managers.
//-------------------------------------------------------------------
// Construction.
public SamWebView()
{
// Events.
Navigating += WebView_Navigating;
Navigated += WebView_Navigated;
// HTML source.
htmlSource = new HtmlWebViewSource() { Html = html };
Source = aboutBlankSource;
}
//-------------------------------------------------------------------
// Properties.
// HTML Panel.
public static String AppActionPrefix => "App:";
public String Html
{
get { return html; }
set
{
Source = aboutBlankSource;
htmlSource.Html = null;
html = value;
htmlSource.Html = html;
Source = htmlSource;
}
}
//-------------------------------------------------------------------
// N a v i g a t i o n
//-------------------------------------------------------------------
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 (!location.StartsWithCI(AppActionPrefix)) return "";
return location.Right(location.Length - AppActionPrefix.Length);
}
//-------------------------------------------------------------------
// C o n t r o l E v e n t s
//-------------------------------------------------------------------
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;
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}