Renovating a WinForms app

1:56 PM
Renovating a WinForms app -

whitehouserenovation

This is a republication of Citrixblogger.org.

Everything must be updated later. What I want to share is how to update a WinForms application to support mobile devices. This post covers the basics of things to worry about.

  • size and position of forms and controls
  • orientation
  • Fonts
  • buttons
  • not mobile friendly controls

the size and position

the first concept to understand that, contrary to standard desktops, mobile devices are different sizes. And, the size can change at any time based on changing the orientation or the keyboard display on the screen. Assuming a fixed size and the layout is a bad thing to do. The application needs to adjust to display changes. Not only the need for forms to be resized, but the content needs a new layout. This step can be a little difficult for a standard Windows Forms.

Landscape

Portrait

This usually occurs when application startup and during orientation changed events. The application must gather information and then apply that information against the container (shape) and content (controls). It must also ensure that he gets updates when they occur (events).

Since we are using WinForms, we use the CMP.NET component SDK to access information and hold events. CMP.NET is contained in the assembly Citrix.CMP.dll and is located in the same directory as the application. It is important to remember that some Mobile SDK for Windows Apps files are allowed to be distributed with applications that use them. The namespace is Citrix.CMP and documentation is available online on Citrix.CMP docs website

The typical location for all Citrix.CMP.dll is :.

 C:  Program Files (x86)  Citrix  MobilitySDK  bin  AnyCPU 

for use with your WinForms program, you need to reference from the Visual Studio project. You should also include a reference to help simplify the name


using Citrix.Cmp ;.

For the interface, it must be initialized. Overall, it just means creating a new object.


///
/// CMP.NET object for Citrix Mobility Pack
///
CmpApi cmpApi = null;

/ / create object CMP.NET
cmpApi CmpApi = new ();

To test whether CMP.NET is actually available, you need to call the IsCmpAvailable SessionManager. The CmpApi object can be created when there is no connection to Citrix Receiver. This can happen either when the session is disconnected or the receiver does not support mobile extensions.


///
/// internal flag to reveal whether SDK is available
/// cmpAvail bool = false;

// confirm that the Citrix Mobility Pack is active
if (cmpApi.SessionManager.IsCmpAvailable == true)
{
cmpAvail = true;
}
other
{
cmpAvail = false;
}

This can be wrapped in a simple function


///
/// Determines whether we have a mobile device available
///
/// bool - true (mobile) false (not mobile)
public bool isMobile ()
{
back (cmpAvail);
}

This isMobile () function can be widely used to determine the application's behavior. For example, the provision can be determined on the basis of this result. It can also be used to ignore mobile applications related to the functions that attempt to use the SDK.


///
/// Get the current state of the orientation
/ //
/// OrientationState
Public OrientationState GetOrientationState ( )
{
OrientationState state = null;

if (isMobile ())
{
cmpApi.Display.GetOrientationState state = ();
}

back (state);
}

You only need to get an instance of CmpApi for an application. In addition, you must create the object on the main UI thread to ensure the event notification. If you do not, there's a good chance you will not receive the events you want. In the example of using the info Mobile program, I just created a class to manage interfaces with the SDK. I did it for several reasons, but the main reason was to better control what was used from the interface. Instead of changing many different places related to the SDK, with primary class, it is easier to manage and standardize. There is no obligation to do so, but it can make things more user friendly


using System ;.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citrix.Cmp;
using System.Diagnostics;

