views:

30

answers:

2

Hello everybody,

I'm working on a application which is communicating by network to monitor another application's variables.

The remote application has loads of variables, and I want to transfer only the variables that I'm currently watching on my user interface, to avoid overloading the network.

I try to keep the architecture of my application as clean as possible, with a model that doesn't know about the view, using bindings, etc.

I thought about refreshing my model data values only when the bindings are active (which mean that the usercontrol displaying some of the variables is shown), do you think it's a good solution? Otherwise, I could also work with the property "IsVisible" of each usercontrol... but I think it would be better to work on the model side of my application I think.

Do you know if there is a way to know if a binding is active or not?

If my question isn't clear enough, I could draw a small schema. Just tell me.

Thank you for your answers,

+2  A: 

Remember that binding is only refreshed when a change is posted via INotifyPropertyChanged (or in the case of a two-way binding your UI modifies a two-way binded variable). Most of the time, you will explicitly query a service. When the service response comes back, you will modify properties in your ViewModel and call the PropertyChanged event to tell the bindings to update themselves.

Essentially, your binding isn't going to update unless you tell it to. ObservableCollections will automatically update the binding because they internally implement INotifyPropertyChanged. But still, that only happens when you update the collection.

To answer your question, yes, keep network chatter down by only updating when you absolutely need to. Since you are using WPF and INotifyPropertyChanged, this will only happen when you invoke an update to a bound variable + fire the event.

j0rd4n
Hi Jordan, Thank you for your answer, but I'm afraid I haven't understood it very well.The thing is, I have a communication task which is clocked by a timer, let's say every 100ms or every 10ms, and which should only update the variables actually displayed. That's why I'm looking for a way to know which variable are displayed, from the model side... I was then thinking to detect which binding was active (as I know bindings are only active when the related UI elements are displayed).
Antoine Jeanrichard
I see what you mean. Are you views sharing the same model or are they broken out into one viewmodel per view?
j0rd4n
Right now my views are sharing the same model. But I'm open to change the architecture if I really need to, because I try to make it reusable, so it should be the best it can ;-)
Antoine Jeanrichard
My recommendation is to have a separate ViewModel for each View. That is going to make your life tons easier. Then only the ViewModel that is active will be updated. You can have your polling model be shared between all your view models, but this way, only the view model that needs the data will make the request.
j0rd4n
Okay, that could be a good idea :-) But...Maybe I don't know the MVVM Pattern well enough, but how can I know that a viewModel is active?Thank you very much for your help
Antoine Jeanrichard
It depends on how your UI layout is setup. For instance, are you using tab pages? If that is the case, you could check the visibility of the tab to know if that view is active. You might have to write some thin "glue code" that communicates to the ViewModel whether or not a View is active. Tab pages are easy to do this with. You can even two-way bind the visibility property to your ViewModel so the model knows the state from the UI.
j0rd4n
Hi Jordan, Thank you for your answers, a binding to the visibility property is a good idea I think :-)I'll work on that.
Antoine Jeanrichard
Sure, hope that helps some.
j0rd4n
A: 

If a binding is "active" (I assume you mean that there are dependency properties interested in the property) they will call the getter for the property on notifypropertychanged for the particular property.

So, as long as you're not firing NotifyPropertyChanged superfluously, every time the get of your property is being called, you are safe to do whatever data refreshing applies to that property.

marr75