visual studio 2012是否支持设计时的条件符号?
我的问题示例:在具有MVVM模式的WPF应用程序中,我直接创建ViewModel的实例:
_viewModel = new OrdersViewModel();但我想在设计时使用条件符号,如下所示:
_viewModel = new OrdersViewModel { Orders = new ObservableCollection<OrderModel>() { new OrderModel(){OrderId = "0e2fa124"}, new OrderModel(){OrderId = "5wqsdgew"}, } };确保条件编译符号不起作用。
Does visual studio 2012 support conditional symbols for design time?
Example of my problem: In a WPF application with MVVM pattern, I create an instance of the ViewModel directly:
_viewModel = new OrdersViewModel();but I want to use a conditional symbol for the design time only like this:
_viewModel = new OrdersViewModel { Orders = new ObservableCollection<OrderModel>() { new OrderModel(){OrderId = "0e2fa124"}, new OrderModel(){OrderId = "5wqsdgew"}, } };For sure the conditional compilation symbols doesn't work.
最满意答案
您应该在视图中使用设计时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接口
You should use design time DataContext in your view.
Something like this:
<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}"/>Also keep in mind that you should derive OrderViewModel class and create a class with constructor to fill in those properties. Thus you will use one class for design time and the similar for real world. d: is a DesignTime interface
视觉工作室和条件符号(Visual studio and conditional symbols)visual studio 2012是否支持设计时的条件符号?
我的问题示例:在具有MVVM模式的WPF应用程序中,我直接创建ViewModel的实例:
_viewModel = new OrdersViewModel();但我想在设计时使用条件符号,如下所示:
_viewModel = new OrdersViewModel { Orders = new ObservableCollection<OrderModel>() { new OrderModel(){OrderId = "0e2fa124"}, new OrderModel(){OrderId = "5wqsdgew"}, } };确保条件编译符号不起作用。
Does visual studio 2012 support conditional symbols for design time?
Example of my problem: In a WPF application with MVVM pattern, I create an instance of the ViewModel directly:
_viewModel = new OrdersViewModel();but I want to use a conditional symbol for the design time only like this:
_viewModel = new OrdersViewModel { Orders = new ObservableCollection<OrderModel>() { new OrderModel(){OrderId = "0e2fa124"}, new OrderModel(){OrderId = "5wqsdgew"}, } };For sure the conditional compilation symbols doesn't work.
最满意答案
您应该在视图中使用设计时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接口
You should use design time DataContext in your view.
Something like this:
<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}"/>Also keep in mind that you should derive OrderViewModel class and create a class with constructor to fill in those properties. Thus you will use one class for design time and the similar for real world. d: is a DesignTime interface
发布评论