0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7...
Transcript of 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7...
![Page 1: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/1.jpg)
0
Windows Phone 8.1 J. ROMAGNY
1. INSTALLATION ......................................................................................................................................... 3
2. EMULATEUR ET RESOLUTIONS ................................................................................................................. 6
3. LAYOUTS .................................................................................................................................................. 7
A. HUB ............................................................................................................................................................. 7 B. PIVOT ......................................................................................................................................................... 10 C. PAGE DE BASE (BASICPAGE) ............................................................................................................................ 11 D. GRID .......................................................................................................................................................... 12 E. STACKPANEL ................................................................................................................................................ 12 F. BORDER ...................................................................................................................................................... 13 G. SCROLLVIEWER ............................................................................................................................................. 13 H. CANVAS ...................................................................................................................................................... 14
4. CONTROLES............................................................................................................................................ 15
A. TEXTE ......................................................................................................................................................... 15 B. BOUTONS ET SELECTION ................................................................................................................................. 16 C. LISTE .......................................................................................................................................................... 17 D. BARRE DE PROGRESSION ................................................................................................................................. 19 E. DATES ........................................................................................................................................................ 20 F. MEDIA ET WEB ............................................................................................................................................ 20
5. BARRE DE COMMANDES ........................................................................................................................ 21
6. REPONSE A L’INTERACTION UTILISATEUR .............................................................................................. 22
7. STYLES.................................................................................................................................................... 23
8. RESSOURCES .......................................................................................................................................... 25
9. THEMES ET ACCENTS .............................................................................................................................. 25
10. TEMPLATES ........................................................................................................................................ 27
11. BLEND ................................................................................................................................................ 28
12. MVVM ............................................................................................................................................... 29
A. OBSERVABLEBASE (INOTIFYPROPERTYCHANGED)................................................................................................ 29 B. COMMANDES ............................................................................................................................................... 29 C. UNIVERSAL APP ............................................................................................................................................ 31
13. NAVIGATION...................................................................................................................................... 32
MISE EN CACHE DE LA PAGE ..................................................................................................................................... 33
14. TRANSITIONS ..................................................................................................................................... 34
ANIMATIONS ........................................................................................................................................................ 35
15. CYCLE DE VIE ...................................................................................................................................... 37
SUSPENSION MANAGER ET NAVIGATIONHELPER (BASICPAGE) ....................................................................................... 38
16. LOCALISATION ................................................................................................................................... 40
17. DATA STORAGE .................................................................................................................................. 43
![Page 2: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/2.jpg)
1
1
A. SAUVEGARDE DE FICHIERS (« LOCAL », « ROAMING », « TEMPORARY ») ............................................................... 43 B. DOSSIER PROJET............................................................................................................................................ 45 C. KNOWNFOLDERS .......................................................................................................................................... 46 D. PICKERS ...................................................................................................................................................... 47 E. PARAMETRES D’APPLICATION (« LOCALSETTINGS », « ROAMINGSETTINGS ») .......................................................... 49 F. DATACHANGED (« ROAMING »)...................................................................................................................... 50 G. CREDENTIAL LOCKER ...................................................................................................................................... 50
18. JSON .................................................................................................................................................. 51
A. « TO JSON » .............................................................................................................................................. 51 B. « FROM JSON » .......................................................................................................................................... 52
19. SQLITE ............................................................................................................................................... 53
20. HTTP .................................................................................................................................................. 56
A. HTTPCLIENT (SYSTEM.WEB.HTTP) .................................................................................................................. 56 B. WEB AUTHENTICATION BROKER ...................................................................................................................... 57 C. BACKGROUNDTRANSFER (TRANSFERT EN ARRIERE-PLAN) ...................................................................................... 58
Download ..................................................................................................................................................... 58 Upload .......................................................................................................................................................... 60
21. VIGNETTE (« TILE ») ........................................................................................................................... 62
A. EPINGLER LA VIGNETTE DE L’APPLICATION A L’ECRAN D’ACCUEIL ............................................................................. 63 B. VIGNETTE SECONDAIRE (« SECONDARYTILE ») .................................................................................................... 64 C. NOTIFICATION DE VIGNETTE ............................................................................................................................ 66 A. DEFINIR LE TEMPLATE POUR PLUSIEURS TAILLES DE VIGNETTE ................................................................................. 67 B. « ADDTOSCHEDULE » POUR DIFFERER LA MODIFICATION ...................................................................................... 67 C. « PERIODIC UPDATE » : WEB SERVICE (WEB API, WCF, ETC.) APPELE PERIODIQUEMENT ........................................... 67 D. SUPPRIMER LES MISES A JOUR DE LA VIGNETTE PRINCIPALE .................................................................................... 68 E. SCHEMAS D’URI............................................................................................................................................ 68 F. RECUPERER LES ARGUMENTS AU LANCEMENT DE L’APPLICATION DEPUIS LA VIGNETTE SECONDAIRE ................................ 68 G. BADGES ...................................................................................................................................................... 69 H. PUSH NOTIFCATION SERVICE (WNS) ................................................................................................................ 69
22. TOASTS .............................................................................................................................................. 70
A. « TOASTTEXT01 » ........................................................................................................................................ 70 B. « TOASTTEXT02 » ........................................................................................................................................ 71 C. TOAST DIFFERE (« SCHEDULED ») .................................................................................................................... 72 D. DECLENCHER UNE ACTION AU CLIC SUR LE TOAST (« ACTIVATED ») ......................................................................... 72 E. LE CENTRE DE NOTIFICATIONS .......................................................................................................................... 73
23. MESSAGEDIALOG ............................................................................................................................... 73
24. GESTION DES CONTACTS ET RENDEZ-VOUS DE L’UTILISATEUR .......................................................... 74
A. GESTION DES CONTACTS ................................................................................................................................. 74 B. ENVOI D’EMAIL A UN CONTACT ........................................................................................................................ 75 C. ENVOI D’UN SMS A UN CONTACT ..................................................................................................................... 78 D. GESTION DE RENDEZ-VOUS ............................................................................................................................. 78
25. CONTRATS DE PARTAGE .................................................................................................................... 79
A. APPLICATION POUVANT PARTAGER DU CONTENU ................................................................................................. 79 B. CREER UNE APPLICATION « CIBLE DE PARTAGE » ................................................................................................. 81
26. TACHES EN ARRIERE-PLAN (BACKGROUNDTASK) ............................................................................... 85
![Page 3: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/3.jpg)
2
2
27. MAPS, GEOLOCATION ........................................................................................................................ 88
A. « LAUNCH URI » .......................................................................................................................................... 88 B. MAPCONTROL ............................................................................................................................................. 89
28. WINDOWS PHONE SILVERLIGHT ........................................................................................................ 90
PIVOT .................................................................................................................................................................. 90 PANORAMA .......................................................................................................................................................... 90 BARRE DE COMMANDES .......................................................................................................................................... 90 WINDOWS PHONE TOOLKIT ..................................................................................................................................... 90 NAVIGATION ......................................................................................................................................................... 91 CYCLE DE VIE ......................................................................................................................................................... 92
} ............................................................................................................................................................................ 92 VISUAL STATES ET ORIENTATIONS .............................................................................................................................. 93 REMINDER, ALARM ET BACKGROUND AGENT ............................................................................................................... 96
Reminder ...................................................................................................................................................... 96 Alarm ............................................................................................................................................................ 96 Background agent ........................................................................................................................................ 97
![Page 4: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/4.jpg)
3
3
1. Installation Requis :
Windows 8.1 Professional Edition afin de pouvoir activer Hyper-V
« Panneau de configuration » … « Programmes » … « Programmes et fonctionnalités » … « Activer
ou désactiver des fonctionnalités Windows »
(On peut utiliser Corefinfo pour savoir si le pc supporte Hyper-V … depuis une invite de
commande administrateur, entrer la commande coreinfo –v)
Avec une machine virtuelle (VMware Player ou Workstation)
Ouvrir d’abord le fichier *.vmx avec le bloc-notes, ajouter
hypervisor.cpuid.v0 = "FALSE" mce.enable = "TRUE"
…Puis éditer les paramètres de la machine virtuelle : 2 processeurs et « Virtualize… »
…Et enfin activer « Hyper-V » depuis le panneau de configuration.
Conseils : pour plus de confort installer les VMware Tools, activer le partage de dossier
![Page 5: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/5.jpg)
4
4
Partage de dossier :
On définit un dossier
partagé dans les options
On retrouve le chemin depuis
la machine virtuelle
![Page 6: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/6.jpg)
5
5
Visual Studio 2013 Community (gratuite), Pro ou Ultimate Edition et installer le SDK
Windows Phone
Archives du Kit de développement logiciel (SDK) Windows Phone
![Page 7: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/7.jpg)
6
6
2. Emulateur et résolutions Permet de tester sur différentes tailles d’écran
Le panneau « Périphérique » permet également d’avoir un aperçu dans Visual Studio
![Page 8: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/8.jpg)
7
7
3. Layouts
a. Hub Démarrage rapide, exemple
Utilisé pour la page principale.
La page est divisée en sections. On peut naviguer entre chaque section en faisant glisser la page avec le doigt (« swipe »).
Avec une application Windows Store cela serait
Hero
Header cliquable
![Page 9: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/9.jpg)
8
8
« Code de base »
<Grid> <Hub Header="Mon application"> <HubSection Header="Section 1" Width="500" > </HubSection> <HubSection Header="Section 2" Width="500"> </HubSection> <!-- autres HubSections--> </Hub> </Grid>
« Hub vertical »
<Hub Header="Field Guide" Orientation="Vertical"> <!-- sections --> </Hub>
Créer une section « hero »
<Grid> <Hub Header="Mon application"> <HubSection Width="480" Margin="0,0,80,0"> <HubSection.Background> <ImageBrush ImageSource="Images/stones.jpg" Stretch="UniformToFill"/> </HubSection.Background> </HubSection> <HubSection Width="500" Header="Section 1" IsHeaderInteractive="True"> </HubSection> <!-- autres HubSections--> </Hub> </Grid>
Définir une image d’arrière-plan du « Hub »
<Grid> <Hub Header="Mon application"> <Hub.Background> <ImageBrush ImageSource="Images/stones.jpg" Stretch="UniformToFill" /> </Hub.Background> <HubSection Header="Section 1" Width="500" > </HubSection> </Hub> </Grid>
![Page 10: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/10.jpg)
9
9
Rendre intéractif l’en-tête d’une section
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Hub Header="Mon application" SectionHeaderClick="Hub_SectionHeaderClick"> <HubSection x:Name="section1" Header="Section 1" Width="500" IsHeaderInteractive="True"> </HubSection> <HubSection Header="Section 2" Width="500"> </HubSection> </Hub> </Grid>
Code-behind de la page
private void Hub_SectionHeaderClick(object sender, HubSectionHeaderClickEventArgs e) { var sectionName = e.Section.Name; switch (sectionName) { case "section1": Frame.Navigate(typeof(PageTwo)); break; default: break; } }
Templates et styles de sections (HeaderTemplate, ItemTemplate)
Exemple une section avec pour contenu une « ListView »
<HubSection Width="500" Header="Section 1"> <DataTemplate> <ListView SelectionMode="None" ItemsSource="{Binding People}" /> </DataTemplate> </HubSection>
Ou
<HubSection Width="500" Header="Section 1" HeaderTemplate="{StaticResource HubSectionHeaderTemplate}" Style="{StaticResource HubSectionStyle}" ContentTemplate="{StaticResource PeopleListTemplate}" />
On peut également définir un DataContext différent par section.
<HubSection Width="500" Header="Section 1" ContentTemplate="{StaticResource Panel_Menu_GridPhoto}" DataContext="{Binding People}" d:DataContext="{d:DesignData Source=/Assets/Design/Data.json, Type=vm:DesignViewModel, IsDesignTimeCreatable=true}"/>
C’est sur le contrôle
« Hub » que l’on ajoute
l’événement
« SectionHeaderClick »
On définir « IsHeaderInteractive » pour les
sections dont l’en-tête est cliquable.
Donner un nom à la section
![Page 11: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/11.jpg)
10
10
b. Pivot Le pivot est un « TabControl ». On bascule d’un onglet à l’autre en cliquant sur l’en-tête.
Recommandations
<Pivot Title="Mon application"> <Pivot.Background> <ImageBrush ImageSource="Assets/foodies.jpg" Stretch="UniformToFill" /> </Pivot.Background> <PivotItem Header="Item 1"> </PivotItem> <PivotItem Header="Item 2"> </PivotItem> <PivotItem Header="Item 3"> </PivotItem> </Pivot>
On peut définir une image en arrière-plan par exemple
Insérer le contenu de chaque « onglet »
(ListView par exemple)
Chaque « PivotItem » peut avoir un DataContext
différent
![Page 12: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/12.jpg)
11
11
c. Page de base (BasicPage) Sont ajoutés en même temps que la première « BasicPage » :
<Grid x:Name="LayoutRoot"> <Grid.ChildrenTransitions> <TransitionCollection> <EntranceThemeTransition/> </TransitionCollection> </Grid.ChildrenTransitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Margin="19,0,0,0"> <TextBlock Text="MY APPLICATION" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0"/> <TextBlock Text="page title" Margin="0,-6.5,0,26.5" Style="{ThemeResource HeaderTextBlockStyle}" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"/> </StackPanel> <Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0"> </Grid> </Grid>
![Page 13: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/13.jpg)
12
12
d. Grid Grille divisée en colonnes et lignes. On place les éléments dans la grille.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="140"/> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Text="Mon application" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextBlockStyle}"/> <Rectangle Fill="Orange" Grid.Row="1" Grid.Column="1" /> </Grid>
e. StackPanel Empilement des éléments de façon verticale (par défaut) ou horizontale.
<StackPanel Orientation="Horizontal"> <Button Content="Bouton 1" /> <Button Content="Bouton 2" /> </StackPanel>
Ecriture avec « * ». Ici la seconde colonne aura une taille
équivalente à 2 fois celle de la première
On peut réunir les colonnes ou lignes
Définition de l’emplacement
dans la grille
Hauteur définie en pixels par exemple
« ThemeResource »
![Page 14: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/14.jpg)
13
13
f. Border Bordure, permet d’entourer un élément (contrôle, layout, etc.). On peut définir son épaisseur,
couleurs, angle, etc.
<Border BorderBrush="SkyBlue" Background="Gray" BorderThickness="5" CornerRadius="10"> <ListView> <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> </ListView> </Border>
g. ScrollViewer Permet d’ajouter des barres de défilement pour un contenu trop grand pour être affiché
entièrement à l’écran.
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"> <Image Source="Assets/foodies.jpg" /> </ScrollViewer>
ScrollBar
![Page 15: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/15.jpg)
14
14
h. Canvas Positionnement des éléments par rapport aux bords gauche et haut de l’écran.
<Canvas> <Image Source="Assets/foodies.jpg" Width="200" Canvas.Left="50" Canvas.Top="150"/> </Canvas>
![Page 16: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/16.jpg)
15
15
4. Contrôles Windows Store App UI Start to finish Sample
a. Texte TextBlock
<TextBlock Text="Mon texte" Style="{StaticResource BodyTextBlockStyle}" />
Styles :
HeaderTextBlockStyle
SubheaderTextBlockStyle
TitleTextBlockStyle
BaseTextBlockStyle
BodyTextBlockStyle
Recommandations en matière de polices
TextBox : permet la saisie des informations
<TextBox TextWrapping="Wrap" Text="Mon texte" />
RichTextBlock
<RichTextBlock TextAlignment="Justify" FontSize="12" FontFamily="Segoe UI" Foreground="#2a2a2a"> <Paragraph> <Bold> <Span Foreground="DarkSlateBlue" FontSize="16">sit amet consectetur</Span></Bold> </Paragraph> <Paragraph> <InlineUIContainer> <Border Background="Gray"> <Image Source="Assets/foodies.jpg" Height="100" Width="100"/> </Border> </InlineUIContainer> <LineBreak /> <Italic>Sed metus dui rutrum eget lacus</Italic> <LineBreak /> </Paragraph> <Paragraph> Lorem ipsum dolor sit amet … </Paragraph> </RichTextBlock>
Styles :
BaseRichTextBlockStyle
BodyRichTextBlockStyle
PasswordBox
![Page 17: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/17.jpg)
16
16
b. Boutons et sélection Button
<Button Content="Ok" /> ToggleButton (bouton « checkable »)
<ToggleButton IsChecked="True" Content="ToggleButton" />
ToggleSwitch
<ToggleSwitch Header="ToggleSwitch" OnContent="On" OffContent="Off" />
HyperlinkButton
<HyperlinkButton Content="Mon lien" NavigateUri="http://www.msn.com/fr-fr/"> CheckBox
<CheckBox Content="Option 1" IsChecked="True" /> RadioButton
<StackPanel Orientation="Horizontal" Margin="0,30,0,0"> <RadioButton Content="Choix 1" IsChecked="True" GroupName="group1" /> <RadioButton Content="Choix 2" GroupName="group1" /> </StackPanel>
Slider
<Slider Margin="0,30,0,0" Maximum="100" Value="20" />
![Page 18: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/18.jpg)
17
17
c. Liste - ListView éléments empilés (« verticalement » par défaut) avec ItemsPanel :
VirtualizingStackPanel
- GridView affichage en grille avec ItemsPanel : WrapGrid
Documentation
Modèles d’éléments de Grille
Modèles d’éléments de Liste
Si aucun « ItemTemplate » n’est défini, la méthode « ToString » de chaque objet est appelée pour
afficher le contenu de chaque élément.
GridView
ListView
<ListView ItemsSource="{Binding People}"> <ListView.ItemTemplate> <DataTemplate> <Border BorderBrush="{ThemeResource PhoneAccentBrush}" BorderThickness="6,0,0,0" Margin="0,0,0,10" Padding="10,0,0,0"> <TextBlock Text="{Binding Name}" Style="{StaticResource BodyTextBlockStyle}"/> </Border> </DataTemplate> </ListView.ItemTemplate> </ListView>
Autre exemple
<ListView SelectionMode="None" ItemsSource="{Binding People}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Image}" Width="80" Height="60"/> <StackPanel Orientation="Vertical" Background="#FF0AB7FD" Width="250"> <TextBlock Text="{Binding Name}" FontSize="14.667" FontFamily="Segoe UI" Margin="5,0,0,0"/> <TextBlock Text="{Binding Email}" FontSize="12" FontFamily="Segoe UI" Margin="5,0,0,0"/> </StackPanel> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>
![Page 19: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/19.jpg)
18
18
ItemsPanel : exmple un GridView à 3 colonnes maximum
<GridView Height="500" Width="400"> <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3"/> </ItemsPanelTemplate> </GridView.ItemsPanel> <Rectangle Height="100" Width="100" Fill="Blue" /> <Rectangle Height="100" Width="100" Fill="Red" /> <Rectangle Height="100" Width="100" Fill="Yellow" /> <Rectangle Height="100" Width="100" Fill="Green" /> <Rectangle Height="100" Width="100" Fill="Gray" /> </GridView>
Autre exemple ListView avec éléments disposés horizontalement
<ListView> <ListView.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> </ListView>
(ListBox)
Bien que la largeur du GridView
permettrait 4 colonnes, on en définit
3 au maximum
![Page 20: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/20.jpg)
19
19
Editer facilement les templates des contrôles (HeaderTemplate, FooterTemplate, ItemTemplate,
ItemContainerStyle, ItemsPanel)
d. Barre de progression ProgressBar
<ProgressBar Value="20" Maximum="100"/>
<ProgressBar IsIndeterminate="True" />
ProgressRing
<ProgressRing IsActive="True" Visibility="Visible"/>
![Page 21: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/21.jpg)
20
20
e. Dates DatePicker
<DatePicker Header="Naissance" />
f. Média et Web Image
<Image Height="100" Source="Images/jetty.jpg" />
MedialElement
WebView (ouvre une page web dans le conteneur)
<WebView Height="300" Source="http://www.msn.com/fr-fr/"></WebView>
FlipView
<FlipView> <Image Source="Images/image_1.jpg" /> <Image Source="Images/image_2.jpg" /> <Image Source="Images/image_3.jpg" /> </FlipView>
Exemple
![Page 22: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/22.jpg)
21
21
5. Barre de commandes <Page.BottomAppBar> <CommandBar> <CommandBar.PrimaryCommands> <AppBarButton Icon="Save" Label="Enregistrer"/> <AppBarButton> <AppBarButton.Icon> <FontIcon FontFamily="Segoe UI Symbol" Glyph="✎" /> </AppBarButton.Icon> </AppBarButton> </CommandBar.PrimaryCommands> <CommandBar.SecondaryCommands> <AppBarButton Label="Bouton secondaire" /> </CommandBar.SecondaryCommands> </CommandBar> </Page.BottomAppBar>
Utilisation de la Font Segoe UI Symbol et de la table de caractères. Segoe UI Symbols
Trouver des icônes : « C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.1\Icons » (Les ajouter au projet et les définir en tant que « contenu » dans le panneau
propriétés)
Bouton avec icône
et label
Bouton avec icône
définie avec la font
« Segoe UI Symbol »
Bouton dans la partie des commandes « secondaires » …
n’apparaissent qu’au clic sur le
bouton « Hamburger »
Bouton « Hamburger »
![Page 23: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/23.jpg)
22
22
Le panneau « Structure du document » (Document Ouline) permet d’ajouter facilement les
barres de commandes et boutons depuis le menu contextuel …
6. Réponse à l’intéraction Utilisateur Documentation
Tap Appuyer Brève pression de l’écran avec le doigt Press and hold Appuyer longuement Pression maintenue avec le doigt Slide Faire glisser Pression de l’écran et déplacement dans une direction Swipe Balayer Turn Tourner Pression à deux doigts et mouvement en arc de cercle de haut
en bas ou de bas en haut Pinch Pincer Pression de l’écran à deux doigts puis rapprochement des doigts Stretch Etirer Pression à deux doigts puis étriement
![Page 24: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/24.jpg)
23
23
7. Styles Appliquer un Style prédéfini à un contrôle
Depuis le panneau « Structure du document » (Document Outline)… clic droit sur le
contrôle… « Modifier le Style » … « Appliquer la ressource »
Exemple avec un « TextBlock » auquel on applique le Style d’en-tête.
Il est possible également de définir son propre … Menu contextuel « Créer un élément vide… »
Le Style peut être ajouté :
- Application : dans « App.xaml »
- En ressource de la page ou du
contrôle
- Dictionnaire de ressources : dans un dictionnaire créé (doit être
référencé auparavant dans
« App.xaml »)
![Page 25: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/25.jpg)
24
24
Il est possible ensuite d’éditer le Style depuis le panneau « Propriétés »
Pour un Style défini dans un dictionnaire de ressources on a en plus un aperçu
Le Style sera disponible parmi les styles « prédéfinis »
Référencer un dictionnaire dans « App.xaml » pour le rendre disponible à l’application.
<Application x:Class="WindowsDemo.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WindowsDemo"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Styles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
![Page 26: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/26.jpg)
25
25
8. Ressources Exemple on définit une couleur en ressource
<SolidColorBrush Color="Gray" x:Key="GrayBrush" />
Celle-ci est disponible depuis le panneau « Propriétés » … « Ressources Locales »
9. Thèmes et Accents « C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.1\Design »
RequestedTheme : Permet de définir le thème de la page ou des contrôles
<Button RequestedTheme="Light" Content="Button Light"/>
Le panneau « Périphérique » permet
d’avoir un aperçu de l’apparence des
pages avec un thème et accent. Mais c’est
depuis les settings du téléphone (ou
émulateur) que l’on peut changer
réellement le thème et accent.
![Page 27: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/27.jpg)
26
26
Accents
<Button Content="Ok" Background="{StaticResource PhoneAccentBrush}" /> Ou
<Button Content="Ok" Background="{ThemeResource PhoneAccentBrush}" />
En changeant l’Accent depuis le panneau « Périphérique »..
Documentation
Changer le thème et l’accent depuis le mobile/ émulateur
Lime
Amber
![Page 28: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/28.jpg)
27
27
10. Templates Depuis le panneau « Structure du document » (Document Outline)… clic droit sur le
contrôle… « Modifier le Modèle » … « Créer un élément vide »
Pour un Template défini dans un dictionnaire de ressources on a un aperçu.
Appliquer un modèle à un contrôle… Menu « Modifier le modèle » … « Appliquer la ressource » …
Un controlTemplate aura la forme
<ControlTemplate x:Key="MyButtonControlTemplate" TargetType="Button"> <!-- définir le template --> </ControlTemplate>
En code pour l’utiliser le Template
<Button Content="Ok" Template="{StaticResource MyButtonControlTemplate}"/>
On peut également définir le Template depuis un Style. Exemple de bouton.
<Style x:Key="MyButtonStyle" TargetType="Button"> <Setter Property="FontFamily" Value="Cambria" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{ThemeResource PhoneAccentBrush}" CornerRadius="10"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
Pour l’utiliser on définit le Style et non pas le Template
<Button Content="Ok" Style="{StaticResource MyButtonStyle}" />
Le Template peut être ajouté :
- Application : dans « App.xaml »
- En ressource de la page ou du
contrôle
- Dictionnaire de ressources : dans un dictionnaire créé (doit être
référencé auparavant dans
« App.xaml »)
![Page 29: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/29.jpg)
28
28
11. Blend Ne pas hésiter à utiliser Blend pour ajouter un comportement (Behavior) ou des états visuels
(Visual State).
![Page 30: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/30.jpg)
29
29
12. Mvvm
a. ObservableBase (INotifyPropertyChanged) Pour notifier l’interface utilisateur d’un changement de propriété
public abstract class ObservableBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (object.Equals(storage, value)) return false; storage = value; RaisePropertyChanged(propertyName); return true; } protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } }
b. Commandes public class RelayCommandBase : ICommand { protected readonly Delegate _executeMethod; protected readonly Func<Object, bool> _canExecuteMethod; public RelayCommandBase(Delegate executeMethod) : this(executeMethod, (args) => true) { } public RelayCommandBase(Delegate executeMethod, Func<Object, bool> canExecuteMethod) { if (executeMethod == null) throw new ArgumentNullException("execute"); _executeMethod = executeMethod; _canExecuteMethod = canExecuteMethod; } public bool CanExecute(object parameter) { return _canExecuteMethod == null || _canExecuteMethod(parameter); } public virtual void Execute(object parameter) { _executeMethod.DynamicInvoke(parameter); } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { var handler = CanExecuteChanged; if (handler != null) { handler(this, EventArgs.Empty); } } }
Il faut déclencher
l’évaluation de la condition d’exécution en appelant
« RaiseCanExecuteChanged »
![Page 31: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/31.jpg)
30
30
RelayCommand
public class RelayCommand : RelayCommandBase { public RelayCommand(Action executeMethod) : this(executeMethod, () => true) { } public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod) : base(executeMethod, (args) => canExecuteMethod()) { } public bool CanExecute() { return CanExecute(null); } public void Execute() { Execute(string.Empty); } public override void Execute(object parameter) { _executeMethod.DynamicInvoke(); } }
RelayCommand « Generic » (commande avec passage de paramètre)
public class RelayCommand<T> : RelayCommandBase { public RelayCommand(Action<T> execute) : this(execute, (args) => true) { } public RelayCommand(Action<T> executeMethod, Func<Object, bool> canExecuteMethod) : base(executeMethod, (args) => canExecuteMethod((T)args)) { } public virtual bool CanExecute(T parameter) { return base.CanExecute(parameter); } }
![Page 32: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/32.jpg)
31
31
c. Universal App Transformer une application Windows ou Windows Phone en Universal App
#if SILVERLIGHT #endif
#if WINDOWS_PHONE_APP var x = 10; #elif WINDOWS_APP var x = 100; #endif
![Page 33: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/33.jpg)
32
32
13. Navigation La fenêtre principale de l’application a une « Frame » comme conteneur dans laquelle les
pages sont affichées.
On peut récupérer la Frame principale
Frame rootFrame = Window.Current.Content as Frame;
Navigation vers une page
Frame.Navigate(typeof(PageTwo));
Navigation avec passage de paramètre
rame.Navigate(typeof(PageTwo),"mon paramètre");
Récupérer le paramètre depuis la page appelée
public sealed partial class PageTwo : Page { // etc. protected override void OnNavigatedTo(NavigationEventArgs e) { if (e.Parameter != null) { string myParameter = e.Parameter.ToString(); } } }
Navigation retour
if (Frame.CanGoBack) { Frame.GoBack(); }
« BackPressed »
public sealed partial class App : Application { public App() { this.InitializeComponent(); this.Suspending += this.OnSuspending; HardwareButtons.BackPressed += HardwareButtons_BackPressed;
Window
Frame
Page
![Page 34: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/34.jpg)
33
33
} void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame != null && rootFrame.CanGoBack) { rootFrame.GoBack(); e.Handled = true; } } // etc. }
Mise en cache de la page Nombre de pages pouvant être mis en cache (« App »)
« Cache » pour chaque page
Ou en code
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; } // }
Eviter qu’une page cachée soit rechargée
protected override void OnNavigatedTo(NavigationEventArgs e) { if (e.NavigationMode == NavigationMode.Back) return; this.navigationHelper.OnNavigatedTo(e); }
Par défaut le bouton de retour permet la navigation
entre « applications ». Désormais elle permettra le
retour entre les pages de l’application
![Page 35: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/35.jpg)
34
34
14. Transitions Transitions entre pages
CommonNavigationTransitionInfo
Frame.Navigate(typeof(PageTwo), null, new CommonNavigationTransitionInfo());
SlideNavigationTransitionInfo (slide par le bas)
Frame.Navigate(typeof(PageTwo),null, new SlideNavigationTransitionInfo());
ContinuumNavigationTransitionInfo
Exemple on a deux pages. Sur la page de départ un TextBlock
<TextBlock ContinuumNavigationTransitionInfo.IsExitElement="True" /> Sur la page cible, un autre TextBlock
<TextBlock ContinuumNavigationTransitionInfo.IsEntranceElement="True" />
On déclenche la navigation vers la seconde page. Il y aura une animation de départ et une
animation d’arrivée (« bond »)
Frame.Navigate(typeof(PageTwo), null, new ContinuumNavigationTransitionInfo());
Autre exemple : animations pour « Pivot »
<Page.Transitions> <TransitionCollection> <NavigationThemeTransition> <NavigationThemeTransition.DefaultNavigationTransitionInfo> <CommonNavigationTransitionInfo IsStaggeringEnabled="True"/> </NavigationThemeTransition.DefaultNavigationTransitionInfo> </NavigationThemeTransition> </TransitionCollection> </Page.Transitions>
Sur chaque « PivotItem »
<Pivot> <PivotItem … CommonNavigationTransitionInfo.IsStaggerElement="True"> <ListView … ContinuumNavigationTransitionInfo.ExitElementContainer="True"> <ListView.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock … CommonNavigationTransitionInfo.IsStaggerElement="True" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </PivotItem> <!--etc.--> </Pivot>
Définit si l’élément
doit être animé
Autre animation
En Ressource de la page
![Page 36: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/36.jpg)
35
35
Animations Recommandations, Documentation, vidéos de démo
Animations de transition
AddDeleteThemeTransition
ContentThemeTransition
EdgeUIThemeTransition
EntranceThemeTransition
PaneThemeTransition
PopupThemeTransition
ReorderThemeTransition
RepositionThemeTransition
Les contrôles, layouts peuvent utiliser ces animations
Exemple : Apparition en « escalier » des éléments d’une « ListView » et animation à
l’ajout/suppression d’éléments.
<ListView> <ListView.ItemContainerTransitions> <TransitionCollection> <EntranceThemeTransition /> <AddDeleteThemeTransition /> </TransitionCollection> </ListView.ItemContainerTransitions> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> <ListViewItem>Item</ListViewItem> </ListView>
Animations de thème (requièrent un storyboard)
DragItemThemeAnimation
DragOverThemeAnimation
DropTargetItemThemeAnimation
FadeInThemeAnimation
FadeOutThemeAnimation
PointerDownThemeAnimation
PointerUpThemeAnimation
PopInThemeAnimation
PopOutThemeAnimation
RepositionThemeAnimation
SplitCloseThemeAnimation
SplitOpenThemeAnimation
SwipeBackThemeAnimation
SwipeHintThemeAnimation
![Page 37: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/37.jpg)
36
36
Exemple : on a un rectangle rouge que l’on fait disparaitre avec « FadeOutThemeAnimation » au
clic sur un bouton
<StackPanel> <StackPanel.Resources> <Storyboard x:Name="storyboard"> <FadeOutThemeAnimation TargetName="rectangle" /> </Storyboard> </StackPanel.Resources> <Button Content="Animer" Click="Button_Click" /> <Rectangle x:Name="rectangle" Fill="Red" Width="200" Height="300"/> </StackPanel>
private void Button_Click(object sender, RoutedEventArgs e) { storyboard.Begin(); }
![Page 38: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/38.jpg)
37
37
15. Cycle de vie
Launch
Suspend
Resume
Termination
Pendant l’exécution de l’application on peut simuler un changement d’état depuis Visual Studio
Pour afficher la barre d’outil : Menu « Affichage »… « Barre d’outils » … « Emplacement de
débogage »
public sealed partial class App : Application { public App() { this.InitializeComponent(); this.Suspending += this.OnSuspending; this.Resuming += OnResuming; } protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.CacheSize = 1; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { } Window.Current.Content = rootFrame; } // etc. } private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); deferral.Complete(); } private void OnResuming(object sender, object e) { } }
« Termination »
Gestion si l’application était « terminée »
On restaure les données
Gestion lorsque l’application passe à l’état
« suspendu »
On sauvegarde les données localement
Gestion lorsque l’application est « résumée »
![Page 39: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/39.jpg)
38
38
Suspension Manager et NavigationHelper (BasicPage) L’objectif est de retrouver la page sur laquelle l’utilisateur était ainsi que les états de cette page
lorsque l’application est relancée.
public sealed partial class App : Application { public App() { this.InitializeComponent(); this.Suspending += this.OnSuspending; } protected async override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); SuspensionManager.RegisterFrame(rootFrame, "appFrame"); if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { await SuspensionManager.RestoreAsync(); } Window.Current.Content = rootFrame; } // etc. Window.Current.Activate(); } private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); await SuspensionManager.SaveAsync(); deferral.Complete(); } }
Permet de sauvegarder
l’historique de navigation
![Page 40: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/40.jpg)
39
39
Utilisation de « NavigationHelper » depuis une page
Exemple la page a une case à cocher, on sauvegarde son état et le restaure
public sealed partial class MainPage : Page { private NavigationHelper navigationHelper; public MainPage() { this.InitializeComponent(); this.navigationHelper = new NavigationHelper(this); this.navigationHelper.LoadState += this.NavigationHelper_LoadState; this.navigationHelper.SaveState += this.NavigationHelper_SaveState; } private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) { var parameter = e.NavigationParameter.ToString(); if (e.PageState != null && e.PageState.ContainsKey("checkBox1CheckedState")) { checkBox1.IsChecked = (bool)e.PageState["checkBox1CheckedState"]; } } private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e) { e.PageState.Add("checkBox1CheckedState", checkBox1.IsChecked); } protected override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); } protected override void OnNavigatedFrom(NavigationEventArgs e) { this.navigationHelper.OnNavigatedFrom(e); } }
Sauvegarde des
états des éléments
de la page
Restauration des
états des éléments
de la page
Récupération de paramètre passé
![Page 41: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/41.jpg)
40
40
16. Localisation Globalisation de votre application, Microsoft Langage Portal
Depuis les paramètres (settings) … « langue » (langage) … on peut ajouter une langue et définir
la langue utilisée.
Définir la langue par défaut.. depuis le manifeste
« Monter » le langage
à utiliser
![Page 42: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/42.jpg)
41
41
Fichier de ressources « fr-FR »
Fichier de ressources « en-US »
Utilisation
<TextBlock x:Uid="FirstNameTextBlock" Text="Prénom" />
En changeant la langue depuis les paramètres du téléphone, le texte du TextBlock s’adapte
Un fichier de ressources par langue
rangé dans un dossier « Strings »
On définit la propriété texte pour un
TextBlock par exemple
On définit un « Uid »
![Page 43: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/43.jpg)
42
42
On peut utiliser aussi Multilingual App Toolkit (à installer depuis la boite « Extensions et mises à
jour ») qui permet une traduction automatique
Accéder à une ressource en code
var loader = new ResourceLoader(); var myString = loader.GetString("MyString");
![Page 44: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/44.jpg)
43
43
17. Data Storage Accès aux données et fichiers, exemple
Gestion des données de l’application (paramètres), exemple
Stockage « Local »
Stockage « Roaming » avec synchronisation du cloud (« Data changed » est levé)
Temporaire
KnownFolders : dossiers « spéciaux » réclamant une autorisation (capacité à cocher dans le manifeste)
Pickers permettent d’accéder aux répertoires sans « autorisation »
Répertoire du projet
Credential Locker : stockage des informations de connexion
a. Sauvegarde de Fichiers (« Local », « Roaming », « Temporary ») « LocalFolder »
var folder = ApplicationData.Current.LocalFolder;
« RoamingFolder »
var folder = ApplicationData.Current.RoamingFolder; « TemporaryFolder »
var folder = ApplicationData.Current.TemporaryFolder; Seul le répertoire de base change, le code pour sauvegarder des fichiers ne change pas.
Texte en « Local »
private async Task WriteTextToLocalStorageFile(string fileName, string text) { var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, text); } private async Task<string> ReadTextFromLocalStorageFile(string fileName) { var folder = ApplicationData.Current.LocalFolder; var file = await folder.GetFileAsync(fileName); var result = await FileIO.ReadTextAsync(file); return result; }
Utilisation
await WriteTextToLocalStorageFile("myfile.txt", "Mon texte !!"); var result = await ReadTextFromLocalStorageFile("myfile.txt");
![Page 45: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/45.jpg)
44
44
Sérialisation JSON « Local »
public static async Task SerializeJsonToLocalStorageFileAsync<T>(string fileName, T obj) where T : new() { var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists); using (var stream = await file.OpenStreamForWriteAsync()) { var serializer = new DataContractJsonSerializer(typeof(T)); serializer.WriteObject(stream, obj); } } public static async Task<T> DeserializeJsonFromLocalStorageFileAsync<T>(string fileName) where T : new() { var folder = ApplicationData.Current.LocalFolder; var file = await folder.GetFileAsync(fileName); T result = default(T); using (var stream = await file.OpenStreamForReadAsync()) { var serializer = new DataContractJsonSerializer(typeof(T)); result = (T)serializer.ReadObject(stream); } return result; }
Utilisation
var people = new List<Person> { new Person(Guid.NewGuid(),"Marie","[email protected]"), new Person(Guid.NewGuid(),"Jérôme","[email protected]") }; await SerializeJsonToLocalStorageFileAsync("people.json", people); var result = await DeserializeJsonFromLocalStorageFileAsync<List<Person>>("people.json");
Avec JSON.NET
var people = new List<Person> { new Person(Guid.NewGuid(),"Marie","[email protected]"), new Person(Guid.NewGuid(),"Jérôme","[email protected]") }; var folder = ApplicationData.Current.LocalFolder; var peopleJson = JsonConvert.SerializeObject(people); var fileJsonList = await folder.CreateFileAsync("people.json",CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(fileJsonList, peopleJson); var json = await FileIO.ReadTextAsync(fileJsonList); var result = JsonConvert.DeserializeObject<List<Person>>(json);
![Page 46: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/46.jpg)
45
45
Sérialisation XML « Local »
public static async Task SerializeXmlToLocalStorageFileAsync<T>(string fileName, T obj) where T : new() { var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists); using (var stream = await file.OpenStreamForWriteAsync()) { var serializer = new XmlSerializer(typeof(T)); serializer.Serialize(stream, obj); } } public static async Task<T> DeserializeXmlFromLocalStorageFileAsync<T>(string fileName) where T : new() { var folder = ApplicationData.Current.LocalFolder; var file = await folder.GetFileAsync(fileName); T result = default(T); using (var stream = await file.OpenStreamForReadAsync()) { var serializer = new XmlSerializer(typeof(T)); result = (T)serializer.Deserialize(stream); } return result; }
Utilisation
var people = new List<Person> { new Person(Guid.NewGuid(),"Marie","[email protected]"), new Person(Guid.NewGuid(),"Jérôme","[email protected]") }; await SerializeXmlToLocalStorageFileAsync("people.xml", people); var result = await DeserializeXmlFromLocalStorageFileAsync<List<Person>>("people.xml");
b. Dossier projet Obtenir les fichiers du projet
var installFolder = Package.Current.InstalledLocation; var projectFiles = await installFolder.GetFilesAsync(); string fileName = projectFiles[0].Name; var assetsFolder = await installFolder.GetFolderAsync("Assets"); var assets = await assetsFolder.GetFilesAsync(); foreach (var item in assets) { list.Items.Add(item.Name); }
Dossier du projet
(StorageFolder)
Fichiers et nom
Fichiers du dossier « Assets »
![Page 47: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/47.jpg)
46
46
c. KnownFolders
AppCaptures
CameraRoll
DocumentsLibrary
HomeGroup
MediaServerDevices
MusicLibrary
Objects3D
PicturesLibrary
Playlists
RemovableDevices
SavedPictures
VideosLibrary
Exemple lister les images sauvegardées
var result = new List<StorageItemThumbnail>(); var folder = KnownFolders.SavedPictures; var files = await folder.GetFilesAsync(); foreach (var file in files) { var thumbnail = await file.GetThumbnailAsync(ThumbnailMode.PicturesView); result.Add(thumbnail); }
Ajouter la capacité autorisant
l’accès à ce dossier spécial dans le
manifeste
![Page 48: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/48.jpg)
47
47
d. Pickers FolderPicker
FileOpenPicker
FileSavePicker
Documentation, exemple
Exemple ouverture et sélection d’une image
Images sauvegardées par
exemple
Application Application Picker
![Page 49: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/49.jpg)
48
48
Page
public sealed partial class PageTwo : Page { // etc. private void Button_Click(object sender, RoutedEventArgs e) { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.FileTypeFilter.Add("*"); picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.PickSingleFileAndContinue(); } public void ContinueFileOpenPick(FileOpenPickerContinuationEventArgs args) { if (args.Files.Count > 0) { var file = args.Files.First(); var dialog = new MessageDialog(file.Name); dialog.ShowAsync(); } } }
« App »
protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); Frame rootFrame = Window.Current.Content as Frame; var continunationEventArgs = args as IContinuationActivatedEventArgs; if (continunationEventArgs != null) { switch (continunationEventArgs.Kind) { case ActivationKind.PickFileContinuation: var pageTwo = rootFrame.Content as PageTwo; var result = continunationEventArgs as FileOpenPickerContinuationEventArgs; pageTwo.ContinueFileOpenPick(result); break; default: break; } } }
Au clic sur le bouton on
ouvrir un
« FileOpenPicker » pour
choisir une image
Après choix de l’image dans le picker,
celui-ci se ferme et l’application est
réactivée.. on passe le paramètre à la
page
On récupère le fichier choisi,
ici on se contente d’afficher le
nom dans une boite de
dialogue
1
3
2
On peut utiliser « * »
pour voir tout type
de fichier ou définir
des filtres par
extension
![Page 50: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/50.jpg)
49
49
e. Paramètres d’application (« LocalSettings », « RoamingSettings ») « LocalSettings »
var settings = ApplicationData.Current.LocalSettings;
« RoamingSettings »
var settings = ApplicationData.Current.RoamingSettings;
Sauvegarde d’une valeur (string, int, bool, etc.)
settings.Values["myString"] = "Ma valeur ";
Récupération
string myString = settings.Values["myString"].ToString(); Suppression
Settings.Values.Remove("myString");
Object
var person = new Person() { FirstName = "Marie", LastName = "Bellin" }; settings.Values["person"] = SerializeToString(person);
private string SerializeToString<T>(T obj) { var serializer = new XmlSerializer(typeof(T)); using (var writer = new StringWriter()) { serializer.Serialize(writer, obj); return writer.ToString(); } }
Récupération
var myObject = DeseralizeFromString<Person>(settings.Values["person"].ToString());
public T DeseralizeFromString<T>(string xml) { var serializer = new XmlSerializer(typeof(T)); var result = (T)serializer.Deserialize(new StringReader(xml)); return result; }
XML
var people = new List<Person>{ new Person{FirstName = "Marie", LastName = "Bellin" }, new Person{FirstName = "Patrick", LastName = "Prenium" } }; settings.Values["people"] = SerializeToString(people);
Récupération
var result = DeseralizeFromString<List<Person>>(settings.Values["people"].ToString());
JSON (utilisation de JSON.NET à installer avec les packages NuGet)
settings.Values["person"] = JsonConvert.SerializeObject(person);
![Page 51: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/51.jpg)
50
50
Récupération
var result = JsonConvert.DeserializeObject<Person>(settings.Values["person"].ToString());
Tableau/ liste
settings.Values["people"] = JsonConvert.SerializeObject(people); // write
Récupération
var result =JsonConvert.DeserializeObject<List<Person>>(settings.Values["people"].ToString());
f. DataChanged (« Roaming ») Cet événement est levé et informe les abonnés du chanement des données.
ApplicationData.Current.DataChanged += Current_DataChanged;
async void Current_DataChanged(ApplicationData sender, object args) { }
On peut simuler un changement
ApplicationData.Current.SignalDataChanged();
g. Credential Locker public void SaveCredential(string username, string password) { var vault = new PasswordVault(); var credential = new PasswordCredential("MyAppResource", username, password); vault.Add(credential); } public IReadOnlyList<PasswordCredential> RetrieveCredential(string resource) { var vault = new PasswordVault(); return vault.FindAllByResource(resource); }
Utilisation
SaveCredential("marie", "secret123"); var result = RetrieveCredential("MyAppResource");
![Page 52: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/52.jpg)
51
51
18. JSON Documentation, exemple
a. « To JSON »
public class Person { public string Name { get; set; } public int Age { get; set; } public bool LikeCakes { get; set; } }
Object
var person = new Person() { Name = "Marie", Age = 20, LikeCakes = true }; var jsonObject = new JsonObject(); jsonObject["Name"] = JsonValue.CreateStringValue(person.Name); jsonObject["Age"] = JsonValue.CreateNumberValue(person.Age); jsonObject["LikeCakes"] = JsonValue.CreateBooleanValue(person.LikeCakes); string jsonString = jsonObject.Stringify(); // {"Name":"Marie","Age":20,"LikeCakes":true}
Array
var people = new List<Person> { new Person() { Name = "Marie", Age = 20, LikeCakes = true }, new Person() { Name = "Patrick", Age = 30, LikeCakes = true } }; var jsonArray = new JsonArray(); foreach (var person in people) { var jsonObject = new JsonObject(); jsonObject["Name"] = JsonValue.CreateStringValue(person.Name); jsonObject["Age"] = JsonValue.CreateNumberValue(person.Age); jsonObject["LikeCakes"] = JsonValue.CreateBooleanValue(person.LikeCakes); jsonArray.Add(jsonObject); } string jsonString = jsonArray.Stringify();
![Page 53: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/53.jpg)
52
52
b. « From JSON » string json = @"{ ""Name"": ""Marie"", ""Email"": ""[email protected]"", ""Age"" : 20, ""LikeCackes"" : true }"; var jsonObject = JsonObject.Parse(json); var person = new Person(); person.Name = jsonObject.GetNamedString("Name"); person.Age = (int)jsonObject.GetNamedNumber("Age"); person.LikeCakes = jsonObject.GetNamedBoolean("LikeCackes");
Array
string json = @"[{ ""Name"": ""Marie"", ""Email"": ""[email protected]"", ""Age"" : 20, ""LikeCackes"" : true },{ ""Name"": ""Patrick"", ""Email"": ""[email protected]"", ""Age"" : 30, ""LikeCackes"" : true }]"; var people = new List<Person>(); var jsonArray = JsonArray.Parse(json); for (int i = 0; i < jsonArray.Count; i++) { var element = jsonArray[i]; if (element.ValueType == JsonValueType.Object) { var jsonObject = element.GetObject(); var person = new Person(); person.Name = jsonObject.GetNamedString("Name"); person.Age = (int)jsonObject.GetNamedNumber("Age"); person.LikeCakes = jsonObject.GetNamedBoolean("LikeCackes"); people.Add(person); } }
![Page 54: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/54.jpg)
53
53
19. SQLite Installer l’extension pour Visual Studio SQLite For Windows Phone 8.1 (vsix)
Ajouter une référence à « SQLite for Windows Phone 8.1 »
Changer la plateforme pour x86 afin de corriger le problème des références.
![Page 55: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/55.jpg)
54
54
Utilisation de « SQLite.net » (à installer depuis les packages NuGet)
Création d’un modèle
Attributs disponibles :
Table
Column
PrimaryKey
AutoIncrement: colonne de clé primaire auto incrémentée.
Indexed : colonne avec un index
Ignore : colonne ignorée lors de la création de la table
Unique : colonne avec valeurs uniques
MaxLength : taille maximale de la donnée d’une colonne
Collation
[Table("Person")] public class Person { [PrimaryKey] [AutoIncrement] [Column("Id")] public int Id { get; set; } [Column("Name")] public string Name { get; set; } [Column("Email")] public string Email { get; set; } public Person() { } public Person(int id, string name,string email) { Id = id; Name = name; Email = email; } public override string ToString() { return Name; } }
![Page 56: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/56.jpg)
55
55
Exemple de service
public class PeopleService : IPeopleService { private SQLiteAsyncConnection _connection; public SQLiteAsyncConnection Connection { get { if (_connection == null) { string dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "dbPeople.sqlite"); _connection = new SQLiteAsyncConnection(dbPath); } return _connection; } } public async Task CreateTablePersonAsync() { await Connection.CreateTableAsync<Person>(); } public async Task Seed() { List<Person> people = new List<Person>(){ new Person(){Name="Marie Bellin",Email="[email protected]"}, new Person(){Name="Jérôme Romagny",Email="[email protected]"} }; await Connection.InsertAllAsync(people); } public async Task<List<Person>> GetAllAsync() { return await Connection.Table<Person>().ToListAsync(); } public async Task<Person> GetOneAsync() { return await Connection.Table<Person>().FirstOrDefaultAsync(); } public async Task AddAsync(Person person) { await Connection.InsertAsync(person); } public async Task<int> UpdateAsync(Person person) { return await Connection.UpdateAsync(person); } public async Task<int> DeleteAsync(Person person) { return await Connection.DeleteAsync(person); } }
Utilisation
Création de la table et remplissage
PeopleService peopleService = new PeopleService(); peopleService.CreateTablePersonAsync(); peopleService.Seed();
Ouverture de connexion, création de la
base si elle n’existe pas
Création d’une table selon
le modèle
Remplissage de la table
CRUD
![Page 57: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/57.jpg)
56
56
20. Http
a. HttpClient (System.Web.Http) Utilisation de System.Web.Http (ne pas confondre avec System.Net.Http) .Exemple de service
public class PeopleService { private string baseUri = "http://localhost:3600/api/people"; public async Task<Person[]> GetAllAsync() { using ( var client = new HttpClient()) { var response = await client.GetStringAsync(new Uri(baseUri)); var people = JsonConvert.DeserializeObject<Person[]>(response); return people; } } public async Task<Person> GetOneAsync(int id) { using (var client = new HttpClient()) { var response = await client.GetStringAsync(new Uri(baseUri + id)); var person = JsonConvert.DeserializeObject<Person>(response); return person; } } public async Task<Person> AddAsync(Person person) { using (var client = new HttpClient()) { string json = JsonConvert.SerializeObject(person); var response = await client.PostAsync(new Uri(baseUri),new HttpStringContent(json)); var content = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject<Person>(content); return result; } } public async Task<Person> UpdateAsync(Person person) { using (var client = new HttpClient()) { string json = JsonConvert.SerializeObject(person); var response = await client.PutAsync(new Uri(baseUri), new HttpStringContent(json)); var content = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject<Person>(content); return result; } } public async void DeleteAsync(int id) { using (var client = new HttpClient()) { await client.DeleteAsync(new Uri(baseUri + id)); } } }
Utilisation de
JSON.NET pour
la sérialisation
L’émulateur supporte les
Web Services « Local »
![Page 58: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/58.jpg)
57
57
Une requête plus détaillée
using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.TryParseAdd("application/json"); string json = JsonConvert.SerializeObject(person); HttpResponseMessage response = await client.PostAsync(new Uri(baseUri), new HttpStringContent(json)); var statusCode = response.StatusCode; response.EnsureSuccessStatusCode(); var responseHeaders = response.Headers; var content = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject<Person>(content); return result; }
Credential
var filter = new HttpBaseProtocolFilter(); var credential = new PasswordCredential("MyAppResource", username, password); filter.ServerCredential = credential; filter.ProxyCredential = credential; using (var client = new HttpClient()) { var response = await client.GetStringAsync(new Uri(baseUri)); // etc. }
b. Web Authentication Broker Authentification et identité des utilisateurs, Web Authentification Broker, Exemple
Header de la
requête
Header de la réponse
Exception si le code n’est pas 200
![Page 59: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/59.jpg)
58
58
c. BackgroundTransfer (transfert en arrière-plan) A utiliser pour les téléchargements « volumineux » (photo, vidéo, musique)
Documentation, Exemple
Download CancellationTokenSource cts; DownloadOperation download; private async void DownloadFileAsync(Uri sourceUri, string destination) { cts = new CancellationTokenSource(); var destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName); var downloader = new BackgroundDownloader(); download = downloader.CreateDownload(sourceUri, destinationFile); try { var progressCallback = new Progress<DownloadOperation>(DownloadProgress); await download.StartAsync().AsTask(cts.Token, progressCallback); } catch (TaskCanceledException) { Log("Téléchargement annulé."); } catch (Exception ex) { } } private void DownloadProgress(DownloadOperation download) { Log(String.Format(CultureInfo.CurrentCulture, "Progression : {0}, Statut: {1}", download.Guid, download.Progress.Status)); double percent = 100; if (download.Progress.TotalBytesToReceive > 0) { percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive; } DisplayProgress(percent); Log(String.Format(CultureInfo.CurrentCulture, " - bytes transférés : {0} of {1}, {2}%", download.Progress.BytesReceived, download.Progress.TotalBytesToReceive, percent)); if (download.Progress.HasRestarted) { Log(" - Téléchargement repris"); } if (download.Progress.HasResponseChanged) { Log(" - Response updated; Header count:" + download.GetResponseInformation().Headers.Count); } } private async void DisplayProgress(double percent) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { pg.Value = percent; }); } private async void Log(string message) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { result.Text += message + "\r\n"; }); }
On reporte la progression avec une « progressbar »
Affichage de messages dans « TextBlock »
Déclarés en dehors de manière à pouvoir y accèder pour les
clicks sur boutons « cancel », « pause », « resume »
![Page 60: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/60.jpg)
59
59
Utilisation (depuis le click sur un bouton)
var uri = new Uri(source.Text); DownloadFileAsync(uri, destination.Text);
Cancel (click sur un bouton cancel)
private void OnCancelDownload(object sender, RoutedEventArgs e) { cts.Cancel(); cts.Dispose(); cts = new CancellationTokenSource(); }
Pause/ Resume
private void OnPause(object sender, RoutedEventArgs e) { if (download.Progress.Status == BackgroundTransferStatus.Running) { download.Pause(); Log("Téléchargement en pause"); } } private void OnResume(object sender, RoutedEventArgs e) { if (download.Progress.Status == BackgroundTransferStatus.PausedByApplication) { download.Resume(); Log("Téléchargement repris"); } }
![Page 61: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/61.jpg)
60
60
Upload
1. Ouverture d’un « FileOpenPicker » et sélection du fichier à Uploader
private async void OnUpload(object sender, RoutedEventArgs e) { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.FileTypeFilter.Add("*"); picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; picker.PickSingleFileAndContinue(); }
2. « App » (après choix du fichier, l’application est « réactivée »)
protected override void OnActivated(IActivatedEventArgs e) { base.OnActivated(e); Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); } var continunationEventArgs = e as IContinuationActivatedEventArgs; if (continunationEventArgs != null) { switch (continunationEventArgs.Kind) { case ActivationKind.PickFileContinuation: var result = continunationEventArgs as FileOpenPickerContinuationEventArgs; var file = result.Files.FirstOrDefault(); rootFrame.Navigate(typeof(MainPage), file); break; default: break; } } }
Sélection du fichier à
uploader (par exemple une
image dans la bilbiothèque
d’images)
Retour dans
l’application et
progression du
transfert
Le click sur le
bouton lance un
FileOpenPicker
Au click sur le bouton
« Upload » ouverture du
picker
Récupération du fichier
sélectionné (StorageFile) ,
navigation et passage du
paramètre
![Page 62: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/62.jpg)
61
61
3. « Upload »
protected override void OnNavigatedTo(NavigationEventArgs e) { if (e.Parameter != null) { var file = e.Parameter as StorageFile; UploadFile(file); } } private async Task UploadFile(StorageFile file) { var uploader = new BackgroundUploader(); uploader.SetRequestHeader("Filename", file.Name); var destionationUri = new Uri("http://<destination>.com/"); var upload = uploader.CreateUpload(destionationUri, file); await upload.StartAsync().AsTask(new Progress<UploadOperation>(UploadProgress)); } private void UploadProgress(UploadOperation upload) { Log(String.Format(CultureInfo.CurrentCulture, "Progression : {0}, Statut: {1}", upload.Guid, upload.Progress.Status)); double percent = 100; if (upload.Progress.TotalBytesToSend > 0) { percent = upload.Progress.BytesSent * 100 / upload.Progress.TotalBytesToSend; } DisplayProgress(percent); Log(String.Format(CultureInfo.CurrentCulture, " - bytes transférés : {0} of {1}, {2}%", upload.Progress.BytesSent, upload.Progress.TotalBytesToSend, percent)); if (upload.Progress.Status == BackgroundTransferStatus.Error) { Log("Erreur"); } if (upload.Progress.Status == BackgroundTransferStatus.Completed) { Log("Upload terminé"); } } private async void DisplayProgress(double percent) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { pg.Value = percent; }); } private async void Log(string message) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { result.Text += message + "\r\n"; }); }
Récupération du paramètre passé
et lancement de l’upload
![Page 63: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/63.jpg)
62
62
21. Vignette (« Tile ») Documentation, exemple
Tailles de vignettes :
Petite (71x71) (« Square71x71Logo.png »)
Moyenne (150x150) (« Logo.png »)
Large (310x150) (« WideLogo.png »)
Minuscule (44x44) (« SmallLogo.png »)
On peut également remplacer dans les « ressources visuelles »:
L’écran de démarrage (480x800) (« SplashScreen.png »)
Le logo Windows Store (50x50) (« StoreLogo.png »)
Supprimer les images du dossier « Assets » et ajouter des images pour chaque taille de vignette
Permet d’afficher le nom
de l’application sur les
différentes vignettes
![Page 64: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/64.jpg)
63
63
On peut également changer le nom de l’application (affichable sur les vignettes) depuis le
manifeste
Vignette de l’application
a. Epingler la vignette de l’application à l’écran d’accueil Maintenir enfoncé le « doigt » sur le logo … « Pin to Start »
On peut changer la taille, déplacer,
détacher la vignette depuis l’écran
d’accueil
![Page 65: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/65.jpg)
64
64
b. Vignette secondaire (« SecondaryTile ») « Pin »
const string tileId = "SecondaryTile.Logo"; private async void PinSecondaryTile(string tileId) { var square150x150Logo = new Uri("ms-appx:///Assets/Logo.png"); var wide310x150Logo = new Uri("ms-appx:///Assets/WideLogo.png"); string tileActivationArguments = " mon argument"; var tile = new SecondaryTile(tileId, "Vignette secondaire", tileActivationArguments, square150x150Logo, TileSize.Square150x150); tile.VisualElements.Wide310x150Logo = wide310x150Logo; tile.VisualElements.ShowNameOnSquare150x150Logo = true; tile.VisualElements.ShowNameOnWide310x150Logo = true; tile.VisualElements.ForegroundText = ForegroundText.Dark; tile.RoamingEnabled = false; await tile.RequestCreateAsync(); }
Une vignette secondaire
Argument ajouté
- Tile id
- Texte affiché sur la vignette
secondaire
- Arguments qui pourront être
retrouvés et passés si
l’application est lancée depuis
cette vignette secondaire
- Uri de l’image 150x150
- Taille de vignette désirée
Id de la tuile
On peut créer un bouton dans la barre
de la page pour « épingler » une
vignette secondaire
Vignette de l’application
![Page 66: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/66.jpg)
65
65
« UnPin »
private async void UnPinSecondaryTile(string tileId, object sender) { if (SecondaryTile.Exists(tileId)) { var secondaryTile = new SecondaryTile(tileId); bool isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync( GetElementRect((FrameworkElement)sender), Placement.Below); if (isUnpinned) { var dialog = new MessageDialog("Vignette secondaire supprimée!"); await dialog.ShowAsync(); } } } public Rect GetElementRect(FrameworkElement element) { GeneralTransform buttonTransform = element.TransformToVisual(null); Point point = buttonTransform.TransformPoint(new Point()); return new Rect(point, new Size(element.ActualWidth, element.ActualHeight)); }
![Page 67: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/67.jpg)
66
66
c. Notification de vignette Catalogue de modèles de vignettes. Selon le « type » d’application, des modèles sont
recommandés.
3 « types » de vignettes :
Animées (« Peek »)
« Gros chiffre » (« Block »)
Collection d’images (« ImageCollection »)
Exemple avec une animation.
Plusieurs possibilités pour créer la vignette (Xml, avec « TileTemplateType » ou en utilisant une
librairie comme « NotificationExtensions »)
XML
string image = "ms-appx:///Assets/pix.jpg"; string message = "Nouveau message!"; string xml = string.Format(@"<tile> <visual version=""2""> <binding template=""TileWide310x150PeekImageAndText01"" fallback=""TileWidePeekImageAndText01""> <image id=""1"" src=""{0}""/> <text id=""1"">{1}</text> </binding> </visual> </tile>", image, message); var document = new XmlDocument(); document.LoadXml(xml); var notification = new TileNotification(document); TileUpdateManager.CreateTileUpdaterForSecondaryTile(tileId).Update(notification);
Vignette 150x150
Vignette 310x150
Pour la vignette de l’application remplacer
par « CreateTileUpdaterForApplication »
![Page 68: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/68.jpg)
67
67
Avec « TileTemplateType »
string message = "Nouveau message!"; var template = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150PeekImageAndText01); var textNodes = template.GetElementsByTagName("text"); textNodes[0].InnerText = message; var imageNodes = template.GetElementsByTagName("image"); ((XmlElement)imageNodes[0]).SetAttribute("src", "ms-appx:///Assets/pix.jpg"); var notification = new TileNotification(template); TileUpdateManager.CreateTileUpdaterForSecondaryTile(tileId).Update(notification);
Avec « Notifications Extensions » (installer avec les packages NuGet la version non PCL)
string image = "ms-appx:///Assets/pix.jpg"; string message = "Nouveau message!"; var template = TileContentFactory.CreateTileWide310x150PeekImageAndText01(); template.TextBodyWrap.Text = message; template.Image.Src = image; var square150x150Template = TileContentFactory.CreateTileSquare150x150Text01(); square150x150Template.TextBody1.Text = message; template.Square150x150Content = square150x150Template; TileUpdateManager.CreateTileUpdaterForSecondaryTile(tileId).Update(largeTemplate.CreateNotification());
a. Définir le template pour plusieurs tailles de vignette string xml = string.Format(@"<tile> <visual version=""2""> <binding template=""TileWide310x150PeekImageAndText01"" fallback=""TileWidePeekImageAndText01""> <image id=""1"" src=""{0}""/> <text id=""1"">{1}</text> </binding> <binding template=""TileSquare150x150PeekImageAndText02"" fallback=""TileSquarePeekImageAndText02""> <image id=""1"" src=""{0}"" alt=""alt text""/> <text id=""1"">{1}</text> <text id=""2"">{2}</text> </binding> </visual> </tile>", image, title, message);
b. « AddToSchedule » pour différer la modification // etc. var notification = new ScheduledTileNotification(document, DateTimeOffset.Now.AddSeconds(10)); var updater = TileUpdateManager.CreateTileUpdaterForApplication(); updater.AddToSchedule(notification);
c. « Periodic Update » : Web Service (Web Api, WCF, etc.) appelé périodiquement var updater = TileUpdateManager.CreateTileUpdaterForApplication(); var uri = new Uri("http://localhost:3660/api/Notification/"); updater.StartPeriodicUpdate(uri, PeriodicUpdateRecurrence.Hour);
Il est possible également de définir depuis le manifeste sans avoir à taper de code.
La vignette sera modifiée
dans 10 secondes
Template pour la
vignette large
(310x150)
Template pour la
vignette moyenne
(150x150)
![Page 69: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/69.jpg)
68
68
d. Supprimer les mises à jour de la vignette principale TileUpdateManager.CreateTileUpdaterForApplication().Clear();
e. Schémas d’Uri Schémas d’Uri
- « http:// …» ou « https://... » Image Web.
- « ms-appx:/// ... » Image incluse dans le package de l’application.
- « ms-appdata:///local/ ... » Image enregistrée sur le stockage local.
- « file:/// ... » Image locale. (Uniquement prise en charge pour les applications de bureau.)
f. Récupérer les arguments au lancement de l’application depuis la vignette
secondaire « App »
protected override void OnLaunched(LaunchActivatedEventArgs e) { // etc. if (!rootFrame.Navigate(typeof(MainPage), e.Arguments)) { throw new Exception("Failed to create initial page"); } // etc. }
Dans la page
protected override void OnNavigatedTo(NavigationEventArgs e) { if (e.Parameter != null) { } }
Si l’application est lancée depuis
une vignette secondaire, les
arguments seront passés
automatiquement en paramètre
Récupération du paramètre
passé
![Page 70: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/70.jpg)
69
69
g. Badges Vue d’ensemble des badges
Seulement 2 glyphes avec Windows Phone : « Alert » et « Attention » plus les nombres. Les badges
peuvent être utilisés pour les vignettes de l’application ou secondaires.
XML
string xml = "<badge value='Attention'/>"; var document = new XmlDocument(); document.LoadXml(xml); var notification = new BadgeNotification(document); BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(notification);
Avec « NotificationsExtensions »
var badgeContent = new BadgeGlyphNotificationContent(GlyphValue.Attention); BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
Avec un nombre utiliser « BadgeNumericContent »
var badgeContent = new BadgeNumericNotificationContent(50); BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
Supprimer un badge
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
h. Push Notifcation Service (WNS) Documentation
« Alert », « Attention » ou
nombre
![Page 71: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/71.jpg)
70
70
22. Toasts Documentation, exemple
Seulement deux Templates disponibles avec Windows Phone : « ToastText01 » et « ToastText02 ». Si
on essaie d’utiliser un autre Template, il sera affiché comme si c’était « ToastText02 »
Activer les notifications par toast depuis le manifeste :
a. « ToastText01 »
<toast>
<visual>
<binding template="ToastText01">
<text id="1">bodyText</text>
</binding>
</visual>
</toast>
XML
string message = "Nouveau message!"; string xml = string.Format(@"<toast> <visual> <binding template=""ToastText01""> <text id=""1"">{0}</text> </binding> </visual> </toast>", message); var document = new XmlDocument(); document.LoadXml(xml); var notification = new ToastNotification(document); ToastNotificationManager.CreateToastNotifier().Show(notification);
Avec « ToastTemplateType »
string message = "Nouveau message!"; var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); var textNodes = template.GetElementsByTagName("text"); textNodes[0].InnerText = message ; var notification = new ToastNotification(template); var manager = ToastNotificationManager.CreateToastNotifier(); manager.Show(notification);
Toast avec message et Logo de
l’application (150x150)
![Page 72: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/72.jpg)
71
71
b. « ToastText02 »
<toast>
<visual>
<binding template="ToastText02">
<text id="1">headlineText</text>
<text id="2">bodyText</text>
</binding>
</visual>
</toast>
XML
string title = "Mon application"; string message = "Nouveau message!"; string xml = string.Format(@"<toast> <visual> <binding template=""ToastText02""> <text id=""1"">{0}</text> <text id=""2"">{1}</text> </binding> </visual> </toast>", title, message); var document = new XmlDocument(); document.LoadXml(xml); var notification = new ToastNotification(document); ToastNotificationManager.CreateToastNotifier().Show(notification);
Avec « ToastTemplateType »
string title = "Mon application"; string message = "Nouveau message!"; var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); var textNodes = template.GetElementsByTagName("text"); textNodes[0].InnerText = title ; textNodes[1].InnerText = message ; var notification = new ToastNotification(template); var manager = ToastNotificationManager.CreateToastNotifier(); manager.Show(notification);
Note : On pourrait également utiliser « Notifications Extensions ».On pourrait également se
créer un mini service de toasts.
Toast avec titre et message et Logo de
l’application (150x150)
![Page 73: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/73.jpg)
72
72
c. Toast différé (« Scheduled ») string title = "Mon application"; string message = "Nouveau message!"; string xml = string.Format(@"<toast> <visual> <binding template=""ToastText02""> <text id=""1"">{0}</text> <text id=""2"">{1}</text> </binding> </visual> </toast>", title, message); var document = new XmlDocument(); document.LoadXml(xml); var notification = new ScheduledToastNotification(document,DateTimeOffset.Now.AddSeconds(10)); ToastNotificationManager.CreateToastNotifier().AddToSchedule(notification);
d. Déclencher une action au clic sur le toast (« Activated ») string title = "Mon application"; string message = "Nouveau message!"; string xml = string.Format(@"<toast> <visual> <binding template=""ToastText02""> <text id=""1"">{0}</text> <text id=""2"">{1}</text> </binding> </visual> </toast>", title, message); var document = new XmlDocument(); document.LoadXml(xml); var notification = new ToastNotification(document); notification.Activated += async (s, ex) => { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { var dialog = new MessageDialog("Cliqué!"); await dialog.ShowAsync(); }); }; ToastNotificationManager.CreateToastNotifier().Show(notification);
![Page 74: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/74.jpg)
73
73
e. Le centre de notifications
Ajouter seulement la notification au centre de notifications sans faire apparaitre de toast.
var notification = new ToastNotification(document); notification.SuppressPopup = true;
23. MessageDialog Documentation
Déroulé par en haut. Répertorie
les notifications reçues
![Page 75: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/75.jpg)
74
74
24. Gestion des contacts et rendez-vous de l’utilisateur
a. Gestion des contacts Avec Picker (ContactPicker)
var picker = new ContactPicker(); picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email); var contact = await picker.PickContactAsync(); if (contact != null) { firstNameTextBlock.Text = contact.FirstName; var stream = await contact.Thumbnail.OpenReadAsync(); if (stream != null && stream.Size > 0) { var bitmap = new BitmapImage(); bitmap.SetSource(stream); outputImage.Source = bitmap; } }
Par code
var store = await ContactManager.RequestStoreAsync(); IReadOnlyList<Contact> contacts = await store.FindContactsAsync(); // var contact = await store.GetContactAsync(id);
Nécessite l’ajout de la capacité « Contacts » dans le manifeste
Documentation
Récupération et
affichage des
informations du
contact
Récupération des contacts
(liste en lecture seule)
Récupération d’un contact
avec l’id
![Page 76: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/76.jpg)
75
75
b. Envoi d’Email à un contact Ajouter un compte email afin de pouvoir envoyer des messages avec le téléphone … Aller dans les
« settings » … « emails&accounts » … « Add an account »
Démarche
1. Sélection du contact avec un picker (ContactPicker)
2. On récupère l’adresse Email du contact
3. On rédige le sujet, message, ajoute pièces jointes, etc.
4. Envoi
Sélection du compte pour l’envoi
d’email
Sélection d’un contact
![Page 77: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/77.jpg)
76
76
private async void GetContact() { var picker = new ContactPicker(); picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email); var contact = await picker.PickContactAsync(); if (contact != null) { SendEmail(contact.Emails.FirstOrDefault().Address); } } private async void SendEmail(string addressTo) { var email = new EmailMessage(); email.To.Add(new EmailRecipient(addressTo)); await EmailManager.ShowComposeNewEmailAsync(email); }
Envoi
Le sujet, message, pièces
jointes seront
automatiquement
ajoutées
Ecriture de l’email (sujet, message,
pièces jointes) …
![Page 78: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/78.jpg)
77
77
On peut également définir les champs « pré remplis »
private async void SendEmail(string subject, string messageBody, string addressTo, StorageFile attachmentFile) { var email = new EmailMessage(); email.Subject = subject; email.Body = messageBody; if (attachmentFile != null) { var stream = RandomAccessStreamReference.CreateFromFile(attachmentFile); var attachment = new EmailAttachment(attachmentFile.Name,stream); email.Attachments.Add(attachment); } email.To.Add(new EmailRecipient(addressTo)); await EmailManager.ShowComposeNewEmailAsync(email); } }
Utilisation
SendEmail("Mon sujet","message de test","[email protected]",null);
Documentation
![Page 79: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/79.jpg)
78
78
c. Envoi d’un SMS à un contact
Envoi de SMS avec le numéro de téléphone du contact
private async void SendSms(string phoneNumber) { var chatMessage = new ChatMessage(); chatMessage.Recipients.Add(phoneNumber); await ChatMessageManager.ShowComposeSmsMessageAsync(chatMessage); }
Documentation
d. Gestion de rendez-vous Documentation, exemple
![Page 80: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/80.jpg)
79
79
25. Contrats de partage Documentation, exemple
a. Application pouvant partager du contenu
Ajouter la capacité dans le manifeste
On partage le contenu
de son application
(exemple du texte)
Choix de
l’application
cible du partage
L’application cible
récupère et affiche
les données
partagées
![Page 81: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/81.jpg)
80
80
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested; } protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); DataTransferManager.GetForCurrentView().DataRequested -= MainPage_DataRequested; } private void AppBarButton_Click(object sender, RoutedEventArgs e) { DataTransferManager.ShowShareUI(); } void MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) { var request = args.Request; var deferral = request.GetDeferral(); request.Data.Properties.Title = "Demo partage"; request.Data.Properties.Description = "Montre comment partager du contenu"; request.Data.SetText(textBox1.Text); // request.Data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat("<span style='color:red'>du texte au format html</span>")); request.Data.SetWebLink(new Uri("http://romagny13.com/mobile/")); var image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Images/image_1.jpg")); request.Data.SetBitmap(image); request.Data.Properties.Thumbnail = image; deferral.Complete(); } }
Affiche le volet de
partage
Ajouter un titre et une
description
Elements partagés. Selon les
élements partagés les
applications cibles varient
![Page 82: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/82.jpg)
81
81
b. Créer une application « Cible de partage » Enregistrer une application comme cible de partage
L’application apparait
en cible de partage
Affichage des
informations
partagées
On déclenche
« ReportCompleted » qui
met fin au partage et
ferme l’application cible
de partage
![Page 83: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/83.jpg)
82
82
L’application apparaitra en cible de partage pour les formats définis (« Déclarations » du manifeste)
Dans « App »
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); Window.Current.Content = rootFrame; } rootFrame.Navigate(typeof(MySharePage), args.ShareOperation); Window.Current.Activate(); }
L’application cible est activée,
on navigue vers la page en
passant les informations
partagées
![Page 84: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/84.jpg)
83
83
Création d’une page qui sera affichée avec les données partagées (exemple « MySharePage »)
public sealed partial class MySharePage : Page { public MySharePage() { this.InitializeComponent(); } ShareOperation shareOperation; protected async override void OnNavigatedTo(NavigationEventArgs e) { shareOperation = e.Parameter as ShareOperation; titleTextBlock.Text = shareOperation.Data.Properties.Title; descTextBlock.Text = shareOperation.Data.Properties.Description; contentTextBlock.Text = await shareOperation.Data.GetTextAsync(); var uri = await shareOperation.Data.GetWebLinkAsync(); webLink.NavigateUri = uri; webLink.Content = uri.AbsoluteUri; if (shareOperation.Data.Properties.Thumbnail != null) { var streamThumbnail = await shareOperation.Data.Properties.Thumbnail.OpenReadAsync(); var thumbnailImage = new BitmapImage(); thumbnailImage.SetSource(streamThumbnail); thumbnail.Source = thumbnailImage; } var photoFile = await shareOperation.Data.GetBitmapAsync(); var stream = await photoFile.OpenReadAsync(); var bitmap = new BitmapImage(); bitmap.SetSource(stream); image.Source = bitmap; } private void ShareButton_Click(object sender, RoutedEventArgs e) { shareOperation.ReportStarted(); // ... if (shareOperation.Data.Contains(StandardDataFormats.Bitmap)) { } shareOperation.ReportCompleted(); } }
On récupère les
informations
partagées dans
« OnNavigatedTo »
Thumbnail
Image
Lorsque l’utilisateur clique
sur le bouton, on pourrait
sauvegarder les informations
localement par exemple.
« ReportCompleted » met fin
au partage et ferme le volet
![Page 85: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/85.jpg)
84
84
<Page.BottomAppBar> <CommandBar> <AppBarButton Icon="Accept" Click="ShareButton_Click"/> <AppBarButton Icon="Cancel" /> </CommandBar> </Page.BottomAppBar> <Grid Background="Black"> <Grid.ChildrenTransitions> <TransitionCollection> <EntranceThemeTransition/> </TransitionCollection> </Grid.ChildrenTransitions> <Grid Margin="40,20,40,60"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel> <Image x:Name="thumbnail" Stretch="Uniform" VerticalAlignment="Top" MinWidth="80" MinHeight="80" MaxWidth="240" MaxHeight="160" Margin="0,0,20,0"/> <TextBlock x:Name="titleTextBlock" Margin="0,-10,0,20" TextWrapping="NoWrap" Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}" Style="{StaticResource SubheaderTextBlockStyle}"/> <TextBlock x:Name="descTextBlock" MaxHeight="60" Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}" Style="{StaticResource BodyTextBlockStyle}"/> </StackPanel> <Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock x:Name="contentTextBlock" Text="Description" Style="{ThemeResource BodyTextBlockStyle}"/> <HyperlinkButton x:Name="webLink" Grid.Row="1" Content="Mon lien" Style="{ThemeResource PivotTitleContentControlStyle}"/> <Image x:Name="image" Grid.Row="2"/> </Grid> </Grid>
![Page 86: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/86.jpg)
85
85
26. Tâches en arrière-plan (BackgroundTask) Documentation, exemple
1. Création d’un projet de type « Composant Windows Runtime (Windows Phone) » pour la
tâche
public sealed class MyTask : IBackgroundTask { public async void Run(IBackgroundTaskInstance taskInstance) { var deferral = taskInstance.GetDeferral(); taskInstance.Progress = 0; taskInstance.Canceled += (sender, e) => { }; for (uint i = 0; i < 10; i++) { taskInstance.Progress = i + 1; await Task.Delay(2000); } deferral.Complete(); } }
Enregistrement
de la tâche
Changement de Timer zone … la
condition étant remplie, la tâche
s’éxécute
Barre de progression et
message une fois la tâche finie
(que l’on peut voir en revenant
en arrière vers l’application)
Projet de type « Composant Windows Runtime
(Windows Phone) » avec la tâche
Implémente
« IBackgroundTask »
Pour la démo, on fait une boucle et
reporte la progression mais cela
pourrait être n’importe quel
traitement
Manifeste avec la déclaration
On peut s’abonner à
« Canceled »
![Page 87: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/87.jpg)
86
86
2. Dans le projet de démo
public sealed partial class MainPage : Page { // code des boutons , etc. private string taskName = "MyBackgroundTask"; private async void RegisterTask() { if (TaskIsRegistered(taskName)) return; await BackgroundExecutionManager.RequestAccessAsync(); var builder = new BackgroundTaskBuilder(); builder.Name = taskName; var trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false); builder.SetTrigger(trigger); builder.TaskEntryPoint = typeof(MyTask).FullName; var task = builder.Register(); task.Progress += OnProgress; task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted); } private bool TaskIsRegistered(string taskName) { foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == taskName) { return true; } } return false; } async void OnProgress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { pg.Value = args.Progress; }); } private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args) { var key = task.TaskId.ToString(); string message = "Tâche terminée - " + DateTime.Now.ToString("U"); DisplayMessage(message); } private async void DisplayMessage(string message) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { result.Text += message + "\n"; }); } private void UnregristerTask(string taskName) { foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == taskName) task.Value.Unregister(true); } } }
Requis seulement avec
Windows Phone
On vérifie que la tâche n’est pas
déjà enregistrée
Trigger et condition
Enregistrement et
abonnement
Vérifie que la tâche n’est pas déjà
enregistrée
Appelé à la fin de l’éxécution de la
tâche
On reporte la progression sur une
« ProgressBar »
![Page 88: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/88.jpg)
87
87
Triggers
SystemTrigger S’éxécute suite à un événement système (UserPresent, InternetAvailable, etc.) TimeTrigger S’éxécute régulièrement (toutes les 30 min par ex) LocationTrigger S’éxécute quand l’utilisateur change de location MaintenanceTrigger S’éxécute régulièrement (toutes les 30 min minimum) PushNotificationTrigger S’éxécute suite à une notification push
Condition … on pourrait également utiliser la méthode « AddCondition »
var builder = new BackgroundTaskBuilder(); builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
Ajout d’une référence au projet de tâche
Et dans le manifeste..
Note on peut s’aider pour déboger la tâche avec la barre d’outils « Emplacement de débogage »
![Page 89: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/89.jpg)
88
88
27. Maps, Géolocation
a. « Launch Uri » Cartes et indications, Schéma d’Uri des cartes
Emplacement
var uri = new Uri("bingmaps:?cp=40.726966~-74.006076"); LaunchMap(uri);
Recherche
var uri = new Uri("bingmaps:?where=" + textBox1.Text); LaunchMap(uri);
Exemple avec « Moulins, France »
Itinéraires (peut demander à installer une application permettant d’afficher des itinéraires
avec le Windows Store si aucune n’est installée)
Documentation, trouver facilement latittude et longitude
string latitude = "46,568059000000000000"; string longitude = "3,334417000000030400"; string name = "Moulins, FRANCE"; Uri uri = new Uri("ms-drive-to:?destination.latitude=" + latitude + "&destination.longitude=" + longitude + "&destination.name=" + name); LaunchMap(uri);
Méthode utilisée
private async void LaunchMap(Uri uri) { var success = await Launcher.LaunchUriAsync(uri); if (success) { } else { } }
![Page 90: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/90.jpg)
89
89
b. MapControl Se rendre sur « bingmapsportal » et se connecter avec son compte Microsoft…
Puis créer une clé.. Menu« My Account » … « Create or view keys »
Dans son projet Windows Phone, ajout d’un MapControl avec la clé <Page … xmlns:maps="using:Windows.UI.Xaml.Controls.Maps"> <Grid> <maps:MapControl x:Name="map" MapServiceToken="Agv8Tk4IzcDNalTQsuq_4VUrPACK3PGY_7sBUCio0Ch7xpNqxhptUtz_123456"> </maps:MapControl> </Grid> </Page>
Dans le code-behind de la page
protected override void OnNavigatedTo(NavigationEventArgs e) { map.Center = new Geopoint(new BasicGeoposition() { Latitude = 46.568059000000000000, Longitude = 3.334417000000030400 }); map.ZoomLevel = 12; map.LandmarksVisible = true; }
Recopier la clé
Ajouter la clé. Il est
possible aussi de la
définir en code
Navigation et réglage des
propriétés du
« mapControl »
![Page 91: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/91.jpg)
90
90
28. Windows Phone Silverlight
Pivot Namespace
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
<phone:Pivot Title="MON APPLICATION"> <phone:PivotItem Header="item1"> <!-- content--> </phone:PivotItem> <phone:PivotItem Header="item2"></phone:PivotItem> </phone:Pivot>
Panorama Proche du contrôle Hub
<phone:Panorama Title="Panorama demo"> <phone:Panorama.Background> <ImageBrush ImageSource="PanoramaBackground.png"/> </phone:Panorama.Background> <phone:PanoramaItem Header="premier élément"> <!-- --> </phone:PanoramaItem> <phone:PanoramaItem Header="second élément"></phone:PanoramaItem> </phone:Panorama>
Barre de commandes <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar Mode="Default" Opacity="1.0" IsMenuEnabled="True" IsVisible="True"> <shell:ApplicationBarIconButton IconUri="/Images/save.png" Text="save"/> <shell:ApplicationBarIconButton IconUri="/Images/refresh.png" Text="refresh"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="menu item 1" /> <shell:ApplicationBarMenuItem Text="menu item 2" /> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>
Windows Phone Toolkit CodePlex
![Page 92: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/92.jpg)
91
91
Navigation Utilisation de Navigation Service.
NavigationService.Navigate(new Uri("/Views/ItemDetailsPage.xaml", UriKind.Relative));
Passage de paramètres
NavigationService.Navigate(new Uri("/Views/ItemDetailsPage.xaml?firstName=Marie&lastName=Bellin", UriKind.Relative));
… Récupération des paramètres dans la page réceptrice protected override void OnNavigatedTo(NavigationEventArgs e) { string firstName; string lastName; NavigationContext.QueryString.TryGetValue("firstName", out firstName); NavigationContext.QueryString.TryGetValue("lastName", out lastName); }
Supprimer la page d’entrée de l’historique de navigation
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); NavigationService.RemoveBackEntry(); }
Retrouver la page sur laquelle on était en résumant l’application … Dans « App »
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e) { if (RootVisual != RootFrame) RootVisual = RootFrame; if (e.NavigationMode == NavigationMode.Reset) { while (RootFrame.RemoveBackEntry() != null) { } } RootFrame.Navigated -= CompleteInitializePhoneApplication; }
![Page 93: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/93.jpg)
92
92
Cycle de vie Page State : utilisé pour sauver les « états de la page »
Deactivated et Activated : utilisation de PhoneApplicationService pour conserver les données et états des pages
Application Closing (voir Deactivated) : On sauve les données de manière persistante (IsolatedStorage)
Application State : propriétés static (Viewmodels souvent) définies dans « App » .Ceux-ci peuvent avoir des méthodes pour sauver/restaurer les données.
Launching peut également être utilisé pour charger des données au lancement de l’application
Page state
protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); if (e.NavigationMode != NavigationMode.Back) this.State["checkBox1CheckedState"] = checkBox1.IsChecked; } protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (State.ContainsKey("checkBox1CheckedState")) checkBox1.IsChecked = (bool)this.State["checkBox1CheckedState"]; }
![Page 94: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/94.jpg)
93
93
Visual States et orientations SupportedOrientations (orientations supportées) : Landscape, Portrait ou
PortraitAndLandscape .Si on définit « Portrait » par exemple, lorsqu’on passe en mode
landscape, la page n’est pas « tournée », pour cela il faut définir
« PortraitAndLandscape ».
Orientation : orientation « courante »
Il peut être bon de définir des états visuels(VisualStates) selon l’orientation pour une meilleure
expérience utilisateur. Exemple : on repositionne une listBox selon l’orientation
<VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="OrientationStates"> <VisualState x:Name="Portrait"/> <VisualState x:Name="Landscape"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Row)" Storyboard.TargetName="listBox1"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <System:Int32>0</System:Int32> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Column)" Storyboard.TargetName="listBox1"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <System:Int32>1</System:Int32> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup>
Dans la balise de la page :
SupportedOrientations="PortraitOrLandscape" Orientation="Landscape" OrientationChanged="PhoneApplicationPage_OrientationChanged"
« OrientationChanged » (page)
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e) { if ((e.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape) { VisualStateManager.GoToState(this, "Landscape", false); } else { VisualStateManager.GoToState(this, "Portrait", false); } }
![Page 95: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/95.jpg)
94
94
Application State et PhoneApplicationService
Création d’une propriété dans « App » accessible pour n’importe quelle page Exemple
public partial class App : Application { private static ProductViewModel _viewModel; public static ProductViewModel ViewModel { get { if (_viewModel == null) _viewModel = new ProductViewModel(); return _viewModel; } set { _viewModel = value; } } // code retiré pour la clarté private void Application_Launching(object sender, LaunchingEventArgs e) { ViewModel.LoadFromIso(); } private void Application_Activated(object sender, ActivatedEventArgs e) { if (!e.IsApplicationInstancePreserved) { var appState = PhoneApplicationService.Current.State; if (appState.ContainsKey("Product")) { ViewModel.Product = appState["Product"] as Product; } } } private void Application_Deactivated(object sender, DeactivatedEventArgs e) { PhoneApplicationService.Current.State["Product"] = ViewModel.Product; SaveData(); } private void Application_Closing(object sender, ClosingEventArgs e) { SaveData(); } private void SaveData() { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; settings["Product"] = ViewModel.Product; settings.Save(); } }
![Page 96: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/96.jpg)
95
95
ViewModel
public class ProductViewModel : INotifyPropertyChanged { private Product _product; public Product Product { get { return _product; } set { _product = value; RaisePropertyChanged("Product"); } } public void LoadDataFromDB() { // simule chargement depuis une base de données Product = new Product() { ProductID = 1, ProductName = "Jeans bleu délavé", Description = "Beau jean bleu délavé coupe moderne." }; } public void LoadFromIso() { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if (settings.Contains("Product")) Product = settings["Product"] as Product; } public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } }
Page « détails »
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); DataContext = App.ViewModel; Deployment.Current.Dispatcher.BeginInvoke(() => { if (App.ViewModel.Product == null) App.ViewModel.LoadDataFromDB(); }); }
![Page 97: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/97.jpg)
96
96
Reminder, alarm et Background agent Documentation
Reminder
string name = "reminder1"; private void Schedule(object sender, RoutedEventArgs e) { // Microsoft.Phone.Scheduler var reminder = new Reminder(name) { Title = "Reminder title", Content = "reminder text content", BeginTime = DateTime.Now.AddSeconds(10), RecurrenceType = RecurrenceInterval.None, NavigationUri = new Uri("/Views/ItemDetailsPage.xaml", UriKind.Relative) }; if (ScheduledActionService.Find(name) != null) MessageBox.Show("Reminder already scheduled."); else ScheduledActionService.Add(reminder); } private void Deschedule(object sender, RoutedEventArgs e) { if (ScheduledActionService.Find(name) != null) { ScheduledActionService.Remove(name); MessageBox.Show("Reminder descheduled."); } }
Alarm // Microsoft.Phone.Scheduler Alarm alarm = new Alarm("alarm1"); alarm.Content = "Message alarm"; alarm.BeginTime = DateTime.Now.AddSeconds(10); // for demo alarm.ExpirationTime = DateTime.Now.AddMinutes(5); alarm.RecurrenceType = RecurrenceInterval.Daily; alarm.Sound = new Uri("/Sounds/alarm.wav",UriKind.Relative); ScheduledActionService.Add(alarm);
![Page 98: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/98.jpg)
97
97
Background agent Ajout d’un projet de type « ScheduledTaskAgent » (Agent des tâches planifiées Windows Phone) à la
solution
On définit le code à exécuter par la tache
protected override void OnInvoke(ScheduledTask task) { var toast = new ShellToast(); toast.Title = "New message!"; toast.Content = "the content message"; toast.NavigationUri = new Uri("/Views/ItemDetailsPage.xaml", UriKind.Relative); toast.Show(); NotifyComplete(); }
Dans l’application de démo.On ajoute une référence au projet
private void Button_Click(object sender, RoutedEventArgs e) { var taskName = "My task"; if (ScheduledActionService.Find(taskName) != null) ScheduledActionService.Remove(taskName); var periodicTask = new PeriodicTask(taskName) { Description = "Task description" }; try { ScheduledActionService.Add(periodicTask); ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(5)); } catch (Exception ex) { } }
Attention sous WP 7.x debugger avec un émulateur 512MB (les background agents étant
désactivés en 256MB) .De plus les toasts n’apparaissent que si on n’est pas sur l’application.
Au clic sur la notification, navigation vers la page « ItemDetailsPage » définie.
![Page 99: 0 Windows Phone 8 - romagny13.comromagny13.com/wp-content/uploads/2015/05/Windows-Phone.pdf · 7 7 3. Layouts a. Hub Démarrage rapide, exemple Utilisé pour la page principale. La](https://reader036.fdocuments.in/reader036/viewer/2022071016/5fcf1766417ccf01d57317a2/html5/thumbnails/99.jpg)
98
98
Désactiver les background tasks (dans les paramètres)