如何更改 IsInDesignMode 以在 Microsoft.Toolkit.MVVM 中使用它?

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

这次一定要迁移MVVMLight,替换成Microsoft.Toolkit.MVVM。

documentation 写有 IsInDesignMode 没有直接替代品,更改或删除它。

我不知道有什么办法可以改变它,有人能帮我吗?

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

大致思路如下,以WPF为例:

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

您可以以不同的方式注入它,例如查询 platform-provided 服务,我想您明白了。