我目前正在尝试在WinRT应用程序中显示webview(没有c#)。 首先,有可能吗?
我目前正试图这样做:
Windows::UI::Xaml::Controls::WebView^ webView = ref new Windows::UI::Xaml::Controls::WebView();
webView->NavigateToString("http://www.google.com");
但是我希望这个代码能够在主线程中运行(我认为,创建一个UI元素是必须的)我正在运行它:
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");
}
));
不好的一点是这一行:
webView->NavigateToString("http://www.google.com");
永远不会到达。在输出控制台中,我在该行引发了三个异常(在逐步调试时):
Windows::UI::Xaml::Controls::WebView^ webView = ref new Windows::UI::Xaml::Controls::WebView();
提出的例外情况如下:
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).
如果你知道如何创建一个webview并在Windows 8(不是Windows Phone 8)上的C ++应用程序中显示它,那就太棒了。同时我强烈考虑将webkit集成到我的应用程序中,如果它可以帮助我只用C ++显示webkit webview。
THX
答案 0 :(得分:0)
使用XAML:
<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>
这样的C ++代码:
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>");
}
您可以构建,添加到父级,然后导航到URL(或字符串)。
您也可以使用Dispatcher
:
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");
}
));
问题是您需要访问将添加WebView
实例的父元素。