user3660050
user3660050

Reputation: 103

Implement a method which display next image

I wish to make a method which change images form a folder. This is the code:

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();

    }

How I need to modify all, to run corectly my app ? Maybe need to modify this line of code

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

i've post all line of codes to understand better.

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); }
    }

Upvotes: 3

Views: 206

Answers (1)

user853710
user853710

Reputation: 1767

Noooooo. Do't do that. Let WPF do the job for you. Create a Property, that will represent the currently viewed image, in a ViewModel which implements a the "INotifyPropertyChanged" interface.

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();
    }
}

The XML will look like this

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

The ViewModel will look like this

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 = ....
    }
}

If this doesn't build, please don't be angry. I'm writing it out of my head. Anyway,.... Invest some time to investigate MVVM and databinding to ViewModels. There's a ton of tutorial out ther. I will really payoff. It will trully make the development fun. ;)

Upvotes: 1

Related Questions