gpt4 book ai didi

c++ - 使用 wxWebViewHandler 注册一个协议(protocol)

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:55 26 4
gpt4 key购买 nike

我目前正在尝试实现一个过度使用 wxWebView 的应用程序。我使用静态页面以及存储/缓存在用户 PC 上的动态页面。对于应用程序,我希望使用两种方案/协议(protocol):

static://profile://

目标是让他们处理本地内容,但更容易解决这些问题。例如静态站点的翻译代码:

wxString Mainframe::GetStaticContent(wxString file)
{
// Choose based on language
wxString language = wxGetApp().GetSelectedLanguage();

// Construct Filename
wxFileName fname(wxGetApp().GetAppPath() + wxString("/static/") + language + wxString("/") + file + wxString(".html"));

// Return full path, which is corrected by wxWidgets
return fname.GetFullPath();
}

使用它的 url 看起来像这样:

static://start

现在我有一个派生自 wxWebViewHandler 的类来实现这一点:

class WebViewHandlerStatic : public wxWebViewHandler
{
public:
WebViewHandlerStatic(const wxString& protocol, Mainframe* parent)
: wxWebViewHandler(protocol), m_parent(parent)
{
m_fs = new wxFileSystem();
}

virtual WebViewHandlerStatic::~WebViewHandlerStatic()
{
wxDELETE(m_fs);
}

virtual wxFSFile* GetFile (const wxString &uri)
{
wxString content = uri.substr(9, uri.length()).BeforeLast('/');
wxString path = m_parent->GetStaticContent(content);

if ( wxFileName::FileExists(path) && wxFileSystem::HasHandlerForPath(path) )
{
//path = wxFileSystem::FileNameToURL(path);
return m_fs->OpenFile(path);
}
else
return NULL;
}

private:
Mainframe* m_parent;
wxFileSystem* m_fs;
};

但是,问题是如果我单击链接 static://about 或任何其他具有该协议(protocol)的链接,什么也不会发生。我尝试了使用和不使用 FilenameToURL()。我还检查了调试器,路径在构建后有效。但是页面没有变化。

有人对此有任何见解吗?

编辑:

临时解决方法:

class WebViewHandlerStatic : public wxWebViewHandler
{
public:
WebViewHandlerStatic(const wxString& protocol, Mainframe* parent)
: wxWebViewHandler(protocol), m_parent(parent)
{
//m_fs = new wxFileSystem();
}

virtual WebViewHandlerStatic::~WebViewHandlerStatic()
{
//wxDELETE(m_fs);
}

virtual wxFSFile* GetFile (const wxString &uri)
{
wxString content = uri.substr(9, uri.length()).BeforeLast('/');
wxFileName path = m_parent->GetStaticContent(content);
wxString url = path.GetFullPath();

if ( wxFileSystem::HasHandlerForPath(url) )
{
m_parent->LoadURL(url);
// The content of url is in this state: "d:\Source\wxSteamStats\bin\static\en_GB\about.html" (Copied from debugger)
}
return NULL;
}

private:
Mainframe* m_parent;
wxFileSystem* m_fs;
};

这可以完美无缺地工作,没有任何异常(exception)。所以我不太确定为什么这里需要 wxFileSystem 或者它在内部是如何工作的。看了组件的代码,对IE的API不熟悉。对我来说,看起来实际文档从未提供给 IE。还是我必须将源“馈送”到控件的某个地方?我无法在 wxArchiveWebHandler 或示例的源代码中发现类似的内容。

最佳答案

我刚刚使用下面的类对此进行了测试,该类对您的类做了一些小改动。

class WebViewHandlerStatic : public wxWebViewHandler
{
public:
WebViewHandlerStatic(const wxString& protocol) : wxWebViewHandler(protocol)
{
m_fs = new wxFileSystem();
}

virtual WebViewHandlerStatic::~WebViewHandlerStatic()
{
wxDELETE(m_fs);
}

virtual wxFSFile* GetFile (const wxString &uri)
{
wxFileName path = "C:/Users/Steven/Desktop/doc.htm";
wxString url = wxFileSystem::FileNameToURL(path);

if (wxFileSystem::HasHandlerForPath(url))
return m_fs->OpenFile(url);
else
return NULL;
}
private:
wxFileSystem* m_fs;
};

然后我按照示例中的另一个示例注册了它,它工作得很好。主要区别在于我使用了 wxFileSystem::FileNameToURL,因为 wxFileSystem 需要 URI 而不是文件路径。我怀疑当您取消对它的调用的注释时,当您传递 wxString 而不是 wxFileName 时,会发生一些狡猾的隐式转换。尝试从 Mainframe::GetStaticContent 返回 fname,然后调用 FileNameToURL,希望它能正常工作。

关于c++ - 使用 wxWebViewHandler 注册一个协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com