WPF 获取当前图像 URL 作为字符串

问题描述 投票:0回答:1

我想获取当前图像路径并将其保存在变量中以与图像路径数组进行比较。如何将图像路径转换为字符串进行比较,然后将图像设置为数组中的下一个路径?我正在创建一个幻灯片,其中有两个箭头可以转到下一张或上一张图像。这是我目前拥有的:

public partial class frmGameDownload : Window
    {
        private List<string> images = new List<string>();

        private int imageIndex = 0;
        public frmGameDownload()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadImages();
        }

        private void LoadImages()
            {
                images.Add(@"Images/img1.png");
                images.Add(@"Images/img2.png");
                images.Add(@"Images/img3.png");
        }

        private void Play_Click(object sender, EventArgs e)
        {
            var focusedImg = imgSlide.Source;
            for (int i = 0; i < images.Length; i++)
                if (focusedImg == images[0])
                {
                Process.Start("explorer", images[i]);
                }
        }

        private void Previous_Click(object sender, EventArgs e)
        {
            var focusedImg = imgSlide.Source;
            for (int i=0; i < images.Length; i++)
            {
                if (focusedImg = images[i])
                {
                    i -= 1;
                    var newImg = images[i];
                    imgSlide.Source = new BitmapImage(new Uri(images[i], UriKind.Relative));
                }
            }
        }
<Window x:Class="nfsExe.frmGameDownload"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:nfsExe"
        mc:Ignorable="d"
        Title="frmGameDownload" Height="450" Width="800">
    <Grid Background="Black">
        <Image x:Name="imgSlide" HorizontalAlignment="Left" Height="377" Margin="100,21,0,0" VerticalAlignment="Top" Width="600" Source="Images/tnfs.png"/>
        <Button x:Name="Previous" Content="6" HorizontalAlignment="Left" Height="40" Margin="18,180,0,0" VerticalAlignment="Top" Width="40" FontFamily="Marlett" FontSize="30" RenderTransformOrigin="0.5,0.5" BorderBrush="Black" Foreground="White" Background="Black" Click="Previous_Click">
            <Button.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Button.RenderTransform>
        </Button>
        <Button x:Name="Next" Content="6" HorizontalAlignment="Left" Height="40" Margin="734,180,0,0" VerticalAlignment="Top" Width="40" FontFamily="Marlett" FontSize="30" RenderTransformOrigin="0.5,0.5" Foreground="White" Background="Black" BorderBrush="Black" Click="Next_Click">
            <Button.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Button.RenderTransform>
        </Button>
        <Button Content="PLAY" HorizontalAlignment="Center" Height="42" Margin="304,0,303.333,36" VerticalAlignment="Bottom" Width="186" Background="Black" Foreground="White" FontFamily="Sans Serif Collection" FontSize="16" FontWeight="Bold" BorderBrush="#FF494848" VerticalContentAlignment="Center" Click="Play_Click" Opacity="0.8"/>
    </Grid>
</Window>
c# wpf xaml
1个回答
0
投票

恕我直言,它可能就像

一样简单
private List<string> _images;
private int _imageIndex = 0;

public frmGameDownload()
{
    InitializeComponent();

    _images = new List<string>
    {
        @"Images/img1.png",
        @"Images/img2.png",
        @"Images/img3.png"
    };
}
private void Play_Click(object sender, EventArgs e)
{
    Process.Start("explorer", _images[_imageIndex]);
}
private void Previous_Click(object sender, EventArgs e)
{
    _imageIndex--;

    //if index out of range, start from end again
    if (_imageIndex < 0)
        _imageIndex = _images.Count - 1;

    imgSlide.Source = new BitmapImage(new Uri(_images[_imageIndex], UriKind.Relative));
}
private void Next_Click(object sender, EventArgs e)
{
    _imageIndex++;

    //if index out of range, start from 0 again
    if (_imageIndex >= _images.Count)
        _imageIndex = 0;

    imgSlide.Source = new BitmapImage(new Uri(_images[_imageIndex], UriKind.Relative));
}
© www.soinside.com 2019 - 2024. All rights reserved.