且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在windows phone 7应用程序开发中动态显示图像?

更新时间:2022-01-10 23:42:04

我知道这是一个很老的问题,但我有几分钟的空闲时间 ;)

每四次点击屏幕,下面将显示与设备上存储的图像不同的随机图像.

The following will display a different random image from the images stored on the device every fourth time the screen is tapped.

XAML:

xmlns:Controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Grid.Background>
        <ImageBrush x:Name="myImg" />
    </Grid.Background>
    <Controls:GestureService.GestureListener>
        <Controls:GestureListener Tap="GestureListener_Tap" />
    </Controls:GestureService.GestureListener>
</Grid>

C#

using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;

private int tapCount = 0;

private void GestureListener_Tap(object sender, GestureEventArgs e)
{
    tapCount += 1;

    if (tapCount % 4 == 0)
    {
        SetRandomImage();
    }
}

private void SetRandomImage()
{
    var lib = new MediaLibrary();

    using (var pic = lib.Pictures[new Random().Next(0, lib.Pictures.Count - 1)])
    {
        var img = new BitmapImage();
        img.SetSource(pic.GetImage());

        myImg.ImageSource = img;
    }
}