2

there is a propery on mainwindow of my app that is updated by a function taht runs in background (DoWork). BackgroundWorker is implemented in ViewModel. If I open an new page and comme back on the mainwindow this property takes automatically its default value with which it was initialized in the ViewModel constructor. What should I do to keep this property updated even if a new window is opened?

public class ImageViewModel : INotifyPropertyChanged
{
   private string currentData;

   public ImageViewModel()
   {
        img = new ImageFile { path = "" };

        currentData = "There is currently no update";

        this.worker = new BackgroundWorker();
        this.worker.DoWork += this.DoWork;
        this.worker.ProgressChanged += this.ProgressChanged;
        this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completed);
        this.worker.WorkerReportsProgress = true;
    }


    public string CurrentData
    {
        get { return this.currentData; }
        private set
        {
            if (this.currentData != value)
            {
                this.currentData = value;
                this.RaisePropertyChanged("CurrentData");
            }
        }
    }

    ...


    private void DoWork(object sender, DoWorkEventArgs e)
    {

        ...

        this.CurrentData = "file X is being updated...";

        ...

    }


    void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
    {

         this.CurrentData = "There is currently no update...";
    }
Guilian
  • 129
  • 1
  • 11
  • you need to use `Dispatcher.CurrentDispatcher.Invoke(() => { this.CurrentData = "";});` Check out about Dispatchers here: http://stackoverflow.com/questions/1644079/change-wpf-controls-from-a-non-main-thread-using-dispatcher-invoke – Felix D. Nov 30 '16 at 12:09
  • Do all of your Windows use the same ViewModel ? – Felix D. Nov 30 '16 at 12:09
  • btw. Where are you callling `worker.RunWorkerAsync()` ? Is it missing or is it just not included in your sample ? – Felix D. Nov 30 '16 at 12:11
  • I guess you did not start your worker. Otherwise you would get some exceptions when updating a property on UI Thread from a Background thread. => See first comment ! – Felix D. Nov 30 '16 at 12:12
  • my windows use the same ViewModel but a new instance of the viewmodel is always opened when I go back to the mainwindow. How can I create a singleton class for the viewmodel? – Guilian Nov 30 '16 at 13:08
  • `public static YourViewModelType Instance {get; set;}` and then in your Window call it like `YourViewModelType.Instance = new YourViewModelType()` – Felix D. Nov 30 '16 at 14:47

1 Answers1

1

You can create a Singleton for your ViewModel like this:

Add this to your ViewModel class:

public static YourViewModelType Instance { get; set; }

In your Window.xaml.cs then assign the DataContext like this:

if(YourViewModel.Instance == null)
{
   YourViewModel.Instance = new YourViewModelType();
}
this.DataContext = YourViewModel.Instance;

Note: This will cause all your Windows to have the same DataContext. You should only do this if every Window needs the same Properties (Bindings) and stuff.

Otherwise I strongly recommend using different ViewModels per window.

Felix D.
  • 4,811
  • 8
  • 38
  • 72