Skip to content

WebView

Summary

Represents the image chart control.

Signature

1
public class WebView : ControlBase

Namespace

cAlgo.API

Methods

Name Description
NavigateAsync Navigate asynchronously to another URL.
NavigateToStringAsync Initiates a navigation asynchronously to html content as source HTML of a new document.
GoBackAsync Navigates asynchronously the WebView to the previous page in the navigation history.
GoForwardAsync Navigates asynchronously the WebView to the next page in the navigation history.
ReloadAsync Reloads asynchronously the current page.
StopAsync Stops asynchronously all navigations and pending resource fetches.
ExecuteScript Executes JavaScript code from the javaScript parameter in the current top level document rendered in the WebViewand returns the result.You can only use this method after control is fully loaded and navigation is completed.
ExecuteScriptAsync Executes asynchronously JavaScript code from the javaScript parameter in the current top level document rendered inthe WebView.You can only use this method after control is fully loaded and navigation is completed.

Properties

Name Description
DefaultBackgroundColor { get; set; } Default background color of WebView.
IsLoaded { get; } Returns True if WebView is loaded otherwise False.

Events

Name Description
NavigationCompleted Occurs when navigation of a page is completed.
WebMessageReceived Occurs after web content sends a message
Loaded Occurs after web view is loaded
Unloaded Occurs after web view is unloaded

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 using cAlgo.API;
 namespace cAlgo.Robots
 {
     [Robot(AccessRights = AccessRights.None)]
     public class WebViewOnChart : Robot
     {
         private WebView _webView;
         private TextBox _addressTextBox;
         private TextBox _scriptTextBox;
         private Button _executeScriptButton;
         protected override void OnStart()
         {
             var goBackButton = new Button
             {
                 Text = "←",
                 Margin = 3
             };
             goBackButton.Click += OnGoBackButtonClick;
             var goForwardButton = new Button
             {
                 Text = "→",
                 Margin = 3
             };
             goForwardButton.Click += OnGoForwardButtonClick;
             _addressTextBox = new TextBox
             {
                 Text = "https://ctrader.com/",
                 Margin = 3,
                 Width = 150,
                 MinWidth = 150,
                 MaxWidth = 150
             };
             var goButton = new Button
             {
                 Text = "→",
                 Margin = 3
             };
             goButton.Click += OnGoButtonClick;
             var reloadButton = new Button
             {
                 Text = "Reload",
                 Margin = 3
             };
             reloadButton.Click += OnReloadButtonClick;
             var stopButton = new Button
             {
                 Text = "x",
                 Margin = 3
             };
             stopButton.Click += OnStopButtonClick;
             _scriptTextBox = new TextBox
             {
                 Text = "alert('Hi');",
                 Margin = 3,
                 Width = 150,
                 MinWidth = 150,
                 MaxWidth = 150,
                 IsEnabled = false
             };
             _executeScriptButton = new Button
             {
                 Text = "Execute Script",
                 Margin = 3,
                 IsEnabled = false
             };
             _executeScriptButton.Click += OnExecuteScriptButtonClick;
             var addressBarPanel = new StackPanel
             {
                 MaxHeight = 50,
                 VerticalAlignment = VerticalAlignment.Top,
                 BackgroundColor = Color.Black,
                 Orientation = Orientation.Horizontal
             };
             addressBarPanel.AddChild(goBackButton);
             addressBarPanel.AddChild(goForwardButton);
             addressBarPanel.AddChild(_addressTextBox);
             addressBarPanel.AddChild(goButton);
             addressBarPanel.AddChild(reloadButton);
             addressBarPanel.AddChild(stopButton);
             addressBarPanel.AddChild(_scriptTextbox);
             addressBarPanel.AddChild(_executeScriptButton);
             _webView = new WebView
             {
                 DefaultBackgroundColor = Color.Red
             };
             _webView.NavigationCompleted += OnWebViewNavigationCompleted;
             _webView.WebMessageReceived += OnWebViewWebMessageReceived;
             _webView.Loaded += OnWebViewLoaded;
             _webView.Unloaded += OnWebViewUnloaded;
             var mainGrid = new Grid(2, 1);
             mainGrid.Rows[0].SetHeightToAuto();
             mainGrid.Rows[1].SetHeightInStars(1);
             mainGrid.AddChild(addressBarPanel, 0, 0);
             mainGrid.AddChild(_webView, 1, 0);
             Chart.AddControl(mainGrid);
         }
         private void OnWebViewLoaded(WebViewLoadedEventArgs args)
         {
             Print($"Webview loaded, IsLoaded: {args.WebView.IsLoaded}");
             _webView.NavigateAsync(_addressTextBox.Text);
         }
         private void OnWebViewUnloaded(WebViewUnloadedEventArgs args)
         {
             Print($"Webview unloaded, IsLoaded: {args.WebView.IsLoaded}");
         }
         private void OnStopButtonClick(ButtonClickEventArgs args)
         {
             _webView.StopAsync();
         }
         private void OnExecuteScriptButtonClick(ButtonClickEventArgs args)
         {
             var result = _webView.ExecuteScript(_scriptTextBox.Text);
             Print($"IsSuccessful: {result.IsSuccessful} | Json: {result.Json}");
         }
         private void OnReloadButtonClick(ButtonClickEventArgs args)
         {
             _webView.ReloadAsync();
         }
         private void OnGoForwardButtonClick(ButtonClickEventArgs args)
         {
             _webView.GoForwardAsync();
         }
         private void OnGoBackButtonClick(ButtonClickEventArgs args)
         {
             _webView.GoBackAsync();
         }
         private void OnGoButtonClick(ButtonClickEventArgs args)
         {
             _webView.NavigateAsync(_addressTextBox.Text);
         }
         private void OnWebViewWebMessageReceived(WebViewWebMessageReceivedEventArgs args)
         {
             Print($"Source: {args.Source} | Message: {args.Message}");
         }
         private void OnWebViewNavigationCompleted(WebViewNavigationCompletedEventArgs args)
         {
             Print($"{args.HttpStatusCode} | {args.IsSuccessful} | {args.Url}");
             _addressTextBox.Text = args.Url;
             _scriptTextBox.IsEnabled = true;
             _executeScriptButton.IsEnabled = true;
         }
     }
 }
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 using cAlgo.API;
 namespace cAlgo.Robots
 {
     [Robot(AccessRights = AccessRights.None)]
     public class WebViewOnWindow : Robot
     {
         private WebView _webView;
         private TextBox _addressTextBox;
         private TextBox _scriptTextbox;
         private Button _executeScriptButton;
         protected override void OnStart()
         {
             var goBackButton = new Button
             {
                 Text = "←",
                 Margin = 3
             };
             goBackButton.Click += OnGoBackButtonClick;
             var goForwardButton = new Button
             {
                 Text = "→",
                 Margin = 3
             };
             goForwardButton.Click += OnGoForwardButtonClick;
             _addressTextBox = new TextBox
             {
                 Text = "https://ctrader.com/",
                 Margin = 3,
                 Width = 150,
                 MinWidth = 150,
                 MaxWidth = 150
             };
             var goButton = new Button
             {
                 Text = "→",
                 Margin = 3
             };
             goButton.Click += OnGoButtonClick;
             var reloadButton = new Button
             {
                 Text = "Reload",
                 Margin = 3
             };
             reloadButton.Click += OnReloadButtonClick;
             var stopButton = new Button
             {
                 Text = "x",
                 Margin = 3
             };
             stopButton.Click += OnStopButtonClick;
             _scriptTextbox = new TextBox
             {
                 Text = "alert('Hi');",
                 Margin = 3,
                 Width = 150,
                 MinWidth = 150,
                 MaxWidth = 150,
                 IsEnabled = false
             };
             _executeScriptButton = new Button
             {
                 Text = "Execute Script",
                 Margin = 3,
                 IsEnabled = false
             };
             _executeScriptButton.Click += OnExecuteScriptButtonClick;
             var addressBarPanel = new StackPanel
             {
                 MaxHeight = 50,
                 VerticalAlignment = VerticalAlignment.Top,
                 BackgroundColor = Color.Black,
                 Orientation = Orientation.Horizontal
             };
             addressBarPanel.AddChild(goBackButton);
             addressBarPanel.AddChild(goForwardButton);
             addressBarPanel.AddChild(_addressTextBox);
             addressBarPanel.AddChild(goButton);
             addressBarPanel.AddChild(reloadButton);
             addressBarPanel.AddChild(stopButton);
             addressBarPanel.AddChild(_scriptTextbox);
             addressBarPanel.AddChild(_executeScriptButton);
             _webView = new WebView
             {
                 DefaultBackgroundColor = Color.Red
             };
             _webView.NavigationCompleted += OnWebViewNavigationCompleted;
             _webView.WebMessageReceived += OnWebViewWebMessageReceived;
             _webView.Loaded += OnWebViewLoaded;
             _webView.Unloaded += OnWebViewUnloaded;
             var mainGrid = new Grid(2, 1);
             mainGrid.Rows[0].SetHeightToAuto();
             mainGrid.Rows[1].SetHeightInStars(1);
             mainGrid.AddChild(addressBarPanel, 0, 0);
             mainGrid.AddChild(_webView, 1, 0);
             var window = new Window
             {
                 Child = mainGrid
             };
             window.Show();
         }
         private void OnWebViewLoaded(WebViewLoadedEventArgs args)
         {
             Print($"Webview loaded, IsLoaded: {args.WebView.IsLoaded}");
             _webView.NavigateAsync(_addressTextBox.Text);
         }
         private void OnWebViewUnloaded(WebViewUnloadedEventArgs args)
         {
             Print($"Webview unloaded, IsLoaded: {args.WebView.IsLoaded}");
         }
         private void OnStopButtonClick(ButtonClickEventArgs args)
         {
             _webView.StopAsync();
         }
         private void OnExecuteScriptButtonClick(ButtonClickEventArgs args)
         {
             var result = _webView.ExecuteScript(_scriptTextbox.Text);
             Print($"IsSuccessful: {result.IsSuccessful} | Json: {result.Json}");
         }
         private void OnReloadButtonClick(ButtonClickEventArgs args)
         {
             _webView.ReloadAsync();
         }
         private void OnGoForwardButtonClick(ButtonClickEventArgs args)
         {
             _webView.GoForwardAsync();
         }
         private void OnGoBackButtonClick(ButtonClickEventArgs args)
         {
             _webView.GoBackAsync();
         }
         private void OnGoButtonClick(ButtonClickEventArgs args)
         {
             _webView.NavigateAsync(_addressTextBox.Text);
         }
         private void OnWebViewWebMessageReceived(WebViewWebMessageReceivedEventArgs args)
         {
             Print($"Source: {args.Source} | Message: {args.Message}");
         }
         private void OnWebViewNavigationCompleted(WebViewNavigationCompletedEventArgs args)
         {
             Print($"{args.HttpStatusCode} | {args.IsSuccessful} | {args.Url}");
             _addressTextBox.Text = args.Url;
             _scriptTextbox.IsEnabled = true;
             _executeScriptButton.IsEnabled = true;
         }
     }
 }

Last update: December 5, 2022

Comments