#209. Added a UI for these parts (simple JavaScript execution, optionally synchronous).

This commit is contained in:
Per Lundberg
2013-11-08 00:39:41 +01:00
parent 22fac75268
commit 577c78108f
2 changed files with 122 additions and 35 deletions
+95 -30
View File
@@ -4,9 +4,11 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:local="clr-namespace:CefSharp.Wpf.Example.Views.Main"
mc:Ignorable="d"
d:DesignWidth="640"
d:DesignHeight="480">
d:DesignHeight="480"
d:DataContext="{d:DesignInstance local:MainViewModel}">
<Grid>
<DockPanel>
<DockPanel VerticalAlignment="Top"
@@ -40,35 +42,98 @@
</DockPanel>
<Expander VerticalAlignment="Top"
DockPanel.Dock="Top"
Header="Tweak WPF Rendering">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Content="Angle:" />
<Slider Grid.Row="0"
Grid.Column="1"
Name="angleSlider"
Minimum="-180"
Maximum="180" />
<Label Grid.Row="1"
Grid.Column="0"
Content="Watermark Opacity:" />
<Slider Grid.Row="1"
Grid.Column="1"
Name="opacitySlider"
Minimum="0"
Maximum="1"
TickFrequency="0.01"
Value="0.95" />
</Grid>
Header="Miscellaneous">
<StackPanel>
<GroupBox Header="Tweak WPF Rendering">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Content="Angle:" />
<Slider Grid.Row="0"
Grid.Column="1"
Name="angleSlider"
Minimum="-180"
Maximum="180" />
<Label Grid.Row="1"
Grid.Column="0"
Content="Watermark Opacity:" />
<Slider Grid.Row="1"
Grid.Column="1"
Name="opacitySlider"
Minimum="0"
Maximum="1"
TickFrequency="0.01"
Value="0.95" />
</Grid>
</GroupBox>
<GroupBox Header="Javascript Integration"
Margin="0,10,0,0">
<Grid Width="700">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0">Execute Javascript (asynchronously):</Label>
<TextBox x:Name="ExecuteJavascriptTextBox"
Grid.Row="0"
Grid.Column="1"
Margin="0,0,0,5">
<TextBox.InputBindings>
<KeyBinding Key="Enter"
Command="{Binding ExecuteJavaScriptCommand}"
CommandParameter="{Binding Text, RelativeSource={RelativeSource AncestorType=TextBox}}" />
</TextBox.InputBindings>
alert('Hello World, do you copy?')
</TextBox>
<Button Grid.Row="0"
Grid.Column="2"
Content="Run"
Margin="10,0,0,3"
Padding="5,2"
Command="{Binding ExecuteJavaScriptCommand}"
CommandParameter="{Binding Text, ElementName=ExecuteJavascriptTextBox}" />
<Label Grid.Row="2"
Grid.Column="0">Evaluate Javascript (and display result):</Label>
<TextBox x:Name="EvaluateJavascriptTextBox"
Grid.Row="2"
Grid.Column="1"
Margin="0,5,0,0">
<TextBox.InputBindings>
<KeyBinding Key="Enter"
Command="{Binding EvaluateJavaScriptCommand}"
CommandParameter="{Binding Text, RelativeSource={RelativeSource AncestorType=TextBox}}" />
</TextBox.InputBindings>
$.get('body').length
</TextBox>
<Button Grid.Row="2"
Grid.Column="2"
Content="Run"
Margin="10,3,0,0"
Padding="5,2"
Command="{Binding EvaluateJavaScriptCommand}"
CommandParameter="{Binding Text, ElementName=EvaluateJavascriptTextBox}" />
</Grid>
</GroupBox>
</StackPanel>
</Expander>
<StatusBar DockPanel.Dock="Bottom">
<ProgressBar HorizontalAlignment="Right"
@@ -1,9 +1,9 @@
using System;
using CefSharp.Example;
using CefSharp.Wpf.Example.Mvvm;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using CefSharp.Example;
using CefSharp.Wpf.Example.Mvvm;
namespace CefSharp.Wpf.Example.Views.Main
{
@@ -48,6 +48,8 @@ namespace CefSharp.Wpf.Example.Views.Main
public DelegateCommand GoCommand { get; set; }
public DelegateCommand ViewSourceCommand { get; set; }
public DelegateCommand HomeCommand { get; set; }
public DelegateCommand<string> ExecuteJavaScriptCommand { get; set; }
public DelegateCommand<string> EvaluateJavaScriptCommand { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
@@ -59,6 +61,8 @@ namespace CefSharp.Wpf.Example.Views.Main
GoCommand = new DelegateCommand(Go, () => !String.IsNullOrWhiteSpace(Address));
ViewSourceCommand = new DelegateCommand(ViewSource);
HomeCommand = new DelegateCommand(() => AddressEditable = Address = ExamplePresenter.DefaultUrl);
ExecuteJavaScriptCommand = new DelegateCommand<string>(ExecuteJavaScript, s => !String.IsNullOrWhiteSpace(s));
EvaluateJavaScriptCommand = new DelegateCommand<string>(EvaluateJavaScript, s => !String.IsNullOrWhiteSpace(s));
PropertyChanged += OnPropertyChanged;
@@ -66,6 +70,17 @@ namespace CefSharp.Wpf.Example.Views.Main
OutputMessage = version;
}
private void EvaluateJavaScript(string s)
{
var result = webBrowser.EvaluateScript(s) ?? "null";
MessageBox.Show("Result: " + result);
}
private void ExecuteJavaScript(string s)
{
webBrowser.ExecuteScriptAsync(s);
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
@@ -120,8 +135,15 @@ namespace CefSharp.Wpf.Example.Views.Main
Keyboard.ClearFocus();
// Commented out, just want to be able to test EvaluateScript.
//var result = webBrowser.EvaluateScript("return 10 * 20;", null);
//var result2 = result;
try
{
var result = webBrowser.EvaluateScript("10 * 20;", null);
var result2 = result;
}
catch (Exception e)
{
MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ViewSource()