Template10 icon indicating copy to clipboard operation
Template10 copied to clipboard

ViewModelBase.Set method does not accept a property/indexer as ref parameter.

Open byrialsen opened this issue 11 years ago • 4 comments

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

byrialsen avatar Nov 10 '15 23:11 byrialsen

This is a csharp limitation, not T10. Sorry.

JerryNixon avatar Nov 11 '15 03:11 JerryNixon

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 in your mvvm folder like this:

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);
    }
} 

ivesely avatar Nov 11 '15 06:11 ivesely

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.

byrialsen avatar Nov 11 '15 08:11 byrialsen

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???

byrialsen avatar Nov 11 '15 22:11 byrialsen