windows mobile窗口之间传递数据的方法

windows mobile上设计UI的时候,经常会碰到多个窗口的情况。有时候,我们需要将一个窗口中的用户输入信息反应到另一个窗口中去,这就涉及到窗口之间的数据传递问题。下面就分nativemanaged这两种方式来讲讲这个窗口之间数据传递的方法。

1. Native

这个方法是当时我在看MFC时学到的,当时应用的场景是这样的。我建立了一个基于对话框的应用程序(Dialog-based),一些参数设置需要另外一个对话框来呈现给用户,在用户输入完之后,就需要将数据传递给主对话框。当时我是这么实现的:

a)stdafx.h头文件中,加入用户定义消息和传递的数据结构,如:

#defineWM_LOCALADDR_SET  WM_USER+21 //本地STC地址设置消息定义

typedef struct tagADDRESS {

  int nAddr0;

    int nAddr1;

    int nAddr2;

    int nAddr3;

} ADDRESS;

 

b)在地址设置对话框的实现文件中,加入“OK”按钮的消息处理函数:

voidCLocalAddrSet::OnButtonApply()

{

        // TODO: Add your control notification handler code here

        UpdateData(TRUE);

        ADDRESSrp;

        rp.nAddr0=m_intLocalAddr0;

        rp.nAddr1=m_intLocalAddr1;

        rp.nAddr2=m_intLocalAddr2;

       rp.nAddr3=m_intLocalAddr3;

        //发送消息

        AfxGetMainWnd()->SendMessage(WM_LOCALADDR_SET, 0, (LPARAM) &rp);

      }

c)在主对话框的头文件中,加入消息映射函数声明:

afx_msgLRESULTLocalAddrSetPrco(WPARAMwParam,LPARAMlParam); //本地设备地址设置消息映射函数声明

d)在主对话框文件(.cpp)中,加入消息映射和对应的实现函数:

ON_MESSAGE(WM_LOCALADDR_SET,LocalAddrSetPrco)   //本地设备地址设置处理映射

//本地地址设置

LRESULTCSlaverDectorDlg::LocalAddrSetPrco(WPARAMwParam,LPARAMlParam)

{

   ADDRESS*prp= (ADDRESS*)lParam;

        inti;

        intHEC=0;//头校验

    ……….

}

 

2. Managed

Managed方式中,有三种方法,分别是:使用构造器传递数据、通过属性传递通过事件(参数)传递。具体可以参考《windows mobile应用与开发》7.2节。

a)使用构造器传递数据

使用构造器可以向一个新窗体传递初始值,如在主窗体Form1的基础上创建Form2,我们就可以在Form2的构造函数中,接收一个初始的值,并显示在对应的label中,代码如下:

        publicForm2(stringdefaultUser)

        {

             InitializeComponent();

 

             m_txtUserName.Text = defaultUser;

        }

Form1中,我们可以使用如下的方法来创建Form2

             Form2f =newForm2("guest");

             DialogResultr = f.ShowDialog();

这样就把guest信息传递到了Form2中。

b)通过属性传递

Form2添加属性,通过为属性赋值和读取属性值来进行数据传递。首先为Form2添加公共属性,代码如下:

        #region使用属性

        publicstringUserName

        {

             get

             {

                  returnm_txtUserName.Text;

             }

             set

             {

                  if(value==null)

                      m_txtUserName.Text =String.Empty;

                  else

                      m_txtUserName.Text =value;

             }

        }

        #endregion

然后在Form1中,显示Form2之前,为其属性赋值,并且在对话框关闭之后,读取该属性的值,从而完成数据的传递。代码如下:

             Form2f =newForm2("guest");

 

             f.UserName = m_userName;

 

             DialogResultr = f.ShowDialog();

             if(r ==DialogResult.OK)

             {

                  m_userName = f.UserName;

                  m_lblMessage.Text =String.Format("Hello, {0}!", f.UserName);

             }

             else

                  m_lblMessage.Text ="Please Login...";

c)通过事件(参数)传递

首先,给Form2添加事件参数类型和事件处理委托的定义,代码如下:

        #region使用事件

        publiceventLoginEventHandlerLogin;

 

        publicclassLoginEventArgs:EventArgs

        {

             stringm_userName;

 

             publicstringUserName

             {

                  get

                  {

                      returnm_userName;

                  }

                  set

                  {

                      if(value!=null)

                           m_userName =value;

                      else

                           m_userName =String.Empty;

                  }

             }

 

             publicLoginEventArgs(stringuserName)

             {

                  this.UserName = userName;

             }

        }

 

        publicdelegatevoidLoginEventHandler(objectsender,LoginEventArgse);

        #endregion

