首页  ·  知识 ·  编程语言
微软.NET精简框架(NETCF)最常见问题(2)
佚名  www.weste.net  .NET  编辑:dezai  图片来源:网络
2.5. 为什么从TextBox上调用CreateGraphics会失败? 只有Form类才支持Control.CreateGraphics(). 2.6. 怎样获得屏幕上文字的大小?

2.5. 为什么从TextBox上调用CreateGraphics会失败?

只有Form类才支持Control.CreateGraphics().

2.6. 怎样获得屏幕上文字的大小?

使用Graphics的MeasureString方法。以下代码说明如何在文字周围画一个方框:


//C#
using System.Drawing;

protected override void OnPaint(PaintEventArgs e)
{
  string s = "Hello World"

  Pen pen = new Pen(Color.Fuchsia);
  Font font = new Font("Arial", 18, FontStyle.Regular);
  Brush brush = new SolidBrush(Color.Black);

  SizeF sSize = e.Graphics.MeasureString(s, font);

  Rectangle r = new Rectangle(9, 199,(int)sSize.Width + 1, (int)sSize.Height + 1);

  e.Graphics.DrawRectangle(pen, r);
  e.Graphics.DrawString(s, font, brush, 10.0f, 200.0f);

  base.OnPaint (e);
}

'VB
Imports System.Drawing

Protected Overrides Sub OnPaint(e As PaintEventArgs)
  Dim s As String = "Hello World"
 
  Dim pen As New Pen(Color.Fuchsia)
  Dim font As New Font("Arial", 18, FontStyle.Regular)
  Dim brush = New SolidBrush(Color.Black)

  Dim sSize As SizeF = e.Graphics.MeasureString(s, font)

  Dim r As New Rectangle(9, 199, Fix(sSize.Width) + 1, Fix(sSize.Height) + 1)

  e.Graphics.DrawRectangle(pen, r)
  e.Graphics.DrawString(s, font, brush, 10F, 200F)

  MyBase.OnPaint(e)

End Sub 'OnPaint


2.7. Can I set the width of a pen?

Setting the pen width is not available in the .NET Compact Framework. Some alternate solutions include:

 

Drawing filled rectangles with the Graphics.FillRectangle method
Drawing multiple lines next to each other
Writing a custom graphics routine with GAPI
2.8. How do I zoom an image?

While there is no inherent support for zooming or stretching a single image, these effects can be achieved quite easily by creating a new Bitmap object with an associated Graphics object and copying the desired portion of the original Bitmap into it. The following sample creates two bitmaps of the same size, where the second contains a zoomed center section of the first, provided the project has an embedded resource named MyImage.bmp. This same technique could be used to stretch images by modifying the source and destination rectangles such that they do not maintain their original aspect ratio.

//C#
using System.Drawing;
using System.Reflection;

Bitmap m_bmpOriginal;
Bitmap m_bmpZoom;

private void Form1_Load(object sender, System.EventArgs e)
{
    Assembly asm = Assembly.GetExecutingAssembly();
    m_bmpOriginal = new Bitmap(asm.GetManifestResourceStream(asm.GetName().Name
      + ".MyImage.bmp"));

    // Take the center quarter of m_bmpOriginal
    // and create stetch it into m_bmpZoom of the same size
    m_bmpZoom = new Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height);
    Graphics gZoom = Graphics.FromImage(m_bmpZoom);
   
    Rectangle srcRect = new Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4,
      m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2);
    Rectangle dstRect = new Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height);
    gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel);
}

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawImage(m_bmpOriginal, 0, 0);
    e.Graphics.DrawImage(m_bmpZoom, 125, 0);
    base.OnPaint (e);
}

'VB
Imports System.Drawing
Imports System.Reflection

Private m_bmpOriginal As Bitmap
Private m_bmpZoom As Bitmap

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
    m_bmpOriginal = New Bitmap(asm.GetManifestResourceStream((asm.GetName().Name _
      + ".MyImage.bmp")))

    ' Take the center quarter of m_bmpOriginal
    ' and create stetch it into m_bmpZoom of the same size
    m_bmpZoom = New Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height)
    Dim gZoom As Graphics = Graphics.FromImage(m_bmpZoom)

    Dim srcRect As New Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4, _
      m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2)
    Dim dstRect As New Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height)
    gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub 'Form1_Load

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    e.Graphics.DrawImage(m_bmpOriginal, 0, 0)
    e.Graphics.DrawImage(m_bmpZoom, 125, 0)
    MyBase.OnPaint(e)
