I'm new to WPF and MVVM (PRISM) (Java is my speciality), however I have created a web service that interacts with my database, with all web service calls handled by my ViewModel Class.
So far, I have attempted a number of possible solutions, which are as follows:
- Observable collections
- Implementing INotifyPropertyChanged
- Implementing OnPropertyChanged
In spite of my efforts, none of these solutions worked for me.
One solution that I did manage to figure out, albeit unsatisfactorily involved creating a TimerDispacher object to make all the web service calls in the ViewModel at an interval of once per second, but I'm sure this is not the best way to do it.
It may even be the worst, because I want the interface to respond as the database changes.
Here is a sample code of the logic in my ViewModel class and please tell me where I went wrong:
public class MyViewModel : ViewModelBase {
private ObservableCollection<SomeClass> _listeData;
public ObservableCollection<SomeClass> ListeData {
set {
_listeData= value;
OnPropertyChanged("ListeData");
}
get { return _listeData; }
}
private string _textData;
public string TextData {
get { return _textData; }
set {
_textData = value;
OnPropertyChanged("TextData");
}
}
//Ctor
protected internal MyViewModel ( ) {
InitData();
SetTimer();
}
public void InitData() {
GetTextData();
GetListeData();
}
protected void GetTextData(string param) {
using (var proxy = new WCFServiceClient()) {
TextData = proxy.GetTextData(param);
}
}
protected void GetListeData(string param) {
using (var proxy = new WCFServiceClient()) {
ListeData = new ObservableCollection<SomeClass> (proxy.GetListeData(param));
}
}
protected void dispatcherTimer_Tick(object sender, EventArgs e) {
InitData();
}
private void SetTimer() {
var dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
}
Note: I want my code to make most of use of WPF functionalities and not having to rely on timer based solutions.