I'm trying to use a webview2 control to get the body text of a website. But nothing showed.
My code:
winrt::Windows::Foundation::IAsyncAction MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
winrt::Microsoft::UI::Xaml::Controls::WebView2 web;
std::wstring uriToLaunch{ L"http://www.bing.com" };
Windows::Foundation::Uri uri{ uriToLaunch };
web.Source(uri);
co_await web.EnsureCoreWebView2Async();
winrt::hstring obj = co_await web.CoreWebView2().ExecuteScriptAsync(L"document.body.innerText");
TextBlock().Text(obj);
web.close();
}
Thank you indeed for your advise in advance.
You might need to change your script. It should be like ExecuteScriptAsync("document.documentElement.outerHTML;")
Related
I want to use spdlog for my code's logging. In my code, there is a important variable for the step in simulation, and I want it to be always displayed in my logs.
Here is the format I wants.
[log_level][the_special_variable][logger_name] messages
So how could format the logger? Or there isn't any way to do that?
Edited:
Sorry I am not good at asking a question in English.
I've read the Readme.md in spdlog's github, and i saw this
// Log patterns can contain custom flags.
// the following example will add new flag '%*' - which will be bound to a <my_formatter_flag> instance.
#include "spdlog/pattern_formatter.h"
class my_formatter_flag : public spdlog::custom_flag_formatter
{
public:
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
{
std::string some_txt = "custom-flag";
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
}
std::unique_ptr<custom_flag_formatter> clone() const override
{
return spdlog::details::make_unique<my_formatter_flag>();
}
};
void custom_flags_example()
{
auto formatter = std::make_unique<spdlog::pattern_formatter>();
formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v");
spdlog::set_formatter(std::move(formatter));
}
but I can't understand the usage of it. It seems like it can only add a string for the custom flag. I would like to kwon if it is OK to display a int variable.
Yes, it is okay to add an int to the log message, you just have to stringify it. For example, in the format method:
auto str = std::to_string(my_special_int_variable);
dest.append(...);
The only question is how you make your int var available in the formatter. The example above assumes it's a global variable.
since yesterday I have been struggling to turn text into label with code from another class, I came to this:
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
TestApp::UI_Error form("test", "test");
Application::Run(% form);
Using the above code, i display a winapi form that receives "test", "test" as 2x const char* on loading, the problem appears when im trying to set the text in labels using these variables
The code looks like this:
public:
UI_Error(const char* errorText, const char* errorCode)
{
InitializeComponent();
this->testLabel->Text = System::Convert::ToString(errorText);
}
For some reason, each time the return value shown in the win forms window is "true", although it should be "test" here, does anyone know the solution?
I tried to use std::string instead of const char*, unfortunately for some reason i get the error code that a static variable is required :(
Maybe I am wrong here, but System::Convert::ToString() seems not to have a method that accepts a const char* pointer. It looks like it gets cast to something else.
Try this instead:
Text = gcnew System::String(errorText);
I have a function:
auto func = v8::FunctionTemplate::New(context,
[](const v8::FunctionCallbackInfo<v8::Value>& args) {
auto isolate = args.GetIsolate();
if (args.IsConstructCall()) { ... }
...
}).ToLocalChecked();
and I exposed this function as following.
global->Set(isolate, "func", func);
I used the function both as a normal function and a constructor,
func();
new func();
and V8 crushed when IsConstructCall is called. I found that IsConstructCall uses QuickIsUndefined to check whether NewTarget is undefined or not. The problem occurs inside ReadRawField, which is called by GetInstanceType, and QuickIsUndefined calls GetInstanceType.
Since T=unsigned short, I added reinterpret_cast<unsigned short*>(addr) to the watch and the result was like this, and it was different from what the exception says.
What makes me more confusing is that sometimes my program runs well without crushing. Instead of IsContructCall, I used v8::Undefined and Value::Equals, and it runs well.
!args.NewTarget()->Equals(isolate->GetCurrentContext(), v8::Undefined(isolate)).ToChecked()
It seems like QuickIsUndefined has some problem, but I can't identify the problem. What may help in this situation? I compiled V8 with MSVC in monolith mode.
I had the same issue:
try to compile your code with -DV8_COMPRESS_POINTERS. V8 has compressed pointers as default now.
v8-internal.h "ReadRawField" can help you to understand the background
Currently learning C++ and using Visual Studio 2017. My UWP app have 10 buttons (named b0-b9) and I want to create a function that will manage the content change of the buttons.
For this I need to pass the button name and the content. I want to modify to the function but I don't know how to do it.
It would look something like this:
void contentButtonChange(Button BtnName, String myString)
{
BntName->Content = myString;
}
Main()
{
.....
contentButtonChange(b0, string1);
contentButtonChange(b1, string2);
contentButtonChange(b2, string3);
.....
}
added note: I'm currently able to change the Content of the button from the Main but I'm unable to write a function that will accept a Button as a parameter. I'm always getting an error no matter what I try.
In the example above BtnName in the function is highlighted with the error: expression must have a pointer or handle type
I found how to do it. I need to add this to my function call:
Windows::UI::Xaml::Controls::Button^ btnName
like this:
void contentButtonChange(Windows::UI::Xaml::Controls::Button^ btnName, Platform::String^ myString)
{
bntName->Content = myString;
}
works now.
You need to use TextBlock to set to the Button.
void contentButtonChange(Button BtnName, string myString)
{
BntName->Content = new TextBlock() { Text = myString };
}
I'm trying to add proton::work function (opening a new sender) inside the work queue of the proton::connection object. I have a pointer to the working queue, but my problem is how to bind the open_sender function correctly.
I'm aware of the real problem here : the parameter of the function :
sender open_sender(const std::string& addr);
As the string is passed by reference, I have to de-reference it. I'm ok with that, but how to do it with the proton tools ?
Here my line of code :
proton::work w = proton::make_work( &proton::connection::open_sender, &m_connection, p_url);
Note :
Of course I'm not using C++11 in my project, it would be too simple
to ask ;) !
Of course I cannot change to C++11
If you have a better idea on how to create a new sender in a multi-threaded program let me know.
Usually you will use the proton::open_sender API from within the handler for connection open or container start so you will not have to use proton::make_work in most cases. If you look at the Proton C++ examples, a good place to start is simple_send.cpp.
Abbreviated code might look like this:
class simple_send : public proton::messaging_handler {
private:
proton::sender sender;
const std::string url;
const std::string addr;
...
public:
simple_send(...) :
url(...),
addr(...)
{}
...
// This handler is called when the container starts
void on_container_start(proton::container &c) {
c.connect(url);
}
// This handler is called when the connection is open
void on_connection_open(proton::connection& c) {
sender = c.open_sender(addr);
}
...
}
int main() {
...
simple_send send(...);
proton::container(send).run();
...
}
There are other examples that come with Proton C++, that should help you figure out other ways to use Proton C++. See https://github.com/apache/qpid-proton/tree/master/examples/cpp.
There is also API documentation you can find at http://qpid.apache.org/releases/qpid-proton-0.20.0/proton/cpp/api/index.html (for the current release as of February 2018).