For simplicities sake strip out all the MVVM parts and simplify the example down to the bare minimum

(Code is declared in the MainWindow now)
This commit is contained in:
amaitland 2016-01-13 10:53:46 +10:00
parent 757b8edf1e
commit 09b92eac80
8 changed files with 45 additions and 144 deletions

View File

@ -1,8 +1,9 @@
<Application x:Class="CefSharp.MinimalExample.Wpf.App" <Application x:Class="CefSharp.MinimalExample.Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:binding="clr-namespace:CefSharp.MinimalExample.Wpf.Binding"
StartupUri="MainWindow.xaml"> StartupUri="MainWindow.xaml">
<Application.Resources> <Application.Resources>
<binding:TitleConverter x:Key="TitleConverter"/>
</Application.Resources> </Application.Resources>
</Application> </Application>

View File

@ -0,0 +1,20 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace CefSharp.MinimalExample.Wpf.Binding
{
public class TitleConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "CefSharp.MinimalExample.Wpf - " + (value ?? "No Title Specified");
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Windows.Data.Binding.DoNothing;
}
}
}

View File

@ -78,10 +78,6 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Views\MainView.xaml.cs">
<DependentUpon>MainView.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\MainViewModel.cs" />
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
@ -90,17 +86,13 @@
<DependentUpon>App.xaml</DependentUpon> <DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Binding\TitleConverter.cs" />
<Compile Include="MainWindow.xaml.cs"> <Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon> <DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Page Include="Views\MainView.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Mvvm\PropertyChangedExtensionMethods.cs" />
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
@ -131,6 +123,7 @@
<ItemGroup> <ItemGroup>
<Resource Include="chromium-256.ico" /> <Resource Include="chromium-256.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\cef.redist.x86.3.2454.1344\build\cef.redist.x86.targets" Condition="Exists('..\packages\cef.redist.x86.3.2454.1344\build\cef.redist.x86.targets')" /> <Import Project="..\packages\cef.redist.x86.3.2454.1344\build\cef.redist.x86.targets" Condition="Exists('..\packages\cef.redist.x86.3.2454.1344\build\cef.redist.x86.targets')" />
<Import Project="..\packages\cef.redist.x64.3.2454.1344\build\cef.redist.x64.targets" Condition="Exists('..\packages\cef.redist.x64.3.2454.1344\build\cef.redist.x64.targets')" /> <Import Project="..\packages\cef.redist.x64.3.2454.1344\build\cef.redist.x64.targets" Condition="Exists('..\packages\cef.redist.x64.3.2454.1344\build\cef.redist.x64.targets')" />

View File

@ -1,8 +1,26 @@
<Window x:Class="CefSharp.MinimalExample.Wpf.MainWindow" <Window x:Class="CefSharp.MinimalExample.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:main="clr-namespace:CefSharp.MinimalExample.Wpf.Views" xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
Title="{Binding WebBrowser.Title}" Title="{Binding Path=Title, ElementName=Browser, Converter={StaticResource TitleConverter}}"
WindowState="Maximized"> WindowState="Maximized">
<main:MainView/> <Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<wpf:ChromiumWebBrowser Grid.Row="0"
x:Name="Browser"
Address="http://www.google.com" />
<StatusBar Grid.Row="1">
<ProgressBar HorizontalAlignment="Right"
IsIndeterminate="{Binding WebBrowser.IsLoading}"
Width="100"
Height="16"
Margin="3" />
<Separator />
<!-- TODO: Could show hover link URL here -->
<TextBlock />
</StatusBar>
</Grid>
</Window> </Window>

View File

@ -1,48 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
namespace CefSharp.MinimalExample.Wpf.Mvvm
{
public static class PropertyChangedExtensionMethods
{
// Based on http://www.wpftutorial.net/INotifyPropertyChanged.html
public static bool ChangeAndNotify<T>(this PropertyChangedEventHandler handler,
ref T field, T value, Expression<Func<T>> memberExpression)
{
if (memberExpression == null)
{
throw new ArgumentNullException("memberExpression");
}
var body = memberExpression.Body as MemberExpression;
if (body == null)
{
throw new ArgumentException("Lambda must return a property.");
}
if (EqualityComparer<T>.Default.Equals(field, value))
{
return false;
}
field = value;
var vmExpression = body.Expression as ConstantExpression;
if (vmExpression != null)
{
var lambda = Expression.Lambda(vmExpression);
var vmFunc = lambda.Compile();
var sender = vmFunc.DynamicInvoke();
if (handler != null)
{
handler(sender, new PropertyChangedEventArgs(body.Member.Name));
}
}
return true;
}
}
}

View File

@ -1,39 +0,0 @@
using CefSharp.MinimalExample.Wpf.Mvvm;
using CefSharp.Wpf;
using System.ComponentModel;
using System.Windows;
namespace CefSharp.MinimalExample.Wpf.ViewModels
{
public class MainViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
private IWpfWebBrowser webBrowser;
public IWpfWebBrowser WebBrowser
{
get { return webBrowser; }
set { PropertyChanged.ChangeAndNotify(ref webBrowser, value, () => WebBrowser); }
}
private string title;
public string Title
{
get { return title; }
set { PropertyChanged.ChangeAndNotify(ref title, value, () => Title); }
}
public MainViewModel()
{
PropertyChanged += OnPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Title")
{
Application.Current.MainWindow.Title = "CefSharp.MinimalExample.Wpf - " + Title;
}
}
}
}

View File

@ -1,30 +0,0 @@
<UserControl x:Class="CefSharp.MinimalExample.Wpf.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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"
mc:Ignorable="d"
d:DesignWidth="640"
d:DesignHeight="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<cefSharp:ChromiumWebBrowser Grid.Row="0"
Address="http://www.google.com"
WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}"
Title="{Binding Title, Mode=TwoWay}" />
<StatusBar Grid.Row="1">
<ProgressBar HorizontalAlignment="Right"
IsIndeterminate="{Binding WebBrowser.IsLoading}"
Width="100"
Height="16"
Margin="3" />
<Separator />
<!-- TODO: Could show hover link URL here -->
<TextBlock />
</StatusBar>
</Grid>
</UserControl>

View File

@ -1,14 +0,0 @@
using System.Windows.Controls;
using CefSharp.MinimalExample.Wpf.ViewModels;
namespace CefSharp.MinimalExample.Wpf.Views
{
public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
}