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

标签 c++ wxwidgets

我目前正在尝试实现一个过度使用 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/

相关文章:

c++ - 如何用 C++ 解析复杂的字符串?

c++ - 使用 wxAutomationObject 将内存位图传递给 Powerpoint?

macos - Alien::wxWidgets 在 OSX 10 上安装失败

c++ - 从wxWidgets过渡到QT需要注意的重要事项

haskell - react 香蕉 : Alternate buttons events

c++ - wxDialog 与 wxMenuBar

c++ - PCL OpenNI2Grabber 在没有查看器的情况下获取点云

c++ - 使用 boost::bind 时出现 "no match for call"错误

C++ boolean 数组初始化

c++ - 在 C++ 中创建对象