现在的位置: 首页 > 综合 > 正文

Symbian多视图程序开发及视图管理

2013年10月16日 ⁄ 综合 ⁄ 共 2411字 ⁄ 字号 评论关闭

在大多数GUI的Applications中,视图之间的切换是极其频繁的。那么如何组织这些视图以及视图之间的通讯,就成为大多数Applications所要面对的问题。

在Symbian中多视图应用程序之间的视图管理可以用下图来表示:

其中AppUI管理AppView,而AppView又管理Container。

AppUI继承自CAknViewAppUi,其管理AppView的代码如下(在void CViewTestAppUi::ConstructL()中):

CAppView1* iView1 = new(ELeave) CAppView1;
CleanupStack::PushL( iView1 );
videoView->ConstructL();
AddViewL( iView1 );
CleanupStack::Pop();// view1

CAppView2* iView2 = new(ELeave) CAppView2;
CleanupStack::PushL( iView2 );
userView->ConstructL();
AddViewL( iView2);
CleanupStack::Pop();// view2

SetDefaultViewL(*iView1);

而其中每个View又继承自:

class CAppView1: public CAknView , public MCoeControlObserver

class CAppView2: public CAknView , public MCoeControlObserver

View之间要想实现自由的切换,就必须定义一个TUid作为自身的唯一标识并实现CAknView的方法

// UID of view
const TUid KViewId = {1};     

/**
* From CAknView returns Uid of View
* @return TUid uid of the view
*/
TUid Id() const;

那么从一个View跳转到另一个View就需要下面的代码:

((CAknViewAppUi*)(iEikonEnv->AppUi()))->ActivateLocalViewL( TUid::Uid( 3 ));

static_cast<CApplicationAppUi*>(iEikonEnv->AppUi())->ActivateLocalViewL( TUid::Uid( 3 ));

 

上面的代码讲述了AppUI如何管理众多的View以及View之间的切换,那么下面讲述每个View是如何管理它里面众多的Containers的

首先要明白View继承自CAknView,它做为Container(继承自CCoeControl)的容器,它可以通过以下的代码激活某个Container:

iContainer = new (ELeave) CApplicationContainerSetting;       
iContainer->ConstructL( KFullScreen );
iContainer->SetMopParent(this);
iContainer->SetObserver(this);
iContainer->SetApplicationDbEngine(iDbEngine);
AppUi()->AddToStackL( *this, iContainer );

那么在DoActivateL()中通过以上代码放置初始显示的Container。

那么View是如何管理众多的Container的呢?

从以上代码可以看出,负责Container管理的View都继承自MCoeControlObserver,而iContainer->SetObserver(this)则把当前的Container注册到MCoeControlObserver,从而实现了View对其内部Container的监听。

View中继承自MCoeControlObserver的接口void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)则负责每个Container所报告的事件的处理。

注意:如果View中并没Container,那么就没必要继承自MCoeControlObserver。

而Container继承自CCoeControl(它是HandleControlEventL的一个参数),CCoeControl中提供下面的方法对View中的HandleControlEventL()报告事件。

  

case EStdKeyDevice1: //右键
  {
   ReportEventL((MCoeControlObserver::TCoeEvent)ECmdDeleteVideoContainer);
   break;
  }

而View通过以下方法对上面代码所报告的事件进行处理。

void CAppView1::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
{
switch(aEventType)
{

case EMenuItemCmdBack:
  {
   ((CAVASAppUi*)(iEikonEnv->AppUi()))->HandleCommandL(EEikCmdExit);
   break;
  }
case ECmdDeleteVideoContainer:
  {
   break;
  }
default:
  break;
}
}

从上面的分析可以看出:

AppView通过继承自CAknView并通过ActivateLocalViewL()切换不同的View。

而AppView通过继承自MCoeControlObserver监听众多的Container,并在HandleControlEventL()里对Container所报告的事件进行响应。

抱歉!评论已关闭.