I have a simple ViewModel:
public class MeetingPageViewModel : ReactiveObject, IRoutableViewModel
{
public MeetingPageViewModel(IScreen hs, IMeetingRef mRef)
{
HostScreen = hs;
_backing = "hi there";
}
public IScreen HostScreen { get; private set; }
public string MeetingTitle
{
get { return _backing; }
set { this.RaiseAndSetIfChanged(ref _backing, value); }
}
string _backing;
public string UrlPathSegment
{
get { return "/meeting"; }
}
}
And I bind to the MeetingTitle a TextBlock:
public sealed partial class MeetingPage : Page, IViewFor<MeetingPageViewModel>
{
public MeetingPage()
{
this.InitializeComponent();
// Bind everything together we need.
this.OneWayBind(ViewModel, x => x.MeetingTitle, y => y.MeetingTitle.Text);
}
/// <summary>
/// Stash the view model
/// </summary>
public MeetingPageViewModel ViewModel
{
get { return (MeetingPageViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModel", typeof(MeetingPageViewModel), typeof(MeetingPage), new PropertyMetadata(null));
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (MeetingPageViewModel)value; }
}
}
After navigating back to the previous screen MeetingPageViewModel isn't garbage collected. I'm using 6.4.1 of RxUI and VS2013 (and the memory analysis analyze tool). If I dispose of the return value from OneWayBind then everything is cleaned up properly - but of course, I no longer have the bindings.
It turns out the problem is the DependencyProperty ViewModel. Its lifetime is forever (note the "static" in its declaration). As a result, the binding that is attached to it is never garbage collected, since it never goes away, and that binding then holds a reference to both the view and viewmodel, and so they never go away.
The only way to break this is explicitly clean up the bindings. RxUI provides the WhenActivated method to help with this. Surround the bindings in a lambda, and use the provided function to track the IDisposals. When the view goes away this will then be cleaned up.
this.WhenActivated(disposeOfMe => {
disposeOfMe (this.OneWayBind(ViewModel, x => x.MeetingTitle, y => y.MeetingTitle.Text));
});
Related
I have a simple ViewModel which has two Models. So it looks like this:
public class ConnectionItemSelectorViewModel : ViewModelBase {
...
#region AvailableConnectionsModel
// Model Nr. 1
[Model]
public ConnectionList AvailableConnectionsModel
{
get { return GetValue<ConnectionList>(AvailableConnectionsModelProperty); }
set { SetValue(AvailableConnectionsModelProperty, value); }
}
public static readonly PropertyData AvailableConnectionsModelProperty = RegisterProperty(nameof(AvailableConnectionsModel), typeof(ConnectionList), () => new ConnectionList());
#endregion
#region SelectedConnectionsModel
// Model Nr. 2
[Model]
public ConnectionList SelectedConnectionsModel
{
get { return GetValue<ConnectionList>(SelectedConnectionsModelProperty); }
set { SetValue(SelectedConnectionsModelProperty, value); }
}
public static readonly PropertyData SelectedConnectionsModelProperty = RegisterProperty(nameof(SelectedConnectionsModel), typeof(ConnectionList), () => new ConnectionList());
#endregion
...
}
ConnectionList extends ModelBase so I can use the [Model]-Attribute several times.
Now I want to expose the properties of the Model to the ViewModel:
public class ConnectionItemSelectorViewModel : ViewModelBase {
...
// init Model properties
#region AvailableConnections
// Using a unique name for the property in the ViewModel
// but linking to the "correct" property in the Model by its name
[ViewModelToModel(nameof(AvailableConnectionsModel), nameof(ConnectionList.Connections))]
public ObservableCollection<ConnectionItem> AvailableConnections
{
get { return GetValue<ObservableCollection<ConnectionItem>>(AvailableConnectionsProperty); }
set { SetValue(AvailableConnectionsProperty, value); }
}
public static readonly PropertyData AvailableConnectionsProperty = RegisterProperty(nameof(AvailableConnections), typeof(ObservableCollection<ConnectionItem>), () => null);
#endregion
// linking other properties to the models
...
}
The problem is that the linking doesn't work. So after initialization the property AvailableConnections (and the others also) are still null, though the Model itself is initialized correctly.
Am I missing something or isn't this possible at all?
thx in advance!
Try setting the MappingType on the ViewModelToModel attribute so that the model wins.
I create (if not exists) a new ViewModel Instance via the IMessenger (MVVM Light Toolkit) and pass a custom Object trough the constructor, which is a Property of my MainViewModel.
In this ViewModel I set it to a Property too and using it e.g. for a Command Execute Method. But when I trigger the Command via Binding, the custom Object Property looses its values.
I already debugged and saw, that the object get's passed correctly with its values, but after initializing from the constructor its empty (Not null, just empty Properties).
My custom Object
public class CustomObject : ObservableObject
{
public int Id { get; set; }
public string Property1 { get; set; }
public string Property2 { get; set; }
// etc...
}
I create the ViewModel like this
public CustomObject CustomObj
{
get { return _customObj; }
set { Set(ref _customObj, value); }
}
_customViewModel = new CustomViewModel(CustomObj, _dataService);
The ViewModel
public CustomViewModel(CustomObject obj, IDataService dataService)
{
_dataService = dataService;
// Here it sets correctly the Object
CustomObj = obj;
}
public CustomObject CustomObj
{
get { return _customObj; }
set { Set(ref _customObj, value); }
}
// Even before the Command is triggered, the Object is already empty
public ICommand SomeCommand => new RelayCommand<string>(async s =>
{
var someThing = await _dataService.GetSomeData(CustomObj.Id);
// Stuff...
}
They are registered in the SimpleIoC Container as well, if that matters.
What could result that?
I have a View where I bind some deep properties of a Model (using the naming convention of Caliburn.Micro):
View:
<UserControl x:Class="TOP.SomeView"
(...)
<TextBox x:Name="NewFooModel_Foo" .../>
Then I need to catch the firing of the INPC of that property in the ViewModel:
Model:
public class FooModel{
private string _foo;
(...)
public int Foo {
get { return _foo; }
set {
if (_foo != value) {
_foo = value;
NotifyOfPropertyChange(() => Foo);
}
}
}
}
From that point, the property of the model is binded correctly. So, I need that change to be notified to the CanCreateFoo and I don't know how:
ViewModel:
public class SomeViewModel{
(...)
public FooModel NewFooModel {
get { return _newFooModel; }
set {
if (_newFooModel != value) {
_newFooModel = value;
NotifyOfPropertyChange(() => Foo);
//HERE I NEED TO NOTIFY TO CANCREATEFOOMODEL THAT A PROPERTY OF THE MODEL IS CHANGED
}
}
}
public bool CanCreateFooModel{
get{
return NewFooModel.Foo != null;
}
}
}
Please, can someone help me? Thanks in advance.
You could use EventAggregator to publish a message when the Property changes (and also NotifyOfPropertyChange for your current VM).
Each Model which is interested can subscribe to this message and handle it.
I'm currently working on a solution that has a set of composite ViewModels that are mapped from domain models coming back from a set of data access services.
So far I've had a good amount of success with implementing INotifyPropertyChanged on the base ViewModel object and notifying the UI of changes to the property objects via property changed events.
Here's an example of a view model:
public class DisplayDataModel : INotifyPropertyChanged{
private DateTime _lastRefreshTime;
public DateTime LastRefreshTime {
get { return _lastRefreshTime; }
set {
_lastRefreshTime = value;
this.NotifyPropertyChanged(lddm => lddm.LastRefreshTime, PropertyChanged);
}
}
private string _lineStatus;
public string LineStatus {
get { return _lineStatus; }
set {
if (_lineStatus != value) {
_lineStatus = value;
this.NotifyPropertyChanged(lddm => lddm.LineStatus, PropertyChanged);
}
}
}
private ProductionBrickModel _productionBrick;
public ProductionBrickModel ProductionBrick {
get { return _productionBrick;}
set {
if (_productionBrick != value) {
_productionBrick = value;
this.NotifyPropertyChanged(lddm => lddm.ProductionBrick, PropertyChanged);
}
}
}
}
public class ProductionBrickModel{
public int? Set { get; set; }
public int? Theoretical { get; set; }
public int? Actual { get; set; }
public string LineName { get; set; }
public TimeSpan? ShiftOverage { get; set; }
public SolidColorBrush ShiftOverageBrush {
get {
if (ShiftOverage.HasValue && ShiftOverage.Value.Milliseconds < 0) {
return Application.Current.FindResource("IndicatorRedBrush") as SolidColorBrush;
}
return Application.Current.FindResource("IndicatorWhiteBrush") as SolidColorBrush;
}
}
public string ShiftOverageString { get { return ShiftOverage.HasValue ? ShiftOverage.Value.ToShortTimeSpanString() : ""; } }
}
So currently I'm firing notification events on the base model and not the production brick property, mostly because the production brick properties will be changing almost every refresh anyways.
Recently I've started cranking refresh times down to around 350ms and I'm seeing situations where the ShiftOverageBrush is changing to white for a split second even though the values are still negative.
My question is by going through and implementing INotifyPropertyChanged on the object types that make up the base view model will I gain any performance, or even possibly solve this issue? Or is this coming from something else entirely that I'm not understanding?
There are two obvious sources of inefficieny in your code:
1) ShiftOverageBrush is using FindResource every time it's called. Why not cache the brushes?
private SolidColorBrush _redBrush;
private SolidColorBrush IndicatorRedBrush
{
get{ return _redBrush ?? (_redBrush =
Application.Current.FindResource("IndicatorRedBrush") as SolidColorBrush));
}
... same for white brush
public SolidColorBrush ShiftOverageBrush {
get {
if (ShiftOverage.HasValue && ShiftOverage.Value.Milliseconds < 0) {
return IndicatorRedBrush;
}
return IndicatorWhiteBrush;
}
}
2) Using a lambda expression for NotifyPropertyChanged is convenient but is pretty slow since it uses reflection. If you're cranking up the update rate, then replace the lambdas with strings.
As is well known, CM doesn't support passing a object of complex type through NavigationService like MVVM Light. So I searched for a workaround and did it like this.
There are two viewmodels: MainPageViewModel and SubPageViewModel.
I first defined 3 classes, namely GlobalData, SnapshotCache and StockSnapshot. StockSnapshot is the type of which the object I want to pass between the 2 viewmodels.
public class SnapshotCache : Dictionary<string, StockSnapshot>
{
public StockSnapshot GetFromCache(string key)
{
if (ContainsKey(key))
return this[key];
return null;
}
}
public class GlobalData
{
private GlobalData()
{
}
private static GlobalData _current;
public static GlobalData Current
{
get
{
if (_current == null)
_current = new GlobalData();
return _current;
}
set { _current = value; }
}
private SnapshotCache _cachedStops;
public SnapshotCache Snapshots
{
get
{
if (_cachedStops == null)
_cachedStops = new SnapshotCache();
return _cachedStops;
}
}
}
public class StockSnapshot
{
public string Symbol { get; set; }
public string Message { get; set; }
}
Next, I call the navigation service on MainPageViewModel like this:
StockSnapshot snap = new StockSnapshot {Symbol="1", Message = "The SampleText is here again!" };
GlobalData.Current.Snapshots[snap.Symbol] = snap;
NavigationService.UriFor<SubPageViewModel>().WithParam(p=>p.Symbol,snap.Symbol).Navigate();
And on SubPageViewModel I've got this:
private string _symbol;
public string Symbol
{
get { return _symbol; }
set
{
_symbol = value;
NotifyOfPropertyChange(() => Symbol);
}
}
public StockSnapshot Snapshot
{
get { return GlobalData.Current.Snapshots[Symbol]; }
}
And that's where the problem lies. When I run the program, I find out that it always runs to the getter of Snapshot first, when Symbol hasn't been initialized yet. So later I've tried adding some extra code to eliminate the ArgumentNullException so that it can run to the setter of Symbol and then everything goes fine except that the UI doesn't get updated anyway.
Could anyone tell me where I've got wrong?
Thx in advance!!
Why not just use:
private string _symbol;
public string Symbol
{
get { return _symbol;}
set
{
_symbol = value;
NotifyOfPropertyChange(() => Symbol);
NotifyOfPropertyChange(() => Snapshot);
}
}
public StockSnapshot Snapshot
{
get { return Symbol!=null? GlobalData.Current.Snapshots[Symbol]:null; }
}
In this case you don't try and get the data from GlobalData when Symbol is null (sensible approach anyway!) and when "Symbol" is set you call NotifyOfPropertyChange() on Snapshot to force a re-get of the property.