End Sub 'OnPaint


2.9. Why can't I load an image?

Ensure that imgdecmp.dll is in the device's Windows directory.

For more information, see the topic "1.31. How do include imgdemp.dll in an emulator image?" of this FAQ.

 

 

--------------------------------------------------------------------------------


3. 发布

3.1. 怎样建立一个PocketPC的设置程序?

这篇文章告诉您如何建立一个单独的.msi文件,可以运行并把应用安装到不同的Pocket PC设备上。整个过程都是自动的,所以很容易把所有需要的组件都打包到.msi文件中。包含C#和Microsoft Visual Basic .NET代码。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfdeployment.asp

3.2. 哪个发布包可以包括在我的安装程序中?

您可以为您的用户提供一个最终的发布包,以帮助他们升级设备。您不能拆开这个发布包把内容给您的用户。但是,您可以拆开开发人员的发布包把内容给您的用户。

3.3. 怎样可以建立一个不需要.net框架的安装程序?

这篇文章讨论了如何成功的安装一个Pocket PC的应用:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnppc2k/html/ppc_allation.asp

3.4. 怎样建立不受PocketPC系统约束的CAB安装文件?

您可以建立一个.inf文件生成一个适合任何Pocket PC设备的应用程序安装文件。查看示例代码:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/cabfile.aspx

3.5. 为什么不同的处理器会有不同的CAB安装文件?

每一个CAB文件都包含一小段检测智能设备上的.net精简框架版本的代码。这个功能是处理器/平台特有的,不同的CAB文件根据处理器类型绑定不同的代码。

3.6. How do I create a desktop installer that detects the presence of the .NET Compact Framework on the target device and installs it if necessary?

The article titled "Creating an MSI Package that Detects and Updates the .NET Compact Framework" in the MSDN Library describes a technique that may be used:
http://msdn.microsoft.com/mobility/understanding/articles/default.aspx?pull=/library/en-us/dnnetcomp/html/netcfdepl.asp

3.7. How do I deploy a .NET Compact Framework Service Pack to the emulator?

Download and install to your desktop development PC a "Developer" version of the service pack (the download title will read something like: "Microsoft?? .NET Compact Framework 1.0 SPx Developer Redistributable") from:
http://msdn.microsoft.com/mobility/downloads/updates/default.aspx

The next step is to copy the appropriate .NET Compact Framework cab file (as per next paragraph) to the emulator. From within the emulator point File Explorer to a share on your PC and then copy and paste the cab to somewhere on the emulator's file system. Now launch the cab file from File Explorer and answer "Yes" if asked to overwrite anything.

 

Emulator
 CAB File
 
Pocket PC 2002
 netcf.core.ppc3.x86.cab
 
Windows Mobile 2003 for Pocket PC
 netcf.core.wce4.x86.cab
 
Windows Mobile 2003 for Smartphone
 RAM installs not supported
 


3.8. How do I include SQL Server CE with my application installation?

To install SQL Server CE with an application, simply install the proper SQL Server CE CAB files as part of the application's installation. There are two sets of cabs associated with SQL Server CE.

The developer CAB includes Query Analyzer, and error strings. This CAB should not be included with application deployment. It comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:

sqlce.dev.ppc3..cab
sqlce.dev.wce4..cab

The SQL Server CE CAB, which includes the engine, client agent, and managed extensions for the client agent is required by applications utilizing System.Data.SqlServerCe components. This CAB also comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:

sqlce.ppc3..cab
sqlce.wce4..cab

Applications that access SQL Server, ie applications utilizing System.Data.SqlClient components should deploy the 'sql' CAB. This CAB also comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:

sql.ppc3..cab
sql.wce4..cab

All of these CABs are included in the Visual Studio .NET 2003 Professional Edtion install. The default location is:

\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\...

3.9. How do I use GAPI to create a graphics engine?

This article describes how to create a DLL that wraps GAPI (Game API), such that it is .NET Compact Framework compliant, and use it to create and optimize a basic graphics library in managed code.
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI1.asp

This article expands upon the "Dancing Rectangles" sample by implementing loading and displaying of bitmaps. It also implements some more advanced features such as animated bitmaps, source and destination key transparency, and alpha blending, i.e., translucency.
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI2.asp

