Skip to content

Commit

Permalink
#209. Added a UI for these parts (simple JavaScript execution, option…
Browse files Browse the repository at this point in the history
…ally synchronous).
  • Loading branch information
perlun committed Nov 7, 2013
1 parent 22fac75 commit 577c781
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 35 deletions.
125 changes: 95 additions & 30 deletions CefSharp.Wpf.Example/Views/Main/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
32 changes: 27 additions & 5 deletions CefSharp.Wpf.Example/Views/Main/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -48,6 +48,8 @@ public IWpfWebBrowser WebBrowser
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;

Expand All @@ -59,13 +61,26 @@ public MainViewModel()
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;

var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
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)
Expand Down Expand Up @@ -120,8 +135,15 @@ private void Go()
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()
Expand Down

0 comments on commit 577c781

Please sign in to comment.