-
Notifications
You must be signed in to change notification settings - Fork 437
Made an MVVM friendly example. #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
4da1fe4
f95ffac
d9de214
c40ba36
a6da190
da924cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using CefSharp.Wpf; | ||
using System.Windows; | ||
using System.Windows.Interactivity; | ||
|
||
namespace CefSharp.MinimalExample.Wpf.Binding.Behaviors | ||
{ | ||
public class LoadHtmlBehavior : Behavior<ChromiumWebBrowser> | ||
{ | ||
// Using a DependencyProperty as the backing store for Html. This enables animation, styling, binding, etc... | ||
public static readonly DependencyProperty HtmlProperty = | ||
DependencyProperty.Register( | ||
"Html", | ||
typeof(string), | ||
typeof(LoadHtmlBehavior), | ||
new PropertyMetadata(string.Empty, OnHtmlChanged)); | ||
|
||
// Using a DependencyProperty as the backing store for HtmlUrl. This enables animation, styling, binding, etc... | ||
public static readonly DependencyProperty HtmlUrlProperty = | ||
DependencyProperty.Register( | ||
"HtmlUrl", | ||
typeof(string), | ||
typeof(LoadHtmlBehavior), | ||
new PropertyMetadata(string.Empty)); | ||
|
||
public string Html | ||
{ | ||
get { return (string)GetValue(HtmlProperty); } | ||
set { SetValue(HtmlProperty, value); } | ||
} | ||
|
||
public string HtmlUrl | ||
{ | ||
get { return (string)GetValue(HtmlUrlProperty); } | ||
set { SetValue(HtmlUrlProperty, value); } | ||
} | ||
|
||
private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (e.NewValue == null || string.IsNullOrWhiteSpace((string)e.NewValue)) | ||
{ | ||
return; | ||
} | ||
|
||
(d as LoadHtmlBehavior)?.AssociatedObject.LoadHtml((string)e.NewValue, "about:blank"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace CefSharp.MinimalExample.Wpf.Binding | ||
{ | ||
public class BindableBase : INotifyPropertyChanged | ||
{ | ||
protected virtual void SetProperty<T>(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 { }; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Windows.Input; | ||
|
||
namespace CefSharp.MinimalExample.Wpf.Binding | ||
{ | ||
public class RelayCommand : ICommand | ||
{ | ||
Action _TargetExecuteMethod; | ||
Func<bool> _TargetCanExecuteMethod; | ||
|
||
public RelayCommand(Action executeMethod) | ||
{ | ||
_TargetExecuteMethod = executeMethod; | ||
} | ||
|
||
public RelayCommand(Action executeMethod, Func<bool> 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<T> : ICommand | ||
{ | ||
Action<T> _TargetExecuteMethod; | ||
Func<T, bool> _TargetCanExecuteMethod; | ||
|
||
public RelayCommand(Action<T> executeMethod) | ||
{ | ||
_TargetExecuteMethod = executeMethod; | ||
} | ||
|
||
public RelayCommand(Action<T> executeMethod, Func<T, bool> 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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,8 +67,10 @@ | |
<ApplicationManifest>app.manifest</ApplicationManifest> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.Expression.Interactions, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> | ||
|
||
<Reference Include="System" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Core" /> | ||
|
@@ -86,6 +88,8 @@ | |
<Generator>MSBuild:Compile</Generator> | ||
<SubType>Designer</SubType> | ||
</ApplicationDefinition> | ||
<Compile Include="Binding\Behaviors\BindHtmlAttachedBehavior.cs" /> | ||
<Compile Include="Binding\RelayCommand.cs" /> | ||
<Page Include="MainWindow.xaml"> | ||
<Generator>MSBuild:Compile</Generator> | ||
<SubType>Designer</SubType> | ||
|
@@ -94,7 +98,9 @@ | |
<DependentUpon>App.xaml</DependentUpon> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="Binding\BindableBase.cs" /> | ||
<Compile Include="Binding\TitleConverter.cs" /> | ||
<Compile Include="MainViewModel.cs" /> | ||
<Compile Include="MainWindow.xaml.cs"> | ||
<DependentUpon>MainWindow.xaml</DependentUpon> | ||
<SubType>Code</SubType> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "<div dir=\"ltr\"><b><i>test</i></b></div>\r\n"; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HtmlUrl
property isn't being used. Also best not o useabout:blank
, as it's a special case. You'd be effectively registering aResourceHandler
that gets loaded every timeabout:blank
is required.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see...so
"http://test/page"
is a better fit?Should we add a new method to load an HTML directly through one parameter?
Could you explain me briefly the intent of the
LoadHtml
with an URL?