namespace MobileUserInfo
{
///

/// Wrapper class for calls for mobility. The goal is to reduce what is what is used
/// and also simplify to a small degree.
///

public class MobileAPI
{
///

/// CMP.NET object for Citrix Mobility Pack
// /

private CmpApi cmpApi = null;

// /

/// internal flag to reveal whether SDK is available
///

bool private cmpAvail = false;

///

/// Constructor for MobileAPI object
///

Public MobileAPI ()
{
// CMP.NET create the object
cmpApi CmpApi = new ();

// confirm that the Citrix Mobility Pack is active
if (cmpApi.SessionManager.IsCmpAvailable == true)
{
cmpAvail = true;
}
other
{
cmpAvail = false;
}
}

///

/// Determines if we have an available mobile device
///

/ // bool - true (mobile) false (not mobile)
public bool isMobile ()
{
back (cmpAvail);
}

This is only a small part of what the class contains. To see more, you will have to wait until the sample is available.

Returning to the question of the size of the shape and position, we will use some SDK API to get what we want. There are a couple of concepts to explain. Their are two main ways to determine the size of the mobile screen. The first is GetDisplayState. The second technique is to use GetViewportInfo. The difference between the two is that GetViewportInfo is more descriptive of the area that the application can use. GetDisplayState simply returns the entire screen size. The information window contains: ServerViewport, ClientViewport and zoomFactor

Viewport information
Viewport article Type Description
customer viewport Rectangle space available on mobile devices
viewport Server rectangle server zone currently displayed
zoom factor Integer current zoom factor (default = 100 steps zoom)

The field we want is customer viewport. With this information, we can define the size of the form. The origin of the client window is always (0,0). The width and the height will vary depending on the size of the device, orientation, and the shortcut key. To collect the information of the current window, call GetViewport.


///

/// Get the parameters of the current window
///

/// ViewportInfo
public ViewportInfo GetViewport ()
{
ViewportInfo state = null;

if (isMobile ())
{
state = cmpApi.Display. GetViewport ();
}

back (state);
}

to apply to the form, simply change the size

is form.


///

/// Move the form to the origin and the correct size for mobile device
///

private void PlaceForm ()
{
ViewportInfo Viewport MobileAPI.GetViewport = ();

if (! (Viewport = null) && (Viewport.ClientViewport.HasValue))
{
Size = new Size ((int) Viewport.ClientViewport.Value.Width, (int) Viewport .ClientViewport.Value.Height);
}

// make sure that the form is on screen at 0.0
= new Location (0, 0);
}

This is the approach used when the form is created, but we also need to know when customer viewport changes. It is an event for changes viewport that can be hung. It is called ViewportInfoChanged. The event provides new information in the viewport arguments


///

/// Hang in ViewportInfoChanged
///

/// ViewportInfoChanged event handler public void HookViewportInfoChanged (EventHandler viewportEventHandler)
{
if (isMobile ())
{
+ = cmpApi.Display.ViewportInfoChanged viewportEventHandler;
}
}

Form init
...
MobileAPI.HookViewportInfoChanged (ViewportChanged);
...

/ /
/ / every time the window changes, we need to readjust our form
/ /
empty ViewportChanged private (object sender, args ViewportInfoChangedArgs)
{
// if it has changed, so we need to re-layout of our fields
= Viewport args.NewState;

ReflowForm ();
}

If the changes viewport, we will know and the form will be resized

Regarding the controls, something new needs happen. Or controls must be moved and resized or we need to create a new way to view the content. Originally, when I was researching the best way to handle this, it seemed that the use of real control was the right direction. Over time, this changed to prefer refactoring techniques. The main reason why is that by refactoring the information he gave a better result. The end result is a collection of code that can handle both approaches.

Given that I work on this post on and off for the last three days, I will continue in the next post for more details.

Have you noticed the renovation of the White House? It is the picture up and connects to the history of this serious renovation between 1948 and 1952. Some of the photos reveal that eviscerated the White House. I promise that you will not have to empty your WinForms application to play well on mobile devices. The idea here is to make things as easy as possible.

In this post, we covered

  • Where to get CMP.NET
  • How to include CMP .NET
  • How to get an object CMP
  • Viewport explained
  • Viewport event hooking
  • form resizing
  • Concept of mobile and non-mobile code

No as far as I was planning for this position, but a good start. The next post should arrive in a few days.

Previous
Next Post »
0 Komentar