2013-08-01 95 views
1

視覺工作室2012是否支持設計時間的條件符號?我的問題視覺工作室和條件符號

例: 與MVVM模式的WPF應用程序,我直接創建視圖模型的實例:

_viewModel = new OrdersViewModel(); 

,但我想用一個有條件的符號設計時僅是這樣的:

_viewModel = new OrdersViewModel 
{ 
    Orders = new ObservableCollection<OrderModel>() 
     { 
      new OrderModel(){OrderId = "0e2fa124"}, 
      new OrderModel(){OrderId = "5wqsdgew"}, 
     } 
}; 

肯定條件編譯符號不起作用。

+0

可能重複:http://stackoverflow.com/questions/834283/is-there-a-way-to-check-if-wpf-is-currently-executing-in-design-mode-or -不 –

回答

3

您應該在您的視圖中使用設計時間DataContext。

事情是這樣的:

<Window x:Class="TestForDesignTimeData.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:MyNamespace="clr-namespace:Myproject.MyNamespace" 
     mc:Ignorable="d" 
     Title="MainWindow" > 
    <StackPanel d:DataContext="{d:DesignInstance MyNamespace:OrdersViewModel}"/> 

也請記住,你應該得到OrderViewModel類,並創建一個類的構造函數來填充這些屬性。因此,您將使用一個類的設計時間和類似的真實世界。 d:是DesignTime界面

2

您可以使用DesignerProperties.GetIsInDesignMode方法。

_viewModel = new OrdersViewModel(); 
if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) 
{ 
    _viewModel.Orders = new ObservableCollection<OrderModel>() 
    { 
     new OrderModel(){OrderId = "0e2fa124"}, 
     new OrderModel(){OrderId = "5wqsdgew"}, 
    }; 
};