Binding WebView directly to Html Content using C++ /CX

This post is inspired by Tim Heuer’s Html Source extension property that uses the WebView’s NavigateToString method to bind webview directly to Html Content. Tim’s code is written using C#. I wanted a C++ version of the same for one of my sample projects and here it is. Feel free to use it in your projects and let me know when you use it. That would make me happy!


Typical Usage scenarios for this extension:


1. You use the SyndicationClient API from the Windows.Web.Syndication namespace and parse a RSS feed of interest.


2. Your UI is a GridView and each GridViewItem displays one article from the RSS feed parsed above.


3. You want to display formatted HTML content instead of unformatted HTML content as string.


For more information, look at this blog post by Soma.


http://blogs.msdn.com/b/somasegar/archive/2012/08/26/building-an-end-to-end-windows-store-app-part-1.aspx


WebViewExtension.h


#pragma once

#include "pch.h"


using namespace Windows::UI::Xaml;

using namespace Windows::UI::Xaml::Controls;

using namespace Windows::UI::Xaml::Interop;

using namespace Platform;


namespace Callisto

{

[Windows::Foundation::Metadata::WebHostHidden]

public ref class WebViewExtension sealed

{

public:


static String^ GetHtmlSource(WebView^ view)

{

if (nullptr == view)

throw ref new Platform::InvalidArgumentException("view");


return view->GetValue(HtmlSourceProperty)->ToString();

}


static void SetHtmlSource(WebView^ view, Platform::String^ value)

{

if (nullptr == view)

throw ref new Platform::InvalidArgumentException("view");


view->SetValue(HtmlSourceProperty, value);

}





private:

static void OnHtmlSourcePropertyChanged(DependencyObject^ o, DependencyPropertyChangedEventArgs^ e)

{

auto view = dynamic_cast(o);

if (nullptr == o)

{

throw ref new Platform::NotImplementedException("The HtmlSource attached dependency property is only valid for WebView instances.");

}

// Determine new HTML content

auto newString = dynamic_cast(e->NewValue);

auto newHtml = newString->IsEmpty() ? "" : newString;


view->NavigateToString(newHtml);

}


static Windows::UI::Xaml::DependencyProperty^ HtmlSourceProperty;

};

}


WebViewExtension.cpp


//

// Copyright (c) 2012 Sridhar Poduri

// Based on Tim Heuer's C-Sharp version available at

// https://github.com/timheuer/callisto/...

//

// Licensed under the Microsoft Public License (Ms-PL) (the "License");

// you may not use this file except in compliance with the License.

// You may obtain a copy of the License at

//

// http://opensource.org/licenses/Ms-PL....

//

// Unless required by applicable law or agreed to in writing, software

// distributed under the License is distributed on an "AS IS" BASIS,

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

// See the License for the specific language governing permissions and

// limitations under the License.

//

// Derived from http://blogs.msdn.com/b/delay/archive...

#include "pch.h"

#include "WebViewExtension.h"


using namespace Callisto;

using namespace Windows::UI::Xaml;

using namespace Windows::UI::Xaml::Controls;

using namespace Windows::UI::Xaml::Interop;

using namespace Platform;




Windows::UI::Xaml::DependencyProperty^ Callisto::WebViewExtension::HtmlSourceProperty =

DependencyProperty::RegisterAttached(ref new String(L"HtmlSource"), TypeName(String::typeid), TypeName(WebViewExtension::typeid),

ref new PropertyMetadata(nullptr, ref new PropertyChangedCallback(OnHtmlSourcePropertyChanged)));



 

 


Once you have this header and cpp file in your solution, using them from XAML markup is pretty easy, such as the simple example below.



Enjoy!


-Sridhar

 •  0 comments  •  flag
Share on Twitter
Published on October 25, 2012 07:46
No comments have been added yet.