概述
這個代碼片段示範如何使用Document Handler API(CDocumentHandler)來打開檔案。下面示範了兩種不同的技術:
在該檔案的處理應用程式中獨立打開
在調出應用程式中嵌入打開檔案
MMP檔案
下面的能力和庫檔案是必須的:
CAPABILITY SwEvent // TApaTask::SwitchOpenFile() (僅獨立版)
LIBRARY apgrfx.lib // TApaTaskList, TApaTask (僅獨立版)
LIBRARY apmime.lib // TDataType
LIBRARY commonui.lib // CDocumentHandler
頭檔案(獨立版)
#include <DocumentHandler.h>
class CMyAppUi : public CAknAppUi
{
// ...
private: // Private functions
void LaunchFileL(const TDesC& aFilename);
void RefreshDocumentFileL(const TUid& aUid, const TDesC& aFileName);
private: // Data
CDocumentHandler* iDocHandler;
}
源代碼檔案(獨立版)
欲在獨立的檔案進行中打開檔案,使用CDocumentHandler::OpenFileL()方法。另外,下面的代碼示範若已經在處理器中打開則如何更新檔案。
void CMyAppUi::ConstructL()
// Create the document handler
iDocHandler = CDocumentHandler::NewL(CEikonEnv::Static()->Process());
void CMyAppUi::LaunchFileL(const TDesC& aFilename)
TDataType emptyDataType = TDataType();
// Open a file in a standalone handler application
iDocHandler->OpenFileL(aFilename, emptyDataType);
TUid handlerUid;
TInt err = KErrNone;
err = iDocHandler->HandlerAppUid(handlerUid);
if (!err)
{
RefreshDocumentFileL(handlerUid, aFilename);
}
/**
* 重新整理獨立處理器中的已打開檔案。若檔案尚未打開則什麼也不做。
*/
void CMyAppUi::RefreshDocumentFileL(const TUid& aUid, const TDesC& aFileName)
TApaTaskList taskList(iCoeEnv->WsSession());
TApaTask task = taskList.FindApp(aUid);
// If the standalone handler is already running, then update the file
if (task.Exists())
User::LeaveIfError(task.SwitchOpenFile(aFileName));
頭檔案(嵌入版)
#include <aknserverapp.h> // MAknServerAppExitObserver
class CMyAppUi : public CAknAppUi, public MAknServerAppExitObserver
private: // Functions from base classes
/**
* From MAknServerAppExitObserver.
* Handles the exit of a connected server application.
*/
void HandleServerAppExit(TInt aReason);
void LaunchFileEmbeddedL(const TDesC& aFilename);
};
源代碼檔案(嵌入版)
以嵌入方式調出檔案,使用CDocumentHandler::OpenFileEmbeddedL()方法:
void CMyAppUi::LaunchFileEmbeddedL(const TDesC& aFilename)
//Set the exit observer so HandleServerAppExit will be called
iDocHandler->SetExitObserver(this);
//Open a file embedded
iDocHandler->OpenFileEmbeddedL(aFilename, emptyDataType);
void CMyAppUi::HandleServerAppExit(TInt aReason)
//Handle closing the handler application
MAknServerAppExitObserver::HandleServerAppExit(aReason);
後置條件
由aFilename所指的檔案用CDocumentHandler打開。
<a href="http://wiki.forum.nokia.com/index.php/%E4%BD%BF%E7%94%A8CDocumentHandler%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6">http://wiki.forum.nokia.com/index.php/%E4%BD%BF%E7%94%A8CDocumentHandler%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6</a>