Home > Enterprise >  How can I change IsInDesignMode to use it in Microsoft.Toolkit.MVVM?
How can I change IsInDesignMode to use it in Microsoft.Toolkit.MVVM?

Time:03-28

This time I must be migrate MVVMLight and replace it with Microsoft.Toolkit.MVVM.

In the documentation is written there's no direct replacement for IsInDesignMode, altered or remove it.

I don't know any option to alter it, can anyone help me ?

     public MainViewModel()
     {
        //To Migrate GalaSoft there's no direct replacement for IsInDesignMode, remove
        //if (IsInDesignMode)
        //{
        //    // Code runs in Blend --> create design time data.
        //}
        //else
        //{
            // Code runs "for real"
           
            this.app = (App)Application.Current;
        //}
     }
    

CodePudding user response:

The general idea would be as follows, WPF for instance:

internal class View : Window
{
    public View()
    {
        Model.IsInDesignMode = () => DesignerProperties.GetIsInDesignMode(this);
    }

    private Model Model => DataContext as Model ?? throw new InvalidOperationException();
}

internal class Model
{
    public Func<bool> IsInDesignMode { get; set; }

    public Model()
    {
        if (IsInDesignMode is null)
            throw new InvalidOperationException();

        if (IsInDesignMode())
        {
            int i;
        }
        else
        {
            int i;
        }
    }
}

You can inject it differently, querying a platform-provided service for instance, I guess you get the point.

  • Related