This article expands upon the "Dancing Zombies" sample by implementing drawing of points, lines, and custom 1 bit fonts converted from 8 bit bitmaps. It also implements an input system that overrides the functionality of the hardware buttons and tracks button states.
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI3.asp  


--------------------------------------------------------------------------------


4. 图形用户界面(GUI): 窗体

4.1. 怎样建立一个全屏的窗体?

您必须把WindowState属性设置为最大化。做一个看不见的窗体,如把全屏的图片放到窗体中,您需要把FormBorderStyle设置为None,关掉ControlBox删掉窗体中所有的菜单。


//C#
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.ControlBox = false;
this.Menu = null;

'VB
Me.WindowState = FormWindowState.Maximized
Me.FormBorderStyle = FormBorderStyle.None
Me.ControlBox = False
Me.Menu = Nothing


4.2. 什么时候用窗体的构造器?什么时候用窗体的Load事件?

窗体的load功能是做界面操作的最好方法。典型又安全的做法是在构造器中创建数据和控件的实例。任何包含用户界面的控件或窗体的初始化,都应该在 load功能中完成。例如:在窗体的构造器中完成控件对象的建立,然后在load功能中设置控件的位置等 是安全的做法。

4.3. 怎样把最小化按钮换成关闭按钮?

通过开发环境的设计器,可以把窗体的自动最小化模式转变成关闭模式,或者通过变成实现。(x)按钮自动最小化应用程序,(ok)按钮回关闭程序。

在设计器中转化窗体的风格
打开Visual Studio环境的窗体设计器,查看属性。在窗体上右键并选择Properties,在Window Style 部分把MinimizeBox设置为 False。

在代码中转变窗体风格
简单的在窗体的load功能中添加以下一行代码:


//C#
this.MinimizeBox = false;

'VB
Me.MinimizeBox = False


4.4. 怎样在.net精简框架上建立多窗体系统结构?

这篇文章讨论了如何在.net精简框架上为应用程序有效的建立用户界面:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfuiframework.asp

4.5. 怎样提高.net精简框架应用程序的载入速度?

通过下面文章中的优化技巧,减少.net精简框架应用程序的载如时间:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfimproveformloadperf.asp

4.6. 运行时怎样修改窗体的风格?

这篇快速入门教程描述了在pocket pc应用程序中使用代码改变窗体界面:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/ppcformproperties.aspx

4.7. 怎样滚动窗体的内容?

这篇快速入门教程演示了如何使用纵向和横向滚动条 还有如何在窗体中绘制一个图象:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/scrolling.aspx

4.8. 怎样制作一个浮动的窗体?好象所有的窗体总是全屏状态的。

有边界的最顶端的窗体总是全屏的,不能移动或改变大小。没有边界或子窗体可以移动和改变大小。

使用: Form.BorderStyle = BorderStyle.None;  //来设置有无边界

4.9. 怎样强迫一个窗体变成最小化?

请查看本问 "6.6. 如何是窗口一直保持最小化? " 部分。

4.10. 我有一个包含很多控件的智能设备窗体,为什么运行的时候出现"NotSupportedException"错误?

如果你的窗体中包含了很多控件,当你运行的时候可能会有NotSupportedException的错误。如果你是在Debug模式下运行的话,你会发现异常是来自于窗体的InitializeComponent部分。这种情况是由于精简框架CLR在编译类的方法时,会有64kb的限制造成的。这意味着 CLR把方法(如,InitializeComponent)翻译成中间语言时,返回的机器码不能超过64KB。如果超过了64KB, NotSupportedException将被抛出。这也是为什么容易在Debug模式(F5)下导致错误,而非Debug模式(Ctrl+F5)却不容易出错。因为Debug模式运行时会生成更多容量的调试代码。

除了模拟器,您也可能在智能设备上遇到这个错误,因为即时编译代码根据因CPU类型而异的(如,PocketPC使用的是ARM指令,而模拟器上使用的x86指令)

没有一个准确的数字说明,一个窗体中到底可以包含多少个控件。因为不同控件的产生的代码量不一样。如,一个Button控件,比TabControl控件产生的代码要少。还会因由设置了多少属性而异。包含集合的控件,象ListBox或TreeView,如果在设计时向属性框中填入了很多值,编译时将产生大量代码。同样,设置了Localized的窗体(Localizable属性为true),会比没设置localize的窗体产生更多代码。因为 localize需要从资源文件中读取属性值,放在InitializeComponent方法中。

