Programming of Handheld and Mobile Devices

advertisement

Programming of Handheld and Mobile

Devices

Lecture 19 Microsoft’s Approach 1 – .NET

Mobile Framework part 2

Rob Pooley rjp@macs.hw.ac.uk

Programming Handheld and

Mobile devices

1

Simple C# examples for .NET

{

Class Form1 static void Main()

{

Application.Run(new Form1());

} private void menuItem2_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

}

Programming Handheld and

Mobile devices

2

A second form

MyNewForm mynewformForm = new MyNewForm (); mynewformForm.ShowDialog();

Programming Handheld and

Mobile devices

3

Passing Values to Controls in Other

Forms

• Let's demonstrate setting a control's value by altering a text label.

The sequence of events is:

1. Create a new instance of the Form that contains the control (but don't display it).

2. Change the property corresponding to the text label of the control.

3. Display the Form.

• Assume we have added a text label called label1 to the new Form, and we can do all that with some code like this:

MyNewForm mynewformForm = new MyNewForm(); mynewformForm.label1.Text = "Hello World"; mynewformForm.ShowDialog();

• The only thing to remember is that the label1 property, by default, will be private so the code for the main Form won't be able to see it.

• You get around this by setting it to Public

Programming Handheld and

Mobile devices

4

What you see

Note The size of the Form is not important as the Pocket PC will always expand it to fill the screen.

Programming Handheld and

Mobile devices

5

Adding a button

• If you are creating a new Form that acts as a dialog box, you should also add some way for the user to close it, such as a new button with code like the following attached: private void button1_Click(object sender,

System.EventArgs e)

{ this.Close();

}

• The new Form will have a close button in the top right for the user to click

Programming Handheld and

Mobile devices

6

Programming Handheld and

Mobile devices

7

Adding tabs

• tabs provide access to multiple pages all stored on one dialog.

• These can be very useful if your application has a lot of settings for the user to work through.

• Use the TabPage Collection to add and rename new tab pages.

• Each page is a like a blank form to which you add controls.

• Treat each tab page as a Form, and place the controls on it as you see fit.

• The Pocket PC will look after displaying the pages and controls

Programming Handheld and

Mobile devices

8

Namespaces supported in .NET Framework and .NET Compact Framework

Programming Handheld and

Mobile devices

9

Windows Forms

• All the common features Windows Forms in the .NET Framework are present in .NET Compact

Framework. These features are differently implemented in a separate manner to make them more efficient for size and performance.

• Supported are:

• Common controls such as:

– Buttons

– Listboxes

– Tree-views

– Listviews

– Combo boxes

– Textboxes

– Picture boxes

– Scroll bars

– Labels

• Bitmaps

• Pens

• Brushes

• Colors

• Drawing

• Fonts

• Support for Forms

Programming Handheld and

Mobile devices

10

Networking with TCP etc.

• The System.Net.Sockets namespace is used to provide an interface to access the transport layer of protocol stacks.

• Multiple protocols can be exposed through this class.

• In addition, .NET Compact Framework provides additional classes that simplify common developer tasks encapsulating much of the necessary code that is common across all TCP client/server applications.

• Some of these are:

– TCPListener

– TCPClient

– UDPClient

Programming Handheld and

Mobile devices

11

HTTP

• HttpWebRequest and HttpWebResponse classes provide a rich HTTP client.

• These classes also support many of the standard mechanisms for encryption and authentication such as

SSL/TLS and basic HTTP authentication.

• Other Web requests can be implemented using other protocols such as:

– WebRequest interface

– WebResponse interface

– IwebRequestCreate interface

Programming Handheld and

Mobile devices

12

System.Threading Namespace

• The System.Threading

namespace provides classes and interfaces that enable multithreaded programming.

• In addition to classes for synchronizing thread activities and access to data ( Mutex , Monitor , Interlocked ,

AutoResetEvent , and so on), this namespace includes a

– ThreadPool class that allows you to use a pool of system-supplied threads, and a

– Timer class that executes callback methods on thread pool threads.

Programming Handheld and

Mobile devices

13

Threading

using System; using System.Threading;

// Simple threading scenario: Start a static method running

// on a second thread. public class ThreadExample

{

// The ThreadProc method is called when the thread starts.

// It loops ten times, writing to the console and yielding

// the rest of its time slice each time, and then ends. public static void ThreadProc() {

}

} for (int i = 0; i < 10; i++) {

Console.WriteLine("ThreadProc: {0}", i);

// Yield the rest of the time slice.

Thread.Sleep(0); public static void Main() {

Console.WriteLine("Main thread: Start a second thread.");

// The constructor for the Thread class requires a

ThreadStart

// delegate that represents the method to be executed on the

// thread. C# simplifies the creation of this delegate.

Thread t = new Thread(new

ThreadStart(ThreadProc));

// Start ThreadProc. On uniprocessor, thread does not get

// any processor time until the main thread yields.

//Uncomment

// Thread.Sleep that follows t.Start() to see the difference. t.Start();

//Thread.Sleep(0); for (int i = 0; i < 4; i++) {

Console.WriteLine("Main thread: Do some work.");

Thread.Sleep(0);

}

Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends."); t.Join();

Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");

Console.ReadLine();

}

}

Programming Handheld and

Mobile devices

14

.NET vs J2ME vs Palm OS

• Problems

– Palm OS is the most cumbersome to program

– Palm OS uses C/C++

– Palm OS and .NET

Compact are both specific to certain kinds of device

– J2ME requires a JVM

(KVM or CVM)

– J2ME is not really compatible with J2SE

• Benefits

– Palm OS is available for many PDAs

– J2ME is the most portable

– J2ME has wireless features as standard

– J2ME uses Java

– .NET is higher level

– .NET supports XML

– .NET supports several languages

Programming Handheld and

Mobile devices

15

Download