Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Windows Phone 7: Navigating Between Pages
1. Windows
Phone:
Naviga1on
between
Pages
Jussi
Pohjolainen
Tampere
University
of
Applied
Sciences
2. Programming
Environments
• Two
programming
environments
– XNA
–
for
games
– Silverlight
–
non-‐games
• You
can
mix
some
of
the
libraries
• When
crea1ng
a
project,
you
must
decide
if
you
are
doing
a
XNA
or
Silverlight
3. Silverlight:
for
apps
and
u1li1es
• Silverlight
apps
are
combina1on
of
code
and
markup
• Extensible
markup
language
(XAML),
pronounced
“zammel”
4. XNA:
for
games
• High
performance
games:
2D
and
3D
• Sprites
and
backgrounds
based
on
bitmaps
• Moving
objects
around
screen,
polling
user
input,
built-‐in
XNA
loop
6. App.xaml.cs:
App
wide
init,
startup
and
shutdown
namespace PhoneApp8
{
public partial class App : Application
{ Par1al
class!
So
public App() another
App-‐
class
must
be
{ somewhere!
...
InitializeComponent();
...
7. App.xaml
• App.xaml
and
App.xaml.cs
files
together
are
the
App-‐class
• How
can
.xaml
file
be
part
of
.cs?
– During
compila1on
• App.xaml
-‐>
App.g.cs
!
• g
stands
for
generated
8. App.g.cs
namespace PhoneApp8 {
public partial class App : System.Windows.Application {
private bool _contentLoaded;
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this, new System.Uri("/
PhoneApp8;component/App.xaml", System.UriKind.Relative));
}
}
}
9. App.xaml
<Application
x:Class="PhoneApp8.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
Store
</Application.Resources>
resources:
colors,
styles..
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
10. MainPage.xaml.cs
• When
app
is
ini1alized
the
App
class
opens
the
Main
Page
• Again
par1al
class!
The
other
half
is
in
MainPage.xaml.cs
namespace PhoneApp8
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
}
11. MainPage.cs
• Contains
visual
elements
of
the
main
page
PhoneApplicationFrame
PhoneApplicationPage
Grid named “LayoutRoot”
StackPanel named “TitlePanel”
TextBlock named “ApplicationTitle”
TextBlock named “PageTitle”
Grid named “ContentPanel”
12.
13. Xap
is
Zip
• binDebug
dir
has
.xap
file
– Pronounced
“zap”
• This
is
the
file
to
be
deployed
to
phone
or
emulator
15. Basic
Structure
• App
class
starts
the
app
• PhoneApplicationFrame
instance
can
host
one
or
more
pages
• PhoneApplicationPage
instances
are
the
pages
16. MainPage
namespace PhoneApp8
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
…
}
17. Navigate
• Naviga1on
is
based
on
XAML-‐files
(like
.html
files
in
Web)
• Use
Naviga1on
Service
to
navigate
– Naviga1onService.Navigate(new
Uri("/
SecondPage.xaml",
UriKind.Rela1ve));
– Naviga1onService.GoBack();
18. public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void navigateButton_Click(object sender,
RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/SecondPage.xaml",
UriKind.Relative));
}
}
19. About
Naviga1on
• Last-‐in-‐first-‐out
=>
Stack
• Phones
back
bueon
is
the
same
than
– Naviga1onService.GoBack();
• To
create
new
instance
of
the
page:
– Naviga1onService.Navigate(new
Uri("/
MainPage.xaml",
UriKind.Rela1ve));
21. Possibili1es
• Every
1me
you
navigate
to
new
page,
a
new
instance
is
created
=>
data
loss
• Solu1ons
– 1)
Just
pass
data
across
pages
– 2)
PhoneApplica1onService:
data
in
memory
while
app
running
– 3)
IsolatedStorage:
persistant
storage
22. Page
1
to
Page
2
• Pass
values
like
in
Web!
–
NavigationService.Navigate(new Uri("/
SecondPage.xaml?name=Jussi",
UriKind.Relative));
23. To
receive
the
parameters
protected override void
OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
IDictionary<string, string> parameters =
NavigationContext.QueryString;
if (parameters.ContainsKey("name"))
{
PageTitle.Text = parameters["name"];
}
base.OnNavigatedTo(e);
}
24. Give
values
back
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (e.Content is MainPage)
{
MainPage reference = (e.Content as MainPage);
reference.PageTitle.Text = "And we are back!";
}
base.OnNavigatedFrom(e);
}
25. Basics
• When
app
is
opened
from
Start
screen
– App
is
launched
• When
app
is
terminated
as
result
of
Back
– App
is
closed
• When
program
is
running
and
user
presses
start
– App
is
deac1vated
-‐>
tombstoned
state
• When
program
is
navigated
back
– App
is
ac1vated
-‐>
back
from
tombstoned
state
27. PhoneApplica1onService:
Store
Data
while
App
in
Memory
• You
can
use
PhoneApplica1onService
to
store
data
in
RAM
while
app
is
in
use
or
tombstoned
• Really
easy
to
implement
store
key
value
pairs
in
PhoneApplica1onService.Current.State
28. Example
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
PhoneApplicationService.Current.State["key"] = "arvo";
base.OnNavigatedFrom(e);
}
protected override void
OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (PhoneApplicationService.Current.State.ContainsKey("key"))
{
string value = (string) PhoneApplicationService.Current.State["key"];
}
base.OnNavigatedTo(e);
}
29. When
values
are
lost?
• Open
app,
press
back
-‐>
app
is
closed
-‐>
data
lost
• Open
app,
press
start
-‐>
press
back
-‐>
data
is
there
• Open
app,
press
start
-‐>
open
app
again
from
menu
-‐>
data
lost
• Open
app,
long
press
back
-‐>
switch
apps
-‐>
long
press
back
to
switch
back
-‐>
data
is
there
30. Persistant
Storage:
Isolated
Storage
• Load
seings
when
app
launching
or
ac1va1ng
• Save
seings
when
app
deac1va1ng
or
closing
• Why?
– program
should
also
save
applica1on
seings
during
the
Deac%vated
event
because
the
program
really
doesn’t
know
if
it
will
ever
be
resurrected.
And
if
it
is
resurrected,
it
should
load
applica1on
seings
during
the
Ac%vated
event
because
otherwise
it
won’t
know
about
those
seings.
31. Common
value
for
All
public partial class App : Application
{
private String commonValue = "Default";
public String CommonValue {
set {
commonValue = value;
}
get {
return commonValue;
}
}
32. Usage
of
Common
Value
private void button1_Click(object sender, RoutedEventArgs e)
{
App appRef = Application.Current as App;
PageTitle.Text = appRef.CommonValue;
}