果你遇到这种情况,下面有一些技巧帮助你避免它的发生:


把一个窗体的代码分成多个窗体。过多控件的窗体,会影响程序开始时载入的性能。尽可能把用户界面分成两个或多个窗体。
不要在设计时填充大的、内部的、带集合的控件。如果你把很多节点集合加到TreeView控件中,这样会在InitializeComponent方法中加入大量代码。尽可能把加入集合的代码移到Form.Load事件中。这样做的缺点是,要在设计时编辑这些集合将变得不容易,但它有助于分割代码。
不要把自己的代码添加到InitializeComponent方法中,这对通常的编码都有用,不建议添加、修改设计起生成的代码。这样做会造成设计器不可知的错误。如果你想添加自己的启动代码,你应该在Form.Load事件中做。
运行时初始化类似的控件。比如,有12个Button控件,只是文字和位置不同,你应该考虑使用循环来设置属性,而不是在设计时设置属性。在次,如果你自己写代码来实现它,不要把代码放在InitializeComponent方法中。

编辑InitializeComponent方法的缺点是,在InitializeComponent代码外建立对象实例的代码,将不能在设计器重被设计。同样,如果你手动修改了InitializeComponent中的代码,你会发现,设计器可能不再识别你修改的代码。所以以上技巧的前提是,不要修改InitializeComponent中的代码。

4.11. 哪个是正确关闭窗体的方法:Appplication.Exit 还是 Form.Close ?

Application.Exit是类似Win32平台下的PostQuitMessage()硬性退出。收回所有弹出的信息,释放呼叫堆栈,把执行权返回给系统。

在windows平台(Win32或.NET)下正确关闭应用程序的方法是关闭主窗体(如:Form.Close)。所有主窗体结束时仍存在的窗体需要手工关闭。Any window that's still present after the main message pump ends needs to be manually closed. 好的方法就是在应用程序调用Form.Close或Form.Dispose退出之前,关闭所有窗体。需要记住.NET框架的OnClosing()就是Win32平台下WM_CLOSE的托管版本,而不是WM_DESTROY。

另外,使用form.Close()的话,你的程序可以在OnClosing或OnClosed事件中处理释放资源、关闭文件等操作。如果使用Application.Exit退出,这些事件将不会被触发。

 


4.12. Why does showing a MessageBox on Windows Mobile 2003 for Smartphone with the Abort, Retry, and Fail set of buttons or the Yes, No, Cancel set of buttons, or the third button as the default button result in a NotSupportedException?

Windows Mobile 2003 for Smartphone only supports 1 or 2 button MessageBoxes.

4.13 How do I get an icon in the Pocket PC Start Menu Most Recently Used (MRU) List?

Create a shortcut to your application somewhere under \windows\start menu\programs. When your application is launched from this shortcut an icon for your applications will appear the MRU list.

 

4.14 How do I center a Form on the screen?

In order to display a non-full screen Form, ensure that the Form's FormBorderStyle property is set to FormBorderStyle.None. To center the form add the following code to the Form's FormLoad event handler: Set FormBorderStyle to FormBorderStyle.None then:

//c#
Rectangle screen = Screen.PrimaryScreen.Bounds;
this.Location = new Point((screen.Width - this.Width) / 2,
  (screen.Height - this.Height ) / 2);

'VB
Dim theScreen As Rectangle
theScreen = Screen.PrimaryScreen.Bounds()
Me.Location = New Point((theScreen.Width - Me.Width) / 2, _
  (theScreen.Height - Me.Height) / 2)


4.15 Why can't I show a Form that has been closed?

Once a Form is closed, it is disposed and therefore may be garbage collected by the system so it is not safe to attempt to show a closed Form. An alternative solution is to use Form.Hide and Form.Show to hide and display Forms respectively.

4.16 How can I enable multiple instances of an application?

Multi-instancing is not supported by the .NET Compact Framework. The following code sample provides a solution that allows applications to be instanced rather than maximized when an application is launched but a running instance already exists.

Note: The following code is not supported and is not guaranteed to work on all versions of the OS, including future versions.

// C#
using System.Runtime.InteropServices;
using System.Reflection;

private void Form1_Load(object sender, System.EventArgs e)
{
    this.Text = string.Format("Form {0}", new Random().Next());
}

