At MIX 10, Microsoft gave a first look at developer platform for new Windows
Phone 7. They have designed this new phone operating system ground up starting
from approach to hardware to the user experience for the phone and from app
platform to tools that let you build new applications.This article will focus on
the developer platform, which is based on Silverlight ad XNA, aspect of this new
phone. Both Silverlight and XNA platforms expose set of APIs that let developers
use C# to build rich applications or games. On top of base Silverlight/ XNA
platform, Windows Phone 7 application platform adds functionality that is phone
specific. Windows Phone Developer Platform consists of four big components (NETCF:
set of base class libraries, Silverlight: Presentations/Media framework, XNA:
Game framework, Windows Phone specific APIs).Developers will be able to use
familiar Silverlight tools such as Microsoft VS 2010 and Microsoft Expression
Blend. Friction free acquisition was the primary goals for these tools. It means
there is one simple setup that gets you everything that you need and you can get
started with the Windows Phone at zero cost. For this new express edition of
Visual Studio, Microsoft Visual Studio Express for Windows Phone is created. It
provides free tooling to get started on Windows Phone 7development.This set of
tools has a completely revised device emulator which lets you run your
applications in Windows Phone OS without having Windows Phone device. This
emulator is not only much faster than the previous version but provides high
fidelity environment since it is running Windows Phone 7 OS and supports
graphics acceleration.
Direct Hit! |
Applies To:Silverlight, XNA, Windows Phone 7,WPF |
Navigation Model
Navigation based UX model is one of the core concepts for Windows Phone 7
and one of the three keys a developer will find on every phone that runs Windows
Phone 7 is the back key. Just like web, Navigation refers to the ability to link
pages with hyperlinks where the user can navigate back and forth.
Namespace: System.Windows.Navigation
Creating a linked page can be as simple as writing a line of XAML or code. Or
NavigationService.Navigate(new Uri("/Page1.xaml",
UriKind.Relative));
Developer could also pass query parameters via URIs.
NavigationService.Navigate(new
Uri("/Page1.xaml?username=vivekd", UriKind.Relative));
One the target page (Page1.xaml) in this case, user can get access to this
query parameter: string username; if (NavigationContext.QueryString.TryGetValue("username",
out username))
textBlock1.Text = username;
Media
MediaElement class can play Media (Play(), Stop()) by either
taking URI or Stream to media source as input.
mediaElement1.Play();
mediaElement1.Stop();
Media on Windows Phone is hardware accelerated which not only makes it fast but
also power efficient. On phone, SL applications can also take advantage of
sounds APIs from XNA for supporting polyphonic sounds.
Windows Phone Developer Plateform Components |
WebBrowser
WebBrowser control lets you host HTML inside your application without having
to launch a separate browser process for scenarios such as advertisement or RSS
feeds or emails.
Namespace: Microsoft.Phone.Control
x:Class="WindowsPhoneApplication1.MainPage"
...
xmlns:browser="clrnamespace:
Microsoft.Phone.Controls;assembly=Microsoft.Phone
.Controls.WebBrowser">
This XAML will host the MSN.com home page within a Windows Phone Application.
WebBrowser control could also load html from string using WebBrowser control's
Navigate-ToString(string) method.
string html = //get a stream of html from website or local html
and convert it to string
webBrowser1.NavigateToString(html);
WebBrowser control has APIs that let the application developer interact with
JavaScript inside the html page that is hosted. This functionality is disabled
by default but can be enabled by setting IsScriptEnabled property to true. When
this is enabled, C# code in WP7 application can call into Javascript using
Invoke- Script method. Javascript uses window.external.Notifty method to push
data to managed application. Developer needs to subscribe to ScriptNotify event
to receive this data.
function CallFromSilverlight(input)
{
textbox.value = input;
return true;
}
function CallFromScript()
{
window.external.Notify(textbox.value);
}
If we had html that contained above script then following code will call
CallFromSilverlight function in Javascript and pass in arguments from managed C#
application to that function.Javascript on the page
webBrowser1.InvokeScript("CallFromSilverlight", arg);
In the ScriptNotify event handler, NotifyEventArgs will contain the actual
data passed in by Javascript.
private void webBrowser1_ScriptNotify(object sender, NotifyEventArgse)
{
textBox1.Text = e.Value;
}
Accelerometer
Accelerometer sensor lets the developer figure out the orientation of the
device in the space, by sending samples of g-forces along X, Y and Z axis with a
timestamp. This is particularly useful in games such as car racing where device
orientation is used as an input for actions in games.
Namespace: Microsoft.Devices.Sensors
acc = AccelerometerSensor.Default;
acc.ReadingChanged += new EventHandler(
acc_ReadingChanged);
acc.Start();
In the ReadingChanged event handler you would use
AccelerometerReadingAsyncEventArgs to get to actual values of gforces along
different axis. void acc_ReadingChanged(object sender, Accelerometer-
ReadingAsyncEventArgs e)
{ Dispatcher.BeginInvoke(() =>
{
//physics engine code
});
}
Launchers and Choosers
Launchers and Choosers are the way to leverage in built OS experiences for
certain tasks such as taking a photo, or selecting a contact.
- Launchers let the developer launch to an OS application. E.g. Developer wants
to send email programmatically.
- Choosers are more like functions; they do some task and then come back to
application with data. E.g. Twitter application wants to choose a photo from
album and post the photo.
Namespace: Microsoft.Phone.Tasks
PhotoChooserTask photopicker = new PhotoChooserTask(); photopicker.Show();
Developer needs to call Show() method on a Task. Chooser returns the data to
page (PhoneApplicationPage class) via OnChooserReturn virtual method.public
override void OnChooserReturn(object sender, EventArgs e)
{
TaskEventArgs args = e as TaskEventArgs;
if (args != null && args.TaskResult == TaskResult.OK &&
args.Result.ChosenPhoto != null)
{
Stream photoStream = args.Result.ChosenPhoto;
//use the stream to display photo
}
}
Stream for Data returned by chooser is available via TaskEventArgs.Results APIs.
Location
Location: Actual location could be decided via a variety of different
methods such as WiFi, GPS or cell phone tower triangulation. Location Service
can map geo location to civic address. Mapping is one of the primary
applications on today's smartphones. A developer using Location APIs is not
aware of actual method used for location is abstracted out. Developer needs to
provide desired accuracy (Low or high) while using location APIs and right
method is used.
Namespace: System.Device.Location
GeoCoordinateWatcher watcher = new GeoCoordinate-
Watcher(GeoPositionAccuracy.Low);
watcher.PositionChanged += new EventHandler>( watcher_PositionChanged);
CivicAddressResolver resolver = new CivicAddressResolver();
resolver.ResolveAddressCompleted += newEventHandler(
resolver_ResolveAddressCompleted);
watcher.Start(); Developer will hook into PositionChanged event of
GeoCoordinateWatcher class and get access to location data via
GeoPositionChangedEventArgs.
void watcher_PositionChanged(object sender, GeoPosition- ChangedEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
resolver.ResolveAddressAsync(e.Position.Location);
}
);
}
Developer will use CivicAddressResolver. ResolveAddressAsynch where raw geo
coordinates are passed to cloud service which will resolve the position in
longitude/ latitude and map it to civic address. ResolveAddressCompleted event
handler gets called with resolved civic address void
resolver_ResolveAddressCompleted(object sender, ResolveAddressCompletedEventArgs
e)
{
Dispatcher.BeginInvoke(() =>
{ //use e.Address.AddressLine1 etc APIs to get the actual address
}
);
}
Push Notification
Scenarios such as news or stock updates require user to get notification on
the phone even when application is not running. Push notification service solves
this problem by providing an URI where app service could post data whenever
event happens. After this point, Microsoft Push service will take care of
delivering this data as a notification to the phone. Sequence of action
depicting steps that happen between applications opening channel for push
notification to data getting delivered to device.
Namespace: Microsoft.Phone.Notification
HttpNotificationChannel channel = new HttpNotificationChannel("
channelforvivek");
channel.Open();
channel.ChannelUriUpdated += new EventHandler(
channel_ChannelUriUpdated);
Developer will call Open () method on an instance of HttpNotificatioChannel.
Once the push services returns an URI to developer via ChannelUriUpdated event,
developer will need to pass on the URI to the service. After this point, service
will post the new data to the URI and it will get delivered to phone.Sequence of
action depicting steps that happen between applications opening channel for push
notification to data getting delivered to device.
Related Articles:
Win Phone 7: Data Binding & Storing Persistent Data Writing code