实现显示下一图像的方法

时间:2014-05-21 09:31:07

标签: c# wpf

我希望制作一个从图像文件夹中更改图像的方法。 这是代码:

private void ShowNexImage()
    {
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        string filename = ((ctr < 10) ? "images/Plane0" + ctr + ".jpeg" : "images/Plane" + ctr + ".jpeg");
        image.UriSource = new Uri(filename, UriKind.Relative);
        image.EndInit();

    }

我如何修改所有内容,运行corectly我的应用程序? 也许需要修改这行代码

string filename = ((ctr < 10) ? "images/Plane0" + ctr + ".jpeg" : "images/Plane" + ctr + ".jpeg");

我发布了所有代码,以便更好地理解。

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        // Clear out placeholder content
        this.wrapPanel.Children.Clear();

        try
        {
            var files = Directory.GetFiles(ImagesDir);

            int i = 0;
            foreach (var file in files)
            {
                FileInfo fileInfo = new FileInfo(file);

                var imageUri = new Uri(file, UriKind.Relative);
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = imageUri;
                bi.EndInit();

                BaseWineModel baseModel = null;
                string label = System.IO.Path.GetFileNameWithoutExtension(file);

                if (fileInfo.Extension.EndsWith("jpg") ||
                    fileInfo.Extension.EndsWith("jpeg") ||
                    fileInfo.Extension.EndsWith("png") ||
                    fileInfo.Extension.EndsWith("gif"))
                {
                    if (label.StartsWith("Group_"))
                    {
                        baseModel = new WineGroupModel();
                        baseModel.Image = imageUri;
                        label = label.Substring(6);
                        (baseModel as WineGroupModel).WinesDir = label;
                    }
                    else
                    {
                        baseModel = new WineModel();
                        baseModel.Image = imageUri;
                        var descFile = System.IO.Path.ChangeExtension(file, "txt");
                        if (File.Exists(descFile))

                        {
                            (baseModel as WineModel).Description = File.ReadAllText(descFile);
                        }
                        (baseModel as WineModel).Price = new Random().NextDouble();                           
                    }

                    var button = new KinectTileButton
                    {
                        Label = label,
                        Background = new ImageBrush(bi),
                        Tag = baseModel
                    };

                    this.wrapPanel.Children.Add(button);
                }
            }
        }


public static readonly DependencyProperty ImagesDirProp = DependencyProperty.Register
         (
              "ImagesDir",
              typeof(string),
              typeof(ImagesGrid),
              new PropertyMetadata(string.Empty)
         );

    public string ImagesDir
    {
        get { return (string)GetValue(ImagesDirProp); }
        set { SetValue(ImagesDirProp, value); }
    }

1 个答案:

答案 0 :(得分:0)

Noooooo。不要这样做。让WPF为你完成这项工作。在ViewModel中创建一个表示当前查看图像的属性,该ViewModel实现了&#34; INotifyPropertyChanged&#34;接口

public partial class MainWindow : Window
{
    private MyMainViewModel VM {get;set;}

    public MainWindow()
    {
        InitializeComponent();
        VM = new MyMainViewModel()
        this.Content = VM;
    }

    private void Btnsend_Click(object sender, RoutedEventArgs e)
    {
        VM.NextImage();
    }
}

XML将如下所示

<Window Name ="Window2" Content={Binding}>
    <Grid>
        <Image Source={Binding CurrentImage} />
    </Grid>
</Window2>

ViewModel将如下所示

public class MyMainViewModel:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ImageSource _currentImage;

    public ImageSource CurrentImage 
    {
        get
        {
            return _currentImage ;
        }
        set
        {
            _currentImage = value;
            OnPropertyChanged("CurrentImage");
        }
    }

    public void NextImage()
    {
        // here come the code that load the next image, as ever you see it fit
        //CurrentImage = ....
    }
}

如果这不构建,请不要生气。我把它写在脑后。 无论如何,....投入一些时间来研究MVVM和数据绑定到ViewModels。还有大量的教程。我会真的得到回报。它将使开发变得有趣。 ;)