Posts

Showing posts from November, 2016

Using the Mediator pattern in MVVM / WPF

Image
Some instructions on how to use the Mediator design pattern as a means of allowing communication between ViewModel classes in your MVVM / WPF application. In this example I use the Mediator as a means of communicating to the main window which view to display when the user clicks a button on either of the child views, View1 and View2. Step 1: Create a new WPF application Step 2: Add event handling classes EventArgs.cs [code language="csharp"] using System; namespace MvvmSwitchViews { public class EventArgs<T> : EventArgs { public EventArgs(T value) { Value = value; } public T Value { get; private set; } } } [/code] EventRaiser.cs [code language="csharp"] using System; namespace MvvmSwitchViews { public static class EventRaiser { public static void Raise(this EventHandler handler, object sender) { if (handler != null) { ...

Using the CefSharp Chromium Web Browser in WPF / XAML

Image
Some brief instructions on getting started with using the Chromium Web Browser in WPF. Step 1: Create a new WPF project Step 2: Obtain the CefSharp packages using NuGet Link for the NuGet packages is here: https://www.nuget.org/packages/CefSharp.WPF/ Select Tool > NuGet Package manager > Package Manager Console. Use the [code language="txt"] PM> install-package CefSharp.Wpf [/code] Step 3: add the CefSharp dll references Right-click on References, select 'Add reference' When the dialog appears, select the Browse button. Navigate to the 'packages' folder that NuGet has installed to your Visual Studio project. For this project I'm choosing the x86 versions... Add CefSharp.Wpf.dll: Add the CefSharp.dll, CefSharp.Core.dll, CefSharp.BrowserSubprocessCore.dll: If you get the error similar to the following: [code language="text"] Error 1 CefSharp.Common will work out of the box if you specify platform (x86 / ...

Using embedded MediaElement controls to play videos in WPF / MVVM

Image
Essentially the same as these instructions , but using a purely MVVM approach and without the additional stop / start / pause etc buttons - the application just 'sees' the MP4 media file it needs to play and plays it upon startup. Step 1: Create a new WPF Application Step 2: Create the Main Window ViewModel classes Add the following classes to your project: MainWindowViewModel.cs [code language="csharp"] namespace MediaElement { public class MainWindowViewModel : BaseViewModel { private string _url; public MainWindowViewModel() { Url = "E:\\temp\\SampleMp4File.mp4"; } public string Url { get { return _url; } set { if (_url == value) return; _url = value; OnPropertyChanged("Url"); } } } } [/code] BaseViewModel.cs [code language="csharp"] usin...

How to embed the IE WebBrowser control in WPF / XAML

Image
Firstly credit, must go to Thomas Freudenberg's splendid 2010 blog posting : Binding WebBrowser content in WPF I have merely tweaked it a little, so that instead of using this technique to set the HTML content, which I don't need. I just want it such that I can get it to bind to the URL link I am interested in. Step 1: Create a new WPF project in Visual Studio: Step 2: Write a helper class to handle the attached properties WebBrowser.Source is not a DependencyProperty. A known workaround is to use AttachedProperty to enable this ability. [code language="csharp"] using System; using System.Windows; using System.Windows.Controls; namespace WebBrowserHelperDemo { public static class WebBrowserHelper { public static readonly DependencyProperty UrlProperty = DependencyProperty.RegisterAttached("Url", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnUrlChanged)); public sta...