然后,在Form2中合适的地方,即用户希望触发数据传递时,触发这个事件,代码如下:

             if(this.Login !=null)

                  this.Login(this,newLoginEventArgs(m_txtUserName.Text));

 

             this.DialogResult =DialogResult.OK;

同样,在Form1中,除了需要创建并显示Form2之外,还要为Form2的对象添加一个事件处理方法,代码如下:

             Form2f =newForm2("guest");

 

             f.Login +=newForm2.LoginEventHandler(f_Login);

             f.ShowDialog();

上面讲述的三种方法,效果都是一样的,如下图所示:

 

 

134 Comments so far

  1. <a href="<http://groups.google.com/group/buy-best-generic-valium>">buy valium</a>
  2. <a href="<http://groups.google.com/group/order-generic-valium---us-pharmacy>">buy valium online</a>
  3. <a href="<http://groups.google.com/group/low-prices-on-soma-only-here>">soma</a>
  4. <a href="<http://groups.google.com/group/low-prices-on-soma-only-here>">buy soma online</a>
  5. <a href="<http://groups.google.com/group/low-prices-on-soma-only-here>">buy soma online</a>
  6. <a href="<http://groups.google.com/group/low-prices-on-soma-only-here-e>">buy soma online</a>
  7. <a href="<http://drugss009.com/s342-minomycin.html>">buy minomycin</a>
  8. <a href="<http://drugss009.com/s342-zyvox.html>">order zyvox</a>
  9. <a href="<http://drugss009.com/s342-monodox.html>">buy monodox</a>
  10. <a href="<http://drugss009.com/s342-depakote.html>">cheap depakote</a>
  11. <a href="<http://drugss009.com/s342-lamictal+dispersible.html>">buy lamictal</a>
  12. <a href="<http://drugss009.com/s342-dilantin+er.html>">dilantin</a>
  13. <a href="<http://drugss009.com/s342-tegretol.html>">buy tegretol</a>
  14. <a href="<http://drugss009.com/s342-lamictal.html>">lamictal</a>
  15. <a href="<http://drugss009.com/s342-topamax.html>">topamax</a>
  16. <a href="<http://drugss009.com/s342-lamictal+dispersable.html>">dispersable</a>
  17. <a href="<http://drugss009.com/s342-minomycin.html>">buy minomycin online</a>
  18. <a href="<http://drugss009.com/s342-zyvox.html>">zyvox</a>
  19. <a href="<http://drugss009.com/s342-monodox.html>">online monodox</a>
  20. <a href="<http://drugss009.com/s342-asendin.html>">buy asendin online</a>
  21. <a href="<http://drugss009.com/s342-pamelor.html>">buy pamelor online</a>
  22. <a href="<http://drugss009.com/s342-pamelor.html>">buy pamelor online</a>
  23. <a href="<http://drugss009.com/s342-pamelor.html>">order pamelor</a>
  24. <a href="<http://drugss009.com/s342-aventyl.html>">aventyl</a>
  25. <a href="<http://drugss009.com/s342-paxil.html>">cheap paxil</a>
  26. <a href="<http://drugss009.com/s342-celexa.html>">online celexa</a>
  27. <a href="<http://drugss009.com/s342-paxil+cr.html>">online paxil</a>
  28. <a href="<http://drugss009.com/s342-cymbalta.html>">buy cymbalta</a>
  29. <a href="<http://drugss009.com/s342-prozac.html>">prozac</a>
  30. <a href="<http://drugss009.com/s342-prozac.html>">prozac</a>
  31. <a href="<http://drugss009.com/s342-desyrel.html>">desyrel</a>
  32. <a href="<http://drugss009.com/s342-rapiflux.html>">buy rapiflux</a>
  33. <a href="<http://drugss009.com/s342-effexor.html>">buy effexor online</a>
  34. <a href="<http://drugss009.com/s342-remeron.html>">buy remeron</a>
  35. <a href="<http://drugss009.com/s342-effexor+xr.html>">effexor</a>
  36. <a href="<http://drugss009.com/s342-sarafem.html>">cheap sarafem</a>
  37. <a href="<http://drugss009.com/s342-elavil.html>">cheap elavil</a>
  38. <a href="<http://drugss009.com/s342-seroquel.html>">cheap seroquel</a>
  39. <a href="<http://drugss009.com/s342-endep.html>">order endep</a>
  40. <a href="<http://drugss009.com/s342-sinequan.html>">buy sinequan online</a>
  41. <a href="<http://drugss009.com/s342-geodon.html>">buy geodon online</a>
  42. <a href="<http://drugss009.com/s342-wellbutrin+sr.html>">buy wellbutrin</a>
  43. <a href="<http://drugss009.com/s342-lexapro.html>">lexapro</a>
  44. <a href="<http://drugss009.com/s342-lexapro.html>">lexapro</a>
  45. <a href="<http://drugss009.com/s342-zoloft.html>">cheap zoloft</a>
  46. <a href="<http://drugss009.com/s342-loxitane.html>">order loxitane</a>
  47. <a href="<http://drugss009.com/s342-videx.html>">online videx</a>
  48. <a href="<http://drugss009.com/s342-famvir.html>">famvir</a>
  49. <a href="<http://drugss009.com/s342-atacand.html>">order atacand</a>
  50. <a href="<http://drugss009.com/s342-atacand-hctz.html>">buy atacand</a>
  51. <a href="<http://drugss009.com/s342-esidrix.html>">buy esidrix online</a>
  52. <a href="<http://drugss009.com/s342-minipress.html>">buy minipress</a>
  53. <a href="<http://drugss009.com/s342-moduretic.html>">buy moduretic online</a>
  54. <a href="http://drugss009.com/s342-capoten.html">buy capoten online</a>
  55. <a href="http://drugss009.com/s342-cardizem.html">order cardizem</a>
  56. <a href="http://drugss009.com/s342-cardizem+er.html">cardizem-er</a>
  57. <a href="http://drugss009.com/s342-nebilet.html">nebilet</a>
  58. <a href="http://drugss009.com/s342-coreg.html">cheap coreg</a>
  59. <a href="http://drugss009.com/s342-plendil.html">plendil</a>
  60. <a href="http://drugss009.com/s342-dilacor.html">online dilacor</a>
  61. <a href="http://drugss009.com/s342-dilacor+cr.html">online dilacor-cr</a>
  62. <a href="http://drugss009.com/s342-tiazac+er.html">buy tiazac-er online</a>
  63. <a href="http://drugss009.com/s342-diltiazem+xl.html">buy diltiazem-xl</a>
  64. <a href="http://drugss009.com/s342-enalapril.html">buy enalapril online</a>
  65. <a href="http://drugss009.com/s342-isordil+sublingual.html">cheap isordil-sublingual</a>
  66. <a href="http://drugss009.com/s342-tricor.html">cheap tricor</a>
  67. 75WPMw <a href="http://rfjhpeteswpz.com/">rfjhpeteswpz</a>, ekwkivpmyrhf, [link=http://bkzncmausyho.com/]bkzncmausyho[/link], http://bkcymmzmfbte.com/
  68. <a href="http://rxxxdrugs.com/item.php?group_id=57&id=267">cialis viagra soft tabs</a>
  69. <a href="http://rxxxdrugs.com/item.php?group_id=57&id=251">cialis soft tabs 10 mg</a>
  70. <a href="http://genericsss-eu.com/buy-viagra+soft.html">online viagra soft</a>
  71. <a href="http://cardiovascular-generics.com/sorbitrate.htm">Generic Sorbitrate Online</a>
  72. <a href="http://womens-health-drugs/comduphaston.htm">duphaston medicine</a>
  73. <a href="http://womens-health-drugs.com/comestrace.htm">estrace ring</a>
  74. <a href="http://womens-health-drugs.com/compremarin-28s.htm">premarin 28s</a>
  75. Aloha! <a href="http://trendyafrica.com">adapt vapor limits public conclusions</a> power earth article http://www.era.co.uk
  76. resulted smaller <a href="http://www.richmondeye.com">record variations exert</a> risk 180 http://www.healthychoice.com
  77. nDnttQo
  78. SwEpZc http://trpjnt.com/ <a href="http://hcltqx.com/ ">KwTEmctD</a> KwTEmctD
  79. YgFUnoJ <a href="http://carsonator.com/ ">Viagra</a> CdIzU <a href="http://www.fotacor.com/ ">Cialis</a> >:-[
  80. fIahgV <a href="http://lilahorovitz.com/ ">Viagra</a> 0861 <a href="http://www.mnnapo.org/ ">Buy Cialis Online</a> >:-OOO
  81. HvBhQWXF <a href="http://www.bluebirdsdownunder.com/ ">Viagra</a> %-[[[ <a href="http://www.briancallphoto.com/ ">Cialis</a> =-]
  82. kApnyqx <a href="http://www.15yearsinproduction.com/ ">Viagra</a> QrpEdj <a href="http://www.canakkalespor.org/ ">Buy Phentermine</a> 3814 <a href="http://www.anesisgaming.com/ ">Buy Cialis</a> 5459
  83. EMJtOWp <a href="http://www.hoolapoppers.com/ ">Viagra</a> aVSRd <a href="http://www.hoteladmiralclubbeograd.com/ ">Cheap Cialis</a> 0919
  84. tDcmxu <a href="http://www.hoolapoppers.com/ ">Cheap viagra</a> >:]] <a href="http://www.hoteladmiralclubbeograd.com/ ">Buy Cialis</a> 2193

leave a reply

All fields marked with " * " are required.