WinRT W8 Create WebView in C++ and add it to the main view

2

I'm currently trying to display a webview in a WinRT application (without c#). First, is it possible ?

I'm currently trying to do it that way:

Windows::UI::Xaml::Controls::WebView^ webView = ref new Windows::UI::Xaml::Controls::WebView();
webView->NavigateToString("http://www.google.com");

But as I want this code to be run in the main thread (I think, to create a UI element it's compulsory) I'm running it like that:

Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->GetForCurrentThread()->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler(
    []
    {
        Windows::UI::Xaml::Controls::WebView^ webView = ref new Windows::UI::Xaml::Controls::WebView();
        webView->NavigateToString("http://www.google.com");
    }
));

Bad point is that this line:

webView->NavigateToString("http://www.google.com");

Is never reached. In the output console I have three exceptions raised (while debugging step by step) at that line:

Windows::UI::Xaml::Controls::WebView^ webView = ref new Windows::UI::Xaml::Controls::WebView();

The exceptions raised are the following:

First-chance exception at 0x763B4B32 in Direct3DApp1.exe: Microsoft C++ exception: Platform::WrongThreadException ^ at memory location 0x0320EFE4. HRESULT:0x8001010E
First-chance exception at 0x763B4B32 in Direct3DApp1.exe: Microsoft C++ exception: Platform::WrongThreadException ^ at memory location 0x0320EFE4. HRESULT:0x8001010E
First-chance exception at 0x763B4B32 (KernelBase.dll) in Direct3DApp1.exe: 0x40080201: WinRT originate error (parameters: 0x8001010E, 0x00000051, 0x0320E494).

If you have any idea how to create a webview and display it in a C++ only application on Windows 8 (not Windows Phone 8), it would be great. Meanwhile I'm strongly thinking about integrating webkit in my application if it could help me to display a webkit webview in C++ only.

Thx

c++
windows-8
windows-runtime
asked on Stack Overflow Nov 7, 2013 by Sistr

1 Answer

0

With XAML like this:

<Page
    x:Class="Win81CPlusPlus.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Win81CPlusPlus"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 
          x:Name="grid">

    </Grid>
</Page>

And C++ code like this:

MainPage::MainPage()
{
    InitializeComponent();
    auto webView = ref new Windows::UI::Xaml::Controls::WebView();

    Grid^ grid = safe_cast<Grid^>(this->FindName("grid"));
    grid->Children->Append(webView);

    auto uri = ref new Uri(L"http://www.stackoverflow.com");
    webView->Navigate(uri);
    // or you could ...
    //webView->NavigateToString(L"<html><body><h1>Hello</h1></body></html>");
}

You can construct, add to a parent, and navigate to a URL (or a string).

You can use the Dispatcher as well:

Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->GetForCurrentThread()->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, 
    ref new Windows::UI::Core::DispatchedHandler(
    [this]   /* the this won't likely make sense from another thread */
    {
        auto webView = ref new Windows::UI::Xaml::Controls::WebView();
                    // will need to get access to the grid
        auto grid = safe_cast<Grid^>(this->FindName("grid"));
        grid->Children->Append(webView);
        auto uri = ref new Uri(L"http://www.google.com");
        webView->Navigate(uri);
        //webView->NavigateToString("http://www.google.com");
    }
));

The issue is that you'll need access to a parent element to which the WebView instance will be added.

answered on Stack Overflow Nov 7, 2013 by WiredPrairie

User contributions licensed under CC BY-SA 3.0