1

如何获得时间间隔为 2 秒的图像幻灯片。我已经从stackoverflow引用了以下代码,但是在运行iam nt时显示任何图像..请致电iam哪里出错了...

xml:

<image Name=myImg Source={Binding bi}/>

代码:

private DispatcherTimer tmr = new DispatcherTimer();

private List<string> images = new List<string>();

private int imageIndex = 0;

public MainPage()
{
    InitializeComponent();

    Loaded += new RoutedEventHandler(MainPage_Loaded);
}

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

    LoadImages();

    ShowNextImage();
}

private void LoadImages()
{
    // list the files (includede in the XAP file) here
    images.Add("/images/filename1.jpg");
    images.Add("/images/filename2.jpg");
    images.Add("/images/filename3.jpg");
    images.Add("/images/filename4.jpg");
}

private void ShowNextImage()
{
    Imagesource bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));

    myImg.Source = bi;

    imageIndex = (imageIndex + 1) % images.Count;
}

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

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

0

更改Name=myImgx:Name="myImg"并完全删除该Source属性。否则,它看起来应该可以工作。

于 2013-01-24T02:30:24.000 回答