How to check if a view model works in design mode or not in C #

I am new to C # and WPF, so I wanted to start with a book with MVVM. I have a small WPF application, and I would like to check if my view model is created in design mode or not (check DesignerProperties); given that I have an IDataService that provides data in the ViewModel either from a hard-coded list (development time) or a REST service (runtime).

Is there a way to trick or stub this DesignerProperties object to make it be one or the other state?

Thanks in advance.

+4
source share
2 answers

DesignerProperties, ?

. ; , "Microsoft Fakes" "Type Mock".

DesignerProperties, IDesignerProperties, /, , . , ; , .

+3

. DesignerProperties, . Injection/Inversion of Control .

static class DesignerProperties
{
    public bool IsInDesigner { get; }

    public void DoSomething(string arg);
    // Other properties and methods
}

. ( T4 )

interface IDesignerProperties
{
    bool IsInDesigner { get; }

    void DoSomething(string arg);
    // mimic properties and methods from the static class here
}

class DesignerPropertiesWrapper : IDesignerProperties
{
    public bool IsInDesigner 
    {
        get { return DesignerProperties.IsInDesigner; } 
    }

    public void DoSomething(string arg)
    {
        DesignerProperties.DoSomething(arg);
    }

    // forward other properties and methods to the static class
}

class DesignerpropertiesMock : IDesignerProperties
{
    public bool IsInDesigner { get; set; } //setter accessible for Mocking
}

class ViewModel 
{
    private readonly IDesignerProperties _designerProperties;

    // Inject the proper implementation
    public ViewModel(IDesignerProperties designerProperties)
    {
        _designerProperties = designerProperties;
    }
}

, .

+2

Source: https://habr.com/ru/post/1629935/