[DllImport("CoreDll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("CoreDll")]
public static extern int SetWindowText(IntPtr hWnd, string lpString);

protected override void OnResize(EventArgs e)
{
    Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
    IntPtr hWnd = FindWindow("#NETCF_AGL_PARK_",
      asm.GetModules()[0].FullyQualifiedName);

    if (hWnd != IntPtr.Zero)
        SetWindowText(hWnd, "#42");

    base.OnResize (e);
}

'VB
Imports System.Runtime.InteropServices
Imports System.Reflection

Private Sub Form1_Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Text = String.Format("Form {0}", New Random().Next())
End Sub 'Form1_Load

_
Public Shared Function FindWindow(ByVal lpClassName As String, _
  ByVal lpWindowName As String) As IntPtr
End Function

_
Public Shared Function SetWindowText(ByVal hWnd As IntPtr, _
  ByVal lpString As String) As Integer
End Function

Protected Overrides Sub OnResize(ByVal e As EventArgs)
    Dim asm As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
    Dim hWnd As IntPtr = FindWindow("#NETCF_AGL_PARK_", _
      asm.GetModules()(0).FullyQualifiedName)
    If hWnd.ToInt32() <> IntPtr.Zero.ToInt32() Then
        SetWindowText(hWnd, "#42")
    End If
    MyBase.OnResize(e)
End Sub 'OnResize

 

 

--------------------------------------------------------------------------------


5. 图形用户界面 (GUI): 通用

 


5.1. 怎样建立一个带有图形或支持多行的按钮?

建立一个带有图形或支持多行的按钮需要使用自定义控件。自定义控件能继承button的paint方法,以及其他任何需要的自定义数据。参考以下连接获得更多关于自定义控件的信息:

http://msdn.microsoft.com/library/en-us/dnnetcomp/html/customctrlscompactfx.asp

快速入门教程告诉你如何建立一个带图像的按钮:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/picturebutton.aspx

5.2. TextBox.AcceptsReturn在.net精简框架下是怎样工作的?

虽然设置了AcceptsReturn为false,但它还是按true的方式来操作。你可以写一个继承TextBox的类,在KeyPress事件中实现对Enter的处理。

5.3. 当数据载入ComboBox的时候,为什么SelectedIndexChanged事件没有被触发?

这是一个已经知道的问题,将在以后的.net精简框架中发布。

5.4. Show和ShowDialog有什么不同?

ShowDialog会把一个窗体以 模式 方式显示,这是一种独占调用方式,知道窗体关闭才会返回。这个方法将返回一个DialogResult枚举,表示关闭的条件。
Show是一种非独占的调用方式,和显示一个控件一样,可以立刻返回,没有返回参数。显示一个控件意味着Visible属性被设置为true,直到Hide方法被调用,Visible方法才会变为false。

5.5. 为什么我不能建立右键菜单分离器?(separator)

这是一个已经知道的BUG,把右键菜单设置为分割线,将抛出NotSupportedException错误。这个问题是由于WinCE系统有个限制,不允许在已经加入右键菜单的菜单项设置为分隔符,并且菜单的父类是一个控件。在Visual Studio 2003种,设计器分割移动应用代码的方式和PC上的应用程序类似。这是导致此问题的原因。解决的方法是,把右键菜单单独放在InitilizeComponent方法外的地方。

5.6. 运行时把ImageList赋给ToolBar时,为什么图象不出现?

你可以在窗体载入的时候把ImageList分配给ToolBar,但重新应用图像在ToolBar上的顺序。在设置ToolBar的ImageList之前 设置ToolBar按钮的图像顺序是不被支持的。

5.7. 怎样把光标设置成等待状态?

这段代码可以把光标设置成等待光标:


//C#
Cursor.Current = Cursors.WaitCursor;

'VB
Cursor.Current = Cursors.WaitCursor


这段代码可以把光标设置为默认:


//C#
Cursor.Current = Cursors.Default;

'VB
Cursor.Current = Cursors.Default


5.8. 如何在菜单项中显示符号:&?

这项功能还不被.net精简框架支持。使用"&&"不会在菜单项的文字中显示"&"符号。

5.9. 如何制作基于.net精简框架的动画控件?

这篇文章将告诉你如何制作基于.net精简框架的动画控件:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/animationcontrol.asp

5.10. 如何在.net精简框架上制作自定义控件?

学习制作.net精简框架控件,提高您的技巧。(文章附带了自定义控件的示例代码):
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/customctrlscompactfx.asp

5.11. 如何制作基于.net精简框架的带图片的按钮?

这篇文章讨论了建立基于.net精简框架的带图片的按钮:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/ImageButton.asp

5.12. 如何使用.net精简框架下的MessageWindow类库?

学习如何使用.net精简框架 MessageWindow 类建立一个提示图标:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/messagewindow.asp

这篇快速入门实现了,当鼠标点击矩形自定义控件或点击Panel控件时,使用MessageWindow把消息发送给发送窗体:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/messagewindow.aspx

5.13. 如何向DataGrid中添加行和列?

这篇快速入门教程解释了如何在运行时向DataGrid控件添加或删除行、列:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagridadding.aspx

5.14. 如何实现DataGrid的编辑?

.net精简框架中的DataGrid控件提供了几乎.net框架中的DataGrid控件的所有功能。一个主要的区别是.net精简框架中的DataGrid不能在运行时编辑单元。这篇快速入门教程演示了如何通过程序实现编辑单元格的一种方法:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagridediting.aspx

5.15. 怎样设置DataGrid的DataSource?

与.net框架的DataGrid的另一个区别是,.net精简框架的DataGrid不支持把DataSource设置为DataSet。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagrid.aspx

5.16. 怎样对DataGrid的列排序?

与.net框架的DataGrid的另一个区别是,.net精简框架的DataGrid不支持在运行时按照列进行排序。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagridsorting.aspx

5.17. 怎样对ListView中的内容排序?

.net精简框架不支持ListView.Sort方法,但任然可以排序。这篇快速入门教程定义了一个继承ArrayList.Sort的IComparable接口的方法:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/listviewsort.aspx

5.18. 如何使用 输入板(SIP)控件?

这篇快速入门教程演示了在PocketPC上打开和关闭软输入板(SIP),以及当SIP显示时,tab控件大小也跟随变化:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/inputpanel.aspx

多个窗体应该共享一个输入板对象。可以通过先在主窗体中建立SIP对象,然后把它传给子窗体或暴露SIP对象的一些方法、属性给其他需要使用SIP的窗体。

5.19. 如何自定义事件?

这篇快速入门教程描述了如何继承Button类、重载方法来事现双击事件。这个自定义事件会在按钮被双击时触发,两次点击的间隔时间是SystemInformation.DoubleClickTime 属性的值,以毫秒为单位。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/btndclick.aspx

.net精简框架的控件不支持OnEnter和OnLeave方法,包括Windows.Forms.Control基类。但是,因为支持Control.onMouseMove方法,您可以通过它和Control.Capture 属性判断鼠标什么时候进入和离开控件。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/enterleave.aspx

5.20. 如何做一个 owner-drawn List Box?

您可以制作一个.net精简框架的owner drawn list box。.net精简框架的ListBox或其他控件不支持DrawMode、DrawItem, 或其他drawing方法,但您可以编程实现。这篇快速入门教程提供一个自定义控件类,建立一个owner-drawn list box,并实现了选择字体的控件的功能。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/ownerdrawnlistbox.aspx

5.21. 如何做一个多选框判断真假?

这篇快速入门教程提供了在Windows.Forms.CheckBox控件上建立真/假多选框:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/tfcheckbox.aspx

5.22. 设置InputPanel.Enabled = true的时候为什么出现异常?

InputPanel组件需要窗体包含MainMenu控件,而且那个窗体是显示在屏幕上的。

5.23. 为什么自定义控件不会自动继承父类的字体?

这个功能不被.net精简框架所支持。

5.24. 为什么当输入字符时,NumericUpDown 和 DomainUpDown 控件不会引发 ValueChanged 和 SelectedItemChanged 事件?

在代码中改变控件的值 或 按下了上、下箭头才会触发ValueChanged和SelectedItemChanged事件。当用户往控件中输入字符的时候时不会触发的。

5.25. 为什么NumericUpDown控件增长的值不是设置好的值?

当您按了上、下后出现的值,不是增长值的倍数,它将向着那个方向(上或下)直到下一个增长值的倍数的值。

5.26. 为什么StatusBar不能放在窗体的任意位置?一定要在底部?

StatusBar控件只能停靠在窗体的底部,它的大小不能改变。

5.27. 为什么我的控件自动继承了父控件的背景色?

这个功能不被.net精简框架所支持。可以采取的方法是继承OnParentChanged方法手动设置颜色:


//C#
protected override void OnParentChanged(EventArgs e)
{
  base.OnParentChanged(e);
  this.BackColor = Parent.BackColor;
}

'VB
Protected Overrides Sub OnParentChanged(ByVal e As EventArgs)
  MyBase.OnParentChanged(e)
  Me.BackColor = Parent.BackColor
End Sub 'OnParentChanged


5.28. 为什么NumericUpDown控件能接受decimal类型的值,但不会显示大于2^16的值?

虽然NumericUpDown控件接受decimal类型的值,但.net精简框架把这个控件的值当作int类型来处理。如,10.23当作10。同样此控件在PocketPC上不接受大于带符号的16位整型。

5.29. 为什么不能在DomainUpDown 中输入文字,而要选择?

DomainUpDown控件不会对输入的文字进行确认(不象完整的.net框架)。如果您先输入了一些文字,再按上、下箭头,它会显示内容改变前的值的下一个值。

5.30. 为什么OpenFileDialog被限制在"My Documents" 文件夹中?

OpenFileDialog的初始化目录被限制在"My Documents"文件夹或它的子文件夹中。这个限制是由PocketPC系统强加的,为了帮助用户在标准目录下管理自己的文档。
  5.31. How can I activate the SIP (InputPanel) without a menu?

The SIP can be activated by P/Invoking the function "SipShowIM" as follows.

//C#
using System.Runtime.InteropServices;

const uint SIPF_OFF = 0x0;
const uint SIPF_ON = 0x1;

[DllImport("coredll.dll")]
private extern static void SipShowIM(uint dwFlag);

'VB
Imports System.Runtime.InteropServices

Const SIPF_OFF As Integer = &H0
Const SIPF_ON As Integer = &H1

_
Private Shared Function SipShowIM(ByVal dwFlag As Integer) As Integer
End Function


5.32. How do I add a subnode to every node in a TreeView?

Adding subnodes to all nodes is accomplished by iterating through all of the nodes in the TreeView and adding a new node to each.


//C#
foreach (TreeNode node in treeView1.Nodes)
{
    node.Nodes.Add(new TreeNode("SubNode"));
}

'VB
Dim node As TreeNode
For Each node In  treeView1.Nodes
    node.Nodes.Add(New TreeNode("SubNode"))
Next node


5.33. How do I determine the number of rows or columns in a DataGrid?

The number of rows and columns in a DataGrid can be determined from the data source itself. For example:


//C#
DataSet ds = new DataSet();

int numRows = ds.Tables[0].Rows.Count;
int numCols = ds.Tables[0].Columns.Count;

'VB
Dim ds As New DataSet()

Dim numRows As Integer = ds.Tables(0).Rows.Count
Dim numCols As Integer = ds.Tables(0).Columns.Count


If the DataGrid is bound to the DataView you can also use DataView.Count.

5.34. How do I create a owner drawn Listbox?

See the .NET Compact Framework QuickStarts, Implementing Events topic:

http://samples.gotdotnet.com/quickstart/compactframework/doc/btndclick.aspx

5.35. How can I implement Control.GetNextControl under the .NET Compact Framework?

The tab order of the controls in the .NET Compact Framework correspond directly to the order of the Controls in the Form.Controls collection. Therefore, GetNextControl can be implemented by determining the index of the specified Control and determing its neighbors in the collection.

//C#
public Control GetNextControl(Control ctl, bool forward)
{
    int curIndex = this.Controls.IndexOf(ctl);

    if (forward)
    {
        if (curIndex < this.Controls.Count)
            curIndex++;
        else
            curIndex = 0;
    }
    else
    {
        if (curIndex > 0)
            curIndex--;
        else
            curIndex = this.Controls.Count - 1;
    }

    return this.Controls[curIndex];
}

'VB
Public Function GetNextControl(ByVal ctl As Control, _
  ByVal forward As Boolean) As Control
    Dim curIndex As Integer = Me.Controls.IndexOf(ctl)

    If forward Then
        If curIndex < Me.Controls.Count Then
            curIndex += 1
        Else
            curIndex = 0
        End If
    Else
        If curIndex > 0 Then
            curIndex -= 1
        Else
            curIndex = Me.Controls.Count - 1
        End If
    End If
   
    Return Me.Controls(curIndex)

End Function 'GetNextControl


5.36. How do I get notified when the user clicks on a treeview node?

TreeView does not support the Click event, however, a workaround is to use the AfterSelect event instead.

5.37. How do I set the title of a fullscreen multiline edit control window?

This is not supported by the current version of the .NET Compact Framework.

5.38. Why don' I see the validItem selected when I set ComboBox.SelectedValue to validItemInCollection?

Setting the SelectedValue property only works if the control is databound.

5.39. How do I detect the location where a 'tap & hold' occurred to bring up a context menu on my custom control?

Handle the ContextMenu.Popup event, and then query the current mouse coordinates using 'Control.MousePosition'.

5.40. Why doesn't the scrollbar value ever get set to the maximum value?

Similar to the NumericUpDown control, the maximum achievable value is the first empty row above the thumb. More specifically, from the editor properties, this equates to:

Maximum - (LargeChange + 1).

5.41. How do I tab out of a custom control to the previous control?

Call this.Parent.Controls(this.Parent.GetChildIndex(customcontrol) - 1).Focus() in the KeyDown event handler when a Keys.Up key is detected.

5.42. How do I add Toolbar buttons with transparency?

Icons support transparency, however, there is a known bug in Visual Studio .NET 2003 designer that creates incorrect code and makes icons non-transparent. A work around is to add an icon file to the ImageList outside of InitializeComponent and add the icon files to the project as content or embedded resources. The following code demonstrates this:

//C#
using System.Drawing;
using System.IO;
using System.Reflection;

// Loaded as content example
private void Form1_Load(object sender, System.EventArgs e)
{
    this.imageList1.Images.Add(new Icon(File.Open("fullFileName.ico",
      FileMode.Open)));

    this.toolBar1.Buttons[0].ImageIndex = 0;
}

// Loaded as a resource example
private void Form1_Load(object sender, System.EventArgs e)
{
    this.imageList1.Images.Add(new
      Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(
      ".filename.ico")));

    this.toolBar1.Buttons[0].ImageIndex = 0;
}

'VB
Imports System.Drawing
Imports System.IO
Imports System.Reflection

' Loaded as content example
Private Sub Form1_Load1(ByVal sender As Object, ByVal e As System.EventArgs)

    Me.imageList1.Images.Add(New Icon(File.Open("fullFileName.ico", _
      FileMode.Open)))

    Me.toolBar1.Buttons(0).ImageIndex = 0

End Sub 'Form1_Load1

' Loaded as a resource example
Private Sub Form1_Load2(ByVal sender As Object, ByVal e As System.EventArgs)

    Me.imageList1.Images.Add(New _
      Icon([Assembly].GetExecutingAssembly().GetManifestResourceStream( _
      ".filename.ico")))

    Me.toolBar1.Buttons(0).ImageIndex = 0

End Sub 'Form1_Load2


 


--------------------------------------------------------------------------------


6. 与本地代码(Native Code)互用

6.1. 如何调用本地代码写的DLL中的函数?

本地DLL代码可以通过系统的Invoke (P/Invoke)方法调用。这些文章提供了如何实现调用和更多的调用技巧:


学习如何使用.net精简框架的Platform Invoke (P/Invoke)细节:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfintrointerp.asp
深入探索.net精简框架下的互用性。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfadvinterop.asp

学习如何通过P/Invoke建立智能设备上的非托管代码。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/unmanagedfuncs.asp

如何在.net精简框架上汇集托管和非托管代码的数据。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfmarshallingtypes.asp

学习使用工具dumpbin.exe在微软.net精简框架的应用中申明P/Invokes。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfdumpbinpinvoke.asp

6.2. 如何使用dumpbin.exe帮助描述 P/Invokes?

见本问答的 "6.1. 如何调用本地写的DLL中的函数? " 章节。

6.3. 如何为微软.net精简框架写非托管代码?

见本问答的 "6.1. 如何调用本地写的DLL中的函数? " 章节。

6.4. 如何调用 P/Invoke GetTickCount?

不需要使用P/Invoke调用GetTickCount功能,因为Environment.TickCount就提供了这个功能。
见本问答的 " 7.2. 如何计算精确的时间间隔?" 章节。

本文作者:佚名 来源:www.weste.net
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读