ViewModelBase.Set method does not accept a property/indexer as ref parameter.
Hi
When I code WPF/UWP I like to have both a core model class and a viewmodel class in respect to each entity types.
I have just started to use Template10 and I discovered that this is not supported out of the box as I am used to do it. So I would like to hear any oppinions on the "best" workaround or solution to this. The problem should be illustrated in the code below.
The code returns an "CS0206 A property or indexer may not be passed as an out or ref parameter"
public class Player
{
public string Name { get; set; }
}
public class PlayerViewModel : ViewModelBase
{
private Player model;
public string Name
{
get { return model.Name; }
set { Set(ref model.Name, value); }
}
}
Thanks in advance
This is a csharp limitation, not T10. Sorry.
Hi, you can use this:
protected bool Set<T>(T currentValue, T newValue, Action doSet,
[CallerMemberName] string property = null)
{
if (Equals(currentValue, newValue)) return false;
doSet.Invoke();
RaisePropertyChanged(property);
return true;
}
Create ViewModelBase
public abstract class ViewModelBase<M> : BindableBase where M : class, new()
{
public ViewModelBase(M thing = null)
{
This = thing == null ? new M() : thing;
}
public M This { get; private set; }
public static implicit operator M(ViewModelBase<M> thing)
{
return thing.This;
}
protected bool Set<T>(T currentValue, T newValue, Action doSet,
[CallerMemberName] string property = null)
{
if (Equals(currentValue, newValue)) return false;
doSet.Invoke();
RaisePropertyChanged(property);
return true;
}
}
and than will your code look like this:
public class Player
{
public string Name { get; set; }
}
public class PlayerViewModel : ViewModelBase<Player>
{
public string Name
{
get { return This.Name; }
Set(This.Name, value, () => This.Name = value);
}
}
Hi
I know it is a c#/language limitation and not to blame Template10 :-). But I was hoping for a nice workaround as i really want to handle both the core model class and a viewmodel class related to my entity types. I know its kind of religion if you will let the core model implement INotifyProperChanged or have a viewmodel class to implement that part, but I think that "my" pattern is commonly used by others out there also.
I will try the solution that 'ivesely' posted. That seems to be able to do the trick for me.
Hi again. Just to sum up. It seems to work like a charm. I ended up adding a class like this:
public abstract class EntityViewModelBase<TModel> :
Template10.Mvvm.BindableBase where TModel : class, new()
So the idea is now to use this special class in situations of ViewModels representing a core model class. Otherwise I just use the existing one. Could it be an idea to have such EntityViewModelBase as a part of the Template10 framework???