diff --git a/CefSharp.MinimalExample.Wpf/Binding/Behaviors/HtmlBindingHelper.cs b/CefSharp.MinimalExample.Wpf/Binding/Behaviors/HtmlBindingHelper.cs new file mode 100644 index 00000000..f3b97ef2 --- /dev/null +++ b/CefSharp.MinimalExample.Wpf/Binding/Behaviors/HtmlBindingHelper.cs @@ -0,0 +1,38 @@ +using CefSharp.Wpf; +using System.Windows; + +namespace CefSharp.MinimalExample.Wpf.Binding.Behaviors +{ + public static class HtmlBindingHelper + { + // Using a DependencyProperty as the backing store for Html. This enables animation, styling, binding, etc... + public static readonly DependencyProperty HtmlProperty = + DependencyProperty.RegisterAttached( + "Html", + typeof(string), + typeof(HtmlBindingHelper), + new PropertyMetadata(string.Empty, OnHtmlChanged)); + + public static string GetHtml(DependencyObject obj) + { + return (string)obj.GetValue(HtmlProperty); + } + + public static void SetHtml(DependencyObject obj, string value) + { + obj.SetValue(HtmlProperty, value); + } + + private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + string htmlText = e.NewValue as string; + + if (string.IsNullOrWhiteSpace(htmlText)) + { + htmlText = string.Empty; + } + + ((ChromiumWebBrowser)d).LoadHtml(htmlText, "http://cefsharp/loadHtml"); + } + } +} diff --git a/CefSharp.MinimalExample.Wpf/Binding/BindableBase.cs b/CefSharp.MinimalExample.Wpf/Binding/BindableBase.cs new file mode 100644 index 00000000..0e03dd33 --- /dev/null +++ b/CefSharp.MinimalExample.Wpf/Binding/BindableBase.cs @@ -0,0 +1,24 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace CefSharp.MinimalExample.Wpf.Binding +{ + public class BindableBase : INotifyPropertyChanged + { + protected virtual void SetProperty(ref T member, T val, + [CallerMemberName] string propertyName = null) + { + if (object.Equals(member, val)) { return; } + + member = val; + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + + protected virtual void OnPropertyChanged(string propertyName) + { + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + + public event PropertyChangedEventHandler PropertyChanged = delegate { }; + } +} diff --git a/CefSharp.MinimalExample.Wpf/Binding/RelayCommand.cs b/CefSharp.MinimalExample.Wpf/Binding/RelayCommand.cs new file mode 100644 index 00000000..92a45bc1 --- /dev/null +++ b/CefSharp.MinimalExample.Wpf/Binding/RelayCommand.cs @@ -0,0 +1,98 @@ +using System; +using System.Windows.Input; + +namespace CefSharp.MinimalExample.Wpf.Binding +{ + public class RelayCommand : ICommand + { + Action _TargetExecuteMethod; + Func _TargetCanExecuteMethod; + + public RelayCommand(Action executeMethod) + { + _TargetExecuteMethod = executeMethod; + } + + public RelayCommand(Action executeMethod, Func canExecuteMethod) + { + _TargetExecuteMethod = executeMethod; + _TargetCanExecuteMethod = canExecuteMethod; + } + + public void RaiseCanExecuteChanged() + { + CanExecuteChanged(this, EventArgs.Empty); + } + #region ICommand Members + + bool ICommand.CanExecute(object parameter) + { + if (_TargetCanExecuteMethod != null) + { + return _TargetCanExecuteMethod(); + } + if (_TargetExecuteMethod != null) + { + return true; + } + return false; + } + + // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command + // Prism commands solve this in their implementation + public event EventHandler CanExecuteChanged = delegate { }; + + void ICommand.Execute(object parameter) + { + _TargetExecuteMethod?.Invoke(); + } + #endregion + } + + public class RelayCommand : ICommand + { + Action _TargetExecuteMethod; + Func _TargetCanExecuteMethod; + + public RelayCommand(Action executeMethod) + { + _TargetExecuteMethod = executeMethod; + } + + public RelayCommand(Action executeMethod, Func canExecuteMethod) + { + _TargetExecuteMethod = executeMethod; + _TargetCanExecuteMethod = canExecuteMethod; + } + + public void RaiseCanExecuteChanged() + { + CanExecuteChanged(this, EventArgs.Empty); + } + #region ICommand Members + + bool ICommand.CanExecute(object parameter) + { + if (_TargetCanExecuteMethod != null) + { + T tparm = (T)parameter; + return _TargetCanExecuteMethod(tparm); + } + if (_TargetExecuteMethod != null) + { + return true; + } + return false; + } + + // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command + // Prism commands solve this in their implementation + public event EventHandler CanExecuteChanged = delegate { }; + + void ICommand.Execute(object parameter) + { + _TargetExecuteMethod?.Invoke((T)parameter); + } + #endregion + } +} diff --git a/CefSharp.MinimalExample.Wpf/CefSharp.MinimalExample.Wpf.csproj b/CefSharp.MinimalExample.Wpf/CefSharp.MinimalExample.Wpf.csproj index 966eb5c8..c78243fc 100644 --- a/CefSharp.MinimalExample.Wpf/CefSharp.MinimalExample.Wpf.csproj +++ b/CefSharp.MinimalExample.Wpf/CefSharp.MinimalExample.Wpf.csproj @@ -86,6 +86,8 @@ MSBuild:Compile Designer + + MSBuild:Compile Designer @@ -94,7 +96,9 @@ App.xaml Code + + MainWindow.xaml Code diff --git a/CefSharp.MinimalExample.Wpf/MainViewModel.cs b/CefSharp.MinimalExample.Wpf/MainViewModel.cs new file mode 100644 index 00000000..ff3da854 --- /dev/null +++ b/CefSharp.MinimalExample.Wpf/MainViewModel.cs @@ -0,0 +1,37 @@ +using CefSharp.MinimalExample.Wpf.Binding; +using System.Windows.Input; + +namespace CefSharp.MinimalExample.Wpf +{ + public class MainViewModel : BindableBase + { + private string html; + + public MainViewModel() + { + ViewHtmlCommand = new RelayCommand(OnViewHtml); + Address = "www.google.com"; + } + + public string Html + { + get { return html; } + set { SetProperty(ref html, value); } + } + + private string address; + + public string Address + { + get { return address; } + set { SetProperty(ref address, value); } + } + + public ICommand ViewHtmlCommand { get; private set; } + + private void OnViewHtml() + { + Html = "
test
\r\n"; + } + } +} diff --git a/CefSharp.MinimalExample.Wpf/MainWindow.xaml b/CefSharp.MinimalExample.Wpf/MainWindow.xaml index 70ad9e34..4e186549 100644 --- a/CefSharp.MinimalExample.Wpf/MainWindow.xaml +++ b/CefSharp.MinimalExample.Wpf/MainWindow.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" + xmlns:behaviors="clr-namespace:CefSharp.MinimalExample.Wpf.Binding.Behaviors" Title="{Binding Path=Title, ElementName=Browser, Converter={StaticResource TitleConverter}}" WindowState="Maximized"> @@ -11,16 +12,45 @@ + Address="{Binding Address}" + behaviors:HtmlBindingHelper.Html="{Binding Html}"/> + - - - - + + + + + + + + + + + + + + + + +