Click here to Skip to main content
15,867,835 members
Home / Discussions / WPF
   

WPF

 
QuestionQuestion about NotifyCollectionChangedEventArgs Pin
Super Lloyd1-Mar-22 12:12
Super Lloyd1-Mar-22 12:12 
AnswerRe: Question about NotifyCollectionChangedEventArgs Pin
Richard Deeming1-Mar-22 21:42
mveRichard Deeming1-Mar-22 21:42 
QuestionAnimation on DataGridRow jumps to wrong row when scrolling Pin
Mc_Topaz27-Feb-22 23:31
Mc_Topaz27-Feb-22 23:31 
AnswerRe: Animation on DataGridRow jumps to wrong row when scrolling Pin
Gerry Schmitz28-Feb-22 8:11
mveGerry Schmitz28-Feb-22 8:11 
GeneralRe: Animation on DataGridRow jumps to wrong row when scrolling Pin
Mc_Topaz28-Feb-22 20:28
Mc_Topaz28-Feb-22 20:28 
QuestionLabelprint on Thermotransfer Pin
Member 118002289-Feb-22 0:34
Member 118002289-Feb-22 0:34 
QuestionRe: Labelprint on Thermotransfer Pin
Eddy Vluggen9-Feb-22 3:13
professionalEddy Vluggen9-Feb-22 3:13 
QuestionWPF c# onStartUp() view model problem with multiple data inputs Pin
StealthRT5-Feb-22 7:31
StealthRT5-Feb-22 7:31 
Hey all I have the following OnStartup for my WPF desktop app that goes to a local directory and gathers all the images within that folder (box1 being the example code below):

<pre lang="C#">public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // set the update interval
            var imageSource = new ImageSource(Path.Combine(@"C:\photos\boxes\", "box1"), TimeSpan.FromHours(1));
            var viewModel = new MainWindowViewModel(imageSource);

            var window = new MainWindow()
            {
                DataContext = viewModel
            };

            window.Closed += (s, a) => { viewModel.Dispose(); };
            window.Show();
        }
    }


This works just fine for the component I have on the MainWindow.xaml that's label box1 but the other boxes 2-9 do not load their own images from their respective folders - they all show the same images as box1 has.

The structure of the directory is this:

C:\
 |-photos\
    |boxes\
      |box1
      |box2
      |box3
      |box4
      |box5
      |box6
      |box7
      |box8
      |box9
      |box10


On the MainWindow I have this code that allows all those photos from each directory into its own element:
XML
<Window.Resources>
        <!-- List of supported animations -->
        <FluidKit:SlideTransition x:Key="SlideTransition" x:Shared="False"/>
        <FluidKit:CubeTransition x:Key="CubeTransition" Rotation="BottomToTop" x:Shared="False"/>
        <FluidKit:FlipTransition x:Key="FlipTransition" x:Shared="False"/>
        <local:ImageSourceConverter x:Key="ImageSourceConverter"/>
        <!-- Data template for animations -->
        <DataTemplate x:Key="ItemTemplate" x:Shared="False">
            <Image Source="{Binding Path, Converter={StaticResource ImageSourceConverter}}"
                   Stretch="Fill"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <FluidKit:TransitionPresenter RestDuration="0:0:3"
                                          IsLooped="True"
                                        Transition="{StaticResource FlipTransition}"
                                       ItemsSource="{Binding box1}"
                                             Width="357"
                                            Height="272"
                               HorizontalAlignment="Left"
                                 VerticalAlignment="Top"
                                      ItemTemplate="{StaticResource ItemTemplate}"
                                            x:Name="box1"
                                            Margin="0,0,0,454"/>
        <FluidKit:TransitionPresenter RestDuration="0:0:3"
                                          IsLooped="True"
                                        Transition="{StaticResource FlipTransition}"
                                       ItemsSource="{Binding box2}"
                                             Width="357"
                                            Height="272"
                               HorizontalAlignment="Left"
                                 VerticalAlignment="Top"
                                      ItemTemplate="{StaticResource ItemTemplate}"
                                            x:Name="box2"
                                            Margin="357,0,0,0"/>
       ETC.......

Like I said above, this first element ItemsSource="{Binding box1} loads the images as they should be from the box1 directory just fine but all the other 2-9 after that are loaded the same images as box1.

The mainWindowViewModel code looks like this:
C#
public class MainWindowViewModel : IDisposable
{
    private readonly IDisposable _token1;
    private readonly IDisposable _token2;
    ...ETC

    public MainWindowViewModel(IImageSource imageSource)
    {
        // subscribe to update images at regular intervals
        _token1 = imageSource
            .GetImages().ObserveOn(DispatcherScheduler.Current)
            .Subscribe(i => UpdateImages(i, box1), ex => ShowError(ex));

        _token2 = imageSource
            .GetImages().ObserveOn(DispatcherScheduler.Current)
            .Subscribe(i => UpdateImages(i, box2), ex => ShowError(ex));
        ETC...
    }

    private void ShowError(Exception ex)
    {
        MessageBox.Show(ex.Message, "Photo Gallery", MessageBoxButton.OK, MessageBoxImage.Error);
    }

    /// <summary>
    /// Updates the animation's images.
    /// </summary>
    /// <param name="images">The images to update with</param>
    /// <param name="animation">The animation to update</param>
    private void UpdateImages(IEnumerable<string> images, ObservableCollection<ImageViewModel> animation)
    {
        animation.Clear();

        foreach (var i in images)
        {
            animation.Add(new ImageViewModel { Path = i });
        }
    }

    /// <summary>
    /// Gets or sets a collection of images used for the first animation.
    /// </summary>
    public ObservableCollection<ImageViewModel> box1 { get; set; } =
        new ObservableCollection<ImageViewModel>();

    public ObservableCollection<ImageViewModel> box2 { get; set; } =
        new ObservableCollection<ImageViewModel>();

    ETC...

    public void Dispose()
    {
        _token1.Dispose();
        _token2.Dispose();
        ETC...
    }
}

The function that loops to get each file image within the directory is this:
C#
public class ImageSource : IImageSource
{
    private readonly string _path;
    private readonly TimeSpan _interval;

    public ImageSource(string path, TimeSpan interval)
    {
        _path = path;
        _interval = interval;
    }

    public IObservable<IEnumerable<string>> GetImages()
    {
        if (!Directory.Exists(_path))
        {
            return Observable.Empty<IEnumerable<string>>();
        }

        return Observable.Create<IEnumerable<string>>(observer =>
        {
            return TaskPoolScheduler.Default.ScheduleAsync(async (ctrl, ct) =>
            {
                for (;;)
                {
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }

                    try
                    {
                        var images = Directory.GetFiles(_path, "*.jpg");

                        // Don’t do anything unless there are a minimum of 10 images.
                        if (images.Count() > 9)
                        {
                            observer.OnNext(images.PickRandom());
                        }
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        throw;
                    }

                    await ctrl.Sleep(_interval).ConfigureAwait(false);
                 }
            });
        });
    }

The code currently goes to each of the ObservableCollection<imageviewmodel> box[X] { get; set; } and sets the path to each image within that folder. Box2-10 are the same files as box 1 of course.

How can I modify that onStartup() code to allow for it to consume each box folders' files and place them into the appropriate box # component instead of just using that box1 files?

Thanks!
AnswerRe: WPF c# onStartUp() view model problem with multiple data inputs Pin
Gerry Schmitz5-Feb-22 18:16
mveGerry Schmitz5-Feb-22 18:16 
AnswerRe: WPF c# onStartUp() view model problem with multiple data inputs Pin
Kevin Marois21-Feb-22 11:29
professionalKevin Marois21-Feb-22 11:29 
QuestionWpf System.Windows.Media.ImageSourcesConverter cannot be applied Pin
StealthRT1-Feb-22 16:08
StealthRT1-Feb-22 16:08 
AnswerRe: Wpf System.Windows.Media.ImageSourcesConverter cannot be applied Pin
Richard Deeming1-Feb-22 21:49
mveRichard Deeming1-Feb-22 21:49 
GeneralRe: Wpf System.Windows.Media.ImageSourcesConverter cannot be applied Pin
StealthRT2-Feb-22 2:39
StealthRT2-Feb-22 2:39 
GeneralRe: Wpf System.Windows.Media.ImageSourcesConverter cannot be applied Pin
Richard Deeming2-Feb-22 2:44
mveRichard Deeming2-Feb-22 2:44 
Question(beginner) There must be a better way (TextBlock properties) Pin
Maximilien26-Jan-22 5:08
Maximilien26-Jan-22 5:08 
AnswerRe: (beginner) There must be a better way (TextBlock properties) Pin
Richard Deeming26-Jan-22 5:33
mveRichard Deeming26-Jan-22 5:33 
GeneralRe: (beginner) There must be a better way (TextBlock properties) Pin
Maximilien26-Jan-22 9:10
Maximilien26-Jan-22 9:10 
QuestionConverting a byte[] to a ImageSource means huge memory leak Pin
Starwer24-Jan-22 11:42
Starwer24-Jan-22 11:42 
AnswerRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Gerry Schmitz24-Jan-22 16:15
mveGerry Schmitz24-Jan-22 16:15 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer24-Jan-22 19:58
Starwer24-Jan-22 19:58 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Gerry Schmitz24-Jan-22 21:26
mveGerry Schmitz24-Jan-22 21:26 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer25-Jan-22 7:56
Starwer25-Jan-22 7:56 
AnswerRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Richard Deeming25-Jan-22 21:51
mveRichard Deeming25-Jan-22 21:51 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer26-Jan-22 9:10
Starwer26-Jan-22 9:10 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Gerry Schmitz27-Jan-22 6:33
mveGerry Schmitz27-Jan-22 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.