Précédent   Forum du club des développeurs et IT Pro > Dotnet > Développement Windows > WinRT
WinRT Forum d'entraide sur le développement d'applications Metro pour Windows 8 avec WinRT
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 08/06/2012, 10h08   #1
yupa
Membre du Club
 
Inscription : mai 2005
Messages : 192
Détails du profil
Informations forums :
Inscription : mai 2005
Messages : 192
Points : 44
Points : 44
Par défaut Mon premier slide show sous Win8

Bonjour,

Je débute dans le dev sur mobile pour Winsows 8. J'ai suivi ce tuto (plutôt bien fait).
Je l'ai fait en C#/XAML. Je suis toutes les instructions jusqu'au point 7. Quand je lance l'application, j'ai bien une image affichée, mais je ne trouve pas comment passer l'image suivante, "slider"...
Est-ce que qq a déjà fait ce tuto ou aurait une idée?

Voici le code C#
Code c# :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Media.PlayTo;
using Windows.UI;
using Windows.Storage;
using Windows.Storage.Streams;
 
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
 
namespace SlideShow
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
 
        private PlayToManager ptm = null;
        private DispatcherTimer timer = null;
        private IReadOnlyList<StorageFile> imageList = null; // Liste des images a afficher
        private int imageIdx = -1; // index de l'image courante
        private Image currentPlayer = null; // afficheur d'image courant
        private bool isStreaming; // True if Streaming
        private CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
 
        public MainPage()
        {
            this.InitializeComponent();
        }
 
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.ptm = PlayToManager.GetForCurrentView();
            this.ptm.SourceRequested += this.SourceRequested;
            this.ptm.SourceSelected += this.SourceSelected;
 
            this.timer = new DispatcherTimer();
            this.timer.Interval = new TimeSpan(0, 0, 5);
            this.timer.Tick += TimeoutElapsed;
 
            this.StartSlideShow();
        }
 
        /// <summary>
        /// Récupère la liste des images et démarre le slideShow
        /// </summary>
        async private void StartSlideShow()
        {
            var resultsLibrary = await Windows.Storage.KnownFolders.PicturesLibrary.GetFilesAsync();
            this.imageList = resultsLibrary;
 
            if (imageList.Count > 0)
            {
                SetImageSource(this.ImagePlayer1, this.GetNextImage());
            }
            else
            {
                MessageBlock.Text = "No image found in the Pictures Library";
            }
 
        }
 
        /// <summary>
        /// Affiche une image dans une vue
        /// </summary>
        /// <param name="image">vue ou l'image est affichée</param>
        /// <param name="imageFile">file image à afficher</param>
        async private void SetImageSource(Image imagePlayer, StorageFile imageFile)
        {
            // ouvert de l'image dans un object bitmap
            IRandomAccessStream imageStream = await imageFile.OpenAsync(FileAccessMode.Read);
 
            BitmapImage imageBitmap = new BitmapImage();
            imageBitmap.SetSource(imageStream);
 
            // Set l'image au player
            imagePlayer.Source = imageBitmap;
        }
 
        /// <summary>
        /// Récupère l'image suivante
        /// </summary>
        /// <returns></returns>
        private StorageFile GetNextImage()
        {
            this.imageIdx++;
            if (this.imageIdx >= this.imageList.Count)
            {
                this.imageIdx = 0;
            }
 
            return imageList[this.imageIdx];
        }
 
        private void ShowImage(Image imagePlayer)
        {
            this.currentPlayer = imagePlayer;
            Image otherImagePlayer = (imagePlayer.Name == "ImagePlayer1") ? this.ImagePlayer2 : this.ImagePlayer1;
 
            // Met en cache la prochaine image
            SetImageSource(otherImagePlayer, GetNextImage());
 
            if (this.isStreaming)
            {
                try
                {
                    otherImagePlayer.PlayToSource.PlayNext();
                }
                catch (Exception )
                {
                }
            }
            else
            {
                otherImagePlayer.Visibility = Visibility.Collapsed;
                imagePlayer.Visibility = Visibility.Visible;
            }
 
            this.currentPlayer = otherImagePlayer;
            this.timer.Start();
 
        }
 
        /// <summary>
        /// Pass image source to play to
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async private void SourceRequested(PlayToManager sender, PlayToSourceRequestedEventArgs e)
        {
            e.SourceRequest.SetSource(currentPlayer.PlayToSource);
 
            // Set Play To Source next properties
            ImagePlayer1.PlayToSource.Next = ImagePlayer2.PlayToSource;
            ImagePlayer2.PlayToSource.Next = ImagePlayer1.PlayToSource;
 
            await messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                // Stop slide show
                timer.Stop();
            });
        }
 
 
        /// <summary>
        /// Set up Streamin if device selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async private void SourceSelected(PlayToManager sender, PlayToSourceSelectedEventArgs e)
        {
            WriteMessageText("Streaming...");
            this.isStreaming = true;
            await this.messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    //hide local image players
                    this.ImagePlayer1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    this.ImagePlayer2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
 
                    // start slide show again with the next image
                    timer.Start();
                });
        }
 
        async private void WriteMessageText(String message, bool isOverwrite = false)
        {
            await messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    if (isOverwrite)
                    {
                        this.MessageBlock.Text = message;
                    }
                    else
                    {
                        this.MessageBlock.Text += message;
                    }
                });
        }
 
        private void TimeoutElapsed(Object sender, Object e)
        {
            this.ShowImage(currentPlayer);
        }
    }
}
et le XAML
Code xaml :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<Page
    x:Class="SlideShow.MainPage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SlideShow"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
 
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
            <TextBlock x:Name="MessageBlock" Width="300"/>
        </StackPanel>
        <StackPanel>
            <Image x:Name="ImagePlayer1" Height="400" Width="600"/>
            <Image x:Name="ImagePlayer2" Height="400" Width="600"/>
        </StackPanel>
    </Grid>
</Page>
yupa est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/06/2012, 16h03   #2
Samuel Blanchard
Rédacteur/Modérateur

 
Avatar de Samuel Blanchard
 
Homme Samuel Blanchard
Expert .NET
Inscription : février 2010
Messages : 1 499
Détails du profil
Informations personnelles :
Nom : Homme Samuel Blanchard
Âge : 39
Localisation : France

Informations professionnelles :
Activité : Expert .NET

Informations forums :
Inscription : février 2010
Messages : 1 499
Points : 2 469
Points : 2 469
Juste pour info, regarde du coté du Timer (il semblerait que c'est lui qui effectue le changmeent d'image).
__________________
.
Pas de question technique en MP, merci.
.
Un emulator Gameboy Color pour Windows Phone ?
c'est moi qui l'ai fait
Samuel Blanchard est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/06/2012, 14h33   #3
yupa
Membre du Club
 
Inscription : mai 2005
Messages : 192
Détails du profil
Informations forums :
Inscription : mai 2005
Messages : 192
Points : 44
Points : 44
Oui effectivement, le code n'était pas bon au niveau du timer. J'ai réussi à le faire marcher.
Merci
yupa est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 00h27.


 
 
 
 
Partenaires

Hébergement Web