1

画像のスライドショーを作成したという点で、1つのWindows Phone 7アプリを作成しています。画像は時間間隔で変化しますが、スライド効果を与えたいのは私のコードです:

xaml コード:

<Grid Background="Red" HorizontalAlignment="Left" Height="242" 
                Margin="10,12,0,0"  
                VerticalAlignment="Top" Width="458">      
    <Image x:Name="myImg" Stretch="UniformToFill" Margin="0,0,0,28"/>
</Grid>

.cs コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Windows.Navigation;
namespace test
{
    public partial class MainPage : PhoneApplicationPage
    {
        private DispatcherTimer tmr = new DispatcherTimer();
        private List<string> images = new List<string>();
        private List<string> texts = new List<string>();
              private int imageIndex = 0;
        private int textIndex = 0;

        public MainPage()
        {

            InitializeComponent();


            Loaded += new RoutedEventHandler(MainPage_Loaded);
            //FirstStoryBoard.Begin();
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            tmr.Interval = TimeSpan.FromSeconds(2);
            tmr.Tick += new EventHandler(tmr_Tick);

            LoadImages();
             LoadText();


            ShowNextImage();
        }

        private void LoadImages()
        {
            images.Add("/img/11.jpg");
            images.Add("/img/ee.jpg");
            images.Add("/img/images.jpg");
            images.Add("/img/20130820080826_F2.jpg");
        }
        private void LoadText()
        {
            texts.Add("image 1");
            texts.Add("image2");
            texts.Add("image3");
            texts.Add("image3");
        }
        private void ShowNextImage()
        {
            var bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));

            myImg.Source = bi;

            imageIndex = (imageIndex + 1) % images.Count;
        }
        private void ShowNextText()
        {
            var bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
            //var ti = new BitmapSource(new Uri(texts[textIndex], UriKind.Relative));
            var ti = texts[textIndex];
            mytext.Text = ti;
            //mytext.Source = ti;
             textIndex = (textIndex + 1) % texts.Count;
        }


        void tmr_Tick(object sender, EventArgs e)
        {
            ShowNextImage();
            ShowNextText();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!tmr.IsEnabled)
            {
                tmr.Start();
            }

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            tmr.Stop();

            base.OnNavigatedFrom(e);
        }
4

1 に答える 1