WPF MVVM ViewModel constructor designmode

2019-02-08 23:56发布

I've got a main wpf window:

<Window x:Class="NorthwindInterface.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:NorthwindInterface.ViewModels" Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <ViewModels:MainViewModel />
    </Window.DataContext>
    <ListView ItemsSource="{Binding Path=Customers}">

    </ListView>
</Window>

And the MainViewModel is this:

class MainViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public MainViewModel()
    {
        Console.WriteLine("test");
        using (NorthwindEntities northwindEntities = new NorthwindEntities())
        {
            this.Customers = (from c in northwindEntities.Customers
                              select c).ToList();
        }
    }

    public List<Customer> Customers { get;private  set; }

Now the problem is that in designermode I can't see my MainViewModel, it highlights it saying that it can't create an instance of the MainViewModel. It is connecting to a database. That is why (when I comment the code the problem is solved).

But I don't want that. Any solutions on best practices around this?

And why does this work when working with MVVM:

    /// <summary>
    /// Initializes a new instance of the <see cref="MainViewModel"/> class.
    /// </summary>
    public MainViewModel()
    {
        // Just providing a default Uri to use here...
        this.Uri = new Uri("http://www.microsoft.com/feeds/msdn/en-us/rss.xml");
        this.LoadFeedCommand = new ActionCommand(() => this.Feed = Feed.Read(this.Uri), () => true);
        this.LoadFeedCommand.Execute(null); // Provide default set of behavior
    }

It even executes perfectly at design time.

标签: .net wpf mvvm
5条回答
别忘想泡老子
2楼-- · 2019-02-09 00:51

What you could try is just setting the DataContext in the code behind and see if that resolves the issue. It is pretty much the exact same thing, but maybe your IDE is just playing up.

DataContext = new MainViewModel();
查看更多
来,给爷笑一个
3楼-- · 2019-02-09 00:51

try this:

public MainViewModel()
{
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
    {
      Console.WriteLine("test");
      using (NorthwindEntities northwindEntities = new NorthwindEntities())
      {
        this.Customers = (from c in northwindEntities.Customers
                      select c).ToList();
      }
    }
}
查看更多
在下西门庆
4楼-- · 2019-02-09 00:52

If you want to set the DataContext in XAML, you can use this at the top of your ViewModel ctor:

if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    return;
查看更多
等我变得足够好
5楼-- · 2019-02-09 00:56

This will allow you to see the designer.

public MainViewModel()
{
    if (!DesignerProperties.IsInDesignTool)
    {
      Console.WriteLine("test");
      using (NorthwindEntities northwindEntities = new NorthwindEntities())
      {
        this.Customers = (from c in northwindEntities.Customers
                          select c).ToList();
      }
    }
}
查看更多
小情绪 Triste *
6楼-- · 2019-02-09 01:00

I have seen this error message when the ViewModel does not have a parameterless constructor.

查看更多
登录 后发表回答