2010-03-23 15 views
10

मैं एक मुख्य WPF खिड़की मिल गया है:WPF MVVM ViewModel निर्माता designmode

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

और MainViewModel यह है:

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

अब समस्या यह है कि designermode में मैं नहीं देख सकता मेरी MainViewModel, यह कहता है कि यह MainViewModel का एक उदाहरण नहीं बना सकता है। यह डेटाबेस से कनेक्ट हो रहा है। यही कारण है कि (जब मैं कोड पर टिप्पणी करता हूं तो समस्या हल हो जाती है)।

लेकिन मुझे यह नहीं चाहिए। इस के आसपास सर्वोत्तम प्रथाओं पर कोई समाधान?

और क्यों इस काम 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 
    } 

यह भी डिजाइन समय में पूरी तरह से निष्पादित करता है।

उत्तर

4

यह आपको डिजाइनर को देखने की अनुमति देगा।

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

ए) DesignerProperties.IsInDesignTool क्या करता है? बी) मैं इस संपत्ति को DesignerProperties –

+12

के तहत नहीं ढूंढ सकता यह केवल सिल्वरलाइट के साथ काम करता है और WPF नहीं? – John

7

आप जो कोशिश कर सकते हैं वह केवल डेटाकेंटेक्स्ट को कोड में सेट करना है और देखें कि क्या यह समस्या हल करता है। यह काफी सटीक बात है, लेकिन हो सकता है कि आपका आईडीई बस खेल रहा हो।

DataContext = new MainViewModel(); 
+3

क्योंकि वह नहीं किया है एमवीवीएम अवधारणा में :) – Snake

+1

@ स्नैक: एमवीवीएम आपको आवश्यकता होने पर कोड-लिखने के लिए मना नहीं करता है ... –

+1

वास्तव में, यह पूरी तरह से मान्य है। एमवीवीएम! = पीछे कोई कोड नहीं। एमवीवीएम == दृश्य में चिंताओं को देखें। –

0

मैं यह त्रुटि संदेश देखा है ViewModel एक parameterless निर्माता नहीं है जब।

4

कोशिश यह:

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(); 
     } 
    } 
} 
8

आप XAML में DataContext सेट करना चाहते हैं, तो आप अपने ViewModel ctor के शीर्ष पर इस का उपयोग कर सकते हैं:

if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) 
    return;