Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - Does MVVM stop the ability for the Visual Studio Designer to show xaml?

I've noticed in my programs when I have alot of complicated bindings going on then visual studio does not show the xaml properly.

Does MVVM and its numerous bindings cause this? Is the safest way to show xaml in the visual studio designer to remove bindings completely?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No. One of the MVVM core principles is designer support. The principle is called "Blendability" after the Expression Blend designer (resp Blend for Visual Studio) tool. Note, that Visual Studio uses the same designer.

With MVVM you can achieve much better design time support by databinding to design time data. For example, you in DataGrid or ListBox you can see in the designer how the actual items looks like when databound.

Breaking the designer has nothing to do with complexity of bindings.

You just need to follow few simple principles and understand what's going on in the designer.

First of all, Visual Studio designer creates instance of the ViewModel in the designer process. You need to be careful, that you don't execute such code in the viewmodel, that could break the designer. Here are some examples:

Don't do this

  1. This will break the designer, because Visual Studio Designer is not allowed to do DB calls.

    //ctor
    public MyViewModel()
    {
        using(var db = new MyDbContext()} ... //
    }
    

    calling DB, or FileSystem in constuctor is bad practice anyway

  2. this breaks the designer, because VS Designer does not have access to your configuration

    //ctor
    public MyViewModel()
    {
       string configValue = ConfigurationManager.AppSettings["SomeProperty"]
    }
    
  3. if you databind to a property, the getter is acually executed. This code breaks the designer, because App.Current is the Visual Studio Designer, not your app! Be carefull about it.

    public class MyViewModel
    {
       public string SomeProperty
       {
           get { return App.Current.MainWindow.SomeProperty; }
       }
    }
    
  4. This will cause NullReferenceException when binding to CountOfItems, because VS Designer doesn't call Load()

    public class MyViewModel
    { 
       private List<string> _items; 
    
       public void Load()
       {
           _items = new List<string>{ "Item1", "Item2" }
       }
    
    
       public int CountOfItems
       {
           get { return _items.Count; }
       }
    }
    

Good practices

Just check if you are in the design mode wherever needed:

//ctor
public MyViewModel
{
    bool IsDesignMode => DesignerProperties.GetIsInDesignMode(new DependecyObject());

    public MyViewModel()
    {
       if (IsDesignMode)
       {
          //this will be shown in the designer
          Items = new List<string>{ "Item1", "Item2" }
       }
    }

    //INotifyPropertyChanged details ommited due to simplification
    public List<string> Items {get; private set;}

    public void Load()
    {
       //optionally you may check IsDesignMode 
       using (var db = new MyDbContext())
       {  
           this.Items = db.Items.Select(i => i.Name).ToList();
       }
    }
}

I've created code snippet, where I use this pattern:

d:DataContext="{d:DesignInstance Type=local:MyViewModelDesignTime, IsDesignTimeCreatable=True}"

I don't actually instantiace the ViewModel directly, but I inject DesignTime version of the viewmodel:

public class MyViewModel()
{
    protected MyViewModel()
    {
        //both runtime and design time logic. 
        //you may use IsDesignMode check if needed
    }

    public MyViewModel(ISomeExternalResource externalResource) : this();
    {
        //this is executed only at run time
        _externalResource = externalResource;
        Items = externalResouce.GetAll();
    }

    public List<string> Items {get; protected set;}
}

public class MyViewModelDesignTime : MyViewModel
{
    public MyViewModelDesignTime () : base()
    { 
        //this will be show in the designer
        Items = new List<string> { "Item1", "Item2" };
    }
}

If your designer breaks anyway and you don't know why, you can attach another instance of visual studio to the xaml designer process and it will nicelly show the problematic line of code.

Last, but not least, you can easily turn off instanciating the ViewModels. Just set IsDesignTimeCreatable=false in d:DataContext

Recapitulation

  1. Check all the code execution paths of your viewmodel which can be executed by the xaml designer process
  2. Do not access database, webservices or filesystem in those execution paths
  3. Do not access static resource, e.g. Application.Current, because thay may not be initialized properly
  4. Check all getters of properties that are databound. They may trie to return something that was not initialized by the designer.
  5. Use branching (e.g. if..else) for the designer and runtime execution paths
  6. Always generate some fake design-time data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...