修改wpf界面功能支持执行js
This commit is contained in:
parent
8e51e97a3d
commit
dd7a518ea6
@ -40,5 +40,17 @@ namespace CefSharp.MinimalExample.Wpf
|
||||
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RunUIThread(Action action)
|
||||
{
|
||||
if (Current.CheckAccess())
|
||||
{
|
||||
action?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
Current.Dispatcher.BeginInvoke(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -103,12 +103,17 @@
|
||||
<HintPath>..\packages\CefSharp.Wpf.107.1.120\lib\net452\CefSharp.Wpf.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\log4net.2.0.15\lib\net45\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xaml.Behaviors, Version=1.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Xaml.Behaviors.Wpf.1.1.19\lib\net45\Microsoft.Xaml.Behaviors.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Xaml">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
|
@ -23,6 +23,10 @@
|
||||
<Setter Property="Margin" Value="2,5" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
@ -30,25 +34,45 @@
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Button Content="Back" Command="{Binding WebBrowser.BackCommand, ElementName=Browser}" Width="50"/>
|
||||
<Button Content="Forward" Command="{Binding WebBrowser.ForwardCommand, ElementName=Browser}" Grid.Column="1" Width="60"/>
|
||||
<TextBox x:Name="txtBoxAddress" Text="{Binding Address, ElementName=Browser, FallbackValue=www.google.com}" Grid.Column="2" FontSize="12" BorderBrush="Gray" BorderThickness="1">
|
||||
<Button Content="Back" Command="{Binding WebBrowser.BackCommand, ElementName=Browser}" Width="50" />
|
||||
<Button Content="Forward" Command="{Binding WebBrowser.ForwardCommand, ElementName=Browser}" Grid.Row="0" Grid.Column="1" Width="60" />
|
||||
<TextBox x:Name="txtBoxAddress" Text="{Binding Address, ElementName=Browser, FallbackValue=www.google.com}" Grid.Row="0" Grid.Column="2" FontSize="12"
|
||||
BorderBrush="Gray" BorderThickness="1">
|
||||
<behaviors:Interaction.Behaviors>
|
||||
<behaviours:TextBoxBindingUpdateOnEnterBehaviour />
|
||||
</behaviors:Interaction.Behaviors>
|
||||
</TextBox>
|
||||
<Button Content="Print..." Command="{Binding WebBrowser.PrintCommand, ElementName=Browser}" Grid.Column="3" Width="50" />
|
||||
<Button Content="View source" Command="{Binding WebBrowser.ViewSourceCommand, ElementName=Browser}" Grid.Column="4" Width="75" />
|
||||
<Button Content="Print..." Command="{Binding WebBrowser.PrintCommand, ElementName=Browser}" Grid.Row="0" Grid.Column="3" Width="50" />
|
||||
<Button Content="View source" Command="{Binding WebBrowser.ViewSourceCommand, ElementName=Browser}" Grid.Row="0" Grid.Column="4" Width="75" />
|
||||
<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBox x:Name="JsTb" Grid.Column="0" VerticalContentAlignment="Center"/>
|
||||
<TextBox x:Name="JsArgs1Tb" Grid.Column="1" VerticalContentAlignment="Center"/>
|
||||
<TextBox x:Name="JsArgs2Tb" Grid.Column="2" VerticalContentAlignment="Center"/>
|
||||
<CheckBox Grid.Column="3" x:Name="HasArgsCb" Width="80" VerticalAlignment="Center" HorizontalAlignment="Center" Content="HasArgs"/>
|
||||
<CheckBox Grid.Column="4" x:Name="HasValueCb" Width="80" VerticalAlignment="Center" HorizontalAlignment="Center" Content="HasValue"/>
|
||||
<Button Grid.Column="5" Content="ExcJs" Click="JsBtn_OnClick" x:Name="JsBtn"/>
|
||||
<TextBlock Grid.Column="6" x:Name="JsResultTb" VerticalAlignment="Center"/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
<Border Grid.Row="1" BorderBrush="Gray" BorderThickness="0,1">
|
||||
<wpf:ChromiumWebBrowser x:Name="Browser"
|
||||
Address="https://www.google.com">
|
||||
<behaviors:Interaction.Behaviors>
|
||||
<behaviours:HoverLinkBehaviour x:Name="HoverLinkBehaviour"/>
|
||||
<behaviours:HoverLinkBehaviour x:Name="HoverLinkBehaviour" />
|
||||
</behaviors:Interaction.Behaviors>
|
||||
</wpf:ChromiumWebBrowser>
|
||||
</Border>
|
||||
<ProgressBar IsIndeterminate="{Binding IsLoading, ElementName=Browser}"
|
||||
<ProgressBar
|
||||
IsIndeterminate="{Binding IsLoading, ElementName=Browser}"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Top"
|
||||
Width="Auto"
|
||||
@ -74,7 +98,10 @@
|
||||
<Separator Grid.Column="1" />
|
||||
<StatusBarItem Grid.Column="2">
|
||||
<TextBlock HorizontalAlignment="Right" TextAlignment="Right" Grid.Column="3" VerticalAlignment="Center">
|
||||
Chromium: <Run Text="{Binding Source={x:Static cef:Cef.ChromiumVersion}, Mode=OneTime}" />, CEF: <Run Text="{Binding Source={x:Static cef:Cef.CefVersion}, Mode=OneTime}" />, CefSharp: <Run Text="{Binding Source={x:Static cef:Cef.CefSharpVersion}, Mode=OneTime}"/>, Environment: <Run Text="{Binding Converter={StaticResource EnvironmentConverter}, Mode=OneTime}"/>
|
||||
Chromium: <Run Text="{Binding Source={x:Static cef:Cef.ChromiumVersion}, Mode=OneTime}" />, CEF:
|
||||
<Run Text="{Binding Source={x:Static cef:Cef.CefVersion}, Mode=OneTime}" />, CefSharp:
|
||||
<Run Text="{Binding Source={x:Static cef:Cef.CefSharpVersion}, Mode=OneTime}" />, Environment:
|
||||
<Run Text="{Binding Converter={StaticResource EnvironmentConverter}, Mode=OneTime}" />
|
||||
</TextBlock>
|
||||
</StatusBarItem>
|
||||
</StatusBar>
|
||||
|
@ -7,6 +7,63 @@ namespace CefSharp.MinimalExample.Wpf
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
Browser.FrameLoadStart += BrowserOnFrameLoadStart;
|
||||
Browser.FrameLoadEnd += BrowserOnFrameLoadEnd;
|
||||
}
|
||||
|
||||
private void BrowserOnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
private void BrowserOnFrameLoadStart(object sender, FrameLoadStartEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
private void JsBtn_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
object[] args = new object[2];
|
||||
args[0] = JsArgs1Tb.Text;
|
||||
args[1] = JsArgs2Tb.Text;
|
||||
if (HasValueCb.IsChecked ?? false)
|
||||
{
|
||||
ExecuteJs(JsTb.Text, (HasArgsCb.IsChecked ?? false) ? args : null);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExecuteJsNoValue(JsTb.Text, (HasArgsCb.IsChecked ?? false) ? args : null);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteJsNoValue(string js, object[] args = null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(js))
|
||||
{
|
||||
js = args == null ? js : WebBrowserExtensions.GetScriptForJavascriptMethodWithArgs(js, args);
|
||||
Browser.ExecuteScriptAsync(js);
|
||||
}
|
||||
else
|
||||
{
|
||||
JsResultTb.Text = "js is empty!";
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteJs(string js, object[] args = null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(js))
|
||||
{
|
||||
js = args == null ? js : WebBrowserExtensions.GetScriptForJavascriptMethodWithArgs(js, args);
|
||||
Browser.EvaluateScriptAsync(js).ContinueWith(o =>
|
||||
{
|
||||
App.RunUIThread(() =>
|
||||
{
|
||||
JsResultTb.Text = (o?.Result?.Success ?? false) ? o?.Result?.Result?.ToString() : o?.Result?.Message;
|
||||
});
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
JsResultTb.Text = "js is empty!";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,4 +5,5 @@
|
||||
<package id="CefSharp.Common" version="107.1.120" targetFramework="net452" />
|
||||
<package id="CefSharp.Wpf" version="107.1.120" targetFramework="net452" />
|
||||
<package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.19" targetFramework="net452" />
|
||||
<package id="log4net" version="2.0.15" targetFramework="net452" />
|
||||
</packages>
|
Loading…
Reference in New Issue
Block a user