my view model creates a BackgroundWorker in its constructor. BackgroundWorker updates the model properties from the DoWork event handler. The code below is a contrived example (ViewModelBase is taken almost verbatim from MVVM paper).
public class MyViewModel : ViewModelBase
{
public int MyProperty
{
get
{
return this.my_property;
}
private set
{
if (this.my_property != value)
{
this.my_property = value;
this.OnPropertyChanged("MyProperty");
}
}
}
public MyViewModel()
{
this.worker = new BackgroundWorker();
this.worker.DoWork += (s, e) => { this.MyProperty = 1; };
this.worker.RunWorkerAsync();
}
}
my view is tied to this view model:
public partial class MyPage : Page
{
public MyPage()
{
InitializeComponent();
this.DataContext = new MyViewModel();
}
}
The problem is that my application is periodically reset when my page is displayed. this definitely has something to do with workflow and data binding in XAML. it seems that if I run the worker from within the Loaded event handler for the page, the problem will disappear, but since it is difficult to play it sequentially, I'm not sure if this fix is ββcorrect.
- - , ?
EDIT: , . - InvalidOperationException, ShowDialog, . .