如何阻止我的ViewModel代码在设计器中运行?

时间:2021-10-01 07:13:10

I'm developing a networked WPF application with the MVVM pattern and it seems that it's running and connecting to servers in the designer.

我正在使用MVVM模式开发一个联网的WPF应用程序,它似乎正在运行并连接到设计器中的服务器。

I know about the IsInDesignMode property, but I'm not sure how to access it in a ViewModel.

我知道IsInDesignMode属性,但我不确定如何在ViewModel中访问它。

6 个解决方案

#1


30  

DependencyObject dep = new DependencyObject();
if (DesignerProperties.GetIsInDesignMode(dep))
{
    ...
}

#2


19  

Just to add to these suggestions, you probably want to optimize for production deployment.

只是为了添加这些建议,您可能希望针对生产部署进行优化。

If you need to check the design mode in the ViewModel, you should only do so when in DEBUG mode, otherwise the released version will always have to perform unnecessary checks.
When developing, if in design mode you can exit the method (or even stub out some fake data).

如果需要在ViewModel中检查设计模式,则只能在DEBUG模式下执行此操作,否则释放的版本将始终必须执行不必要的检查。在开发时,如果在设计模式下,您可以退出该方法(甚至存根一些假数据)。

Put this code as the first line of your constructor (or whatever code is being called):

将此代码作为构造函数的第一行(或者调用的任何代码):

C#:

C#:

#if DEBUG
    if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return;
#endif

VB:

VB:

#If DEBUG Then
    If DesignerProperties.GetIsInDesignMode(New DependencyObject()) Then Return
#End If

#3


7  

I thought I'll add to this as I've just looked up something I spotted in VS2015 and it provides an alternative solution. In the designer there is a button to "Disable project code".

我想我会加入这个,因为我只是查看了我在VS2015中发现的东西,它提供了另一种解决方案。在设计器中有一个“禁用项目代码”按钮。

I'm making the assumption that your ViewModel is being instantiated and doing stuff from your code behind. I know it breaks pure MVVM, but I've seen plenty of people do stuff like DataContext = new MyViewModel(); within the constructor in the code behind.

我假设您的ViewModel正在被实例化并从您的代码中执行操作。我知道它打破了纯粹的MVVM,但我看到很多人都在做像DataContext = new MyViewModel();在后面的代码中的构造函数内。

Toggling this button should solve that issue and helps to keep your code cleaner. Checkout MSDN for more information.

切换此按钮可以解决该问题,并有助于保持代码清洁。结帐MSDN以获取更多信息。

Here's the image from the MSDN documentation so you know what it looks like. I'm sure the link will break eventually, anyway.

这是MSDN文档中的图像,因此您可以了解它的外观。无论如何,我确信链接最终会破裂。

如何阻止我的ViewModel代码在设计器中运行?

I spotted this in VS2015, but not sure which edition this feature was added.

我在VS2015中发现了这一点,但不确定添加了哪个版本的功能。

As a side note, it also doubles up as a nice way to reload the designer. Albeit a slow one when I tried. Your milage may vary.

作为旁注,它也是一种重新加载设计师的好方法。虽然我尝试时速度很慢。你的milage可能会有所不同。

#4


4  

I use the following statement around code that I can only execute at application runtime and would otherwise cause an exception in the XAML designer.

我在代码中使用以下语句,我只能在应用程序运行时执行,否则会在XAML设计器中导致异常。

      if (System.Windows.Application.Current.MainWindow != null)

#5


3  

Put a design time data source in your XAML like this:

将设计时数据源放在XAML中,如下所示:

<UserControl x:Class="Company.Product.View.MyView"
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:vm="clr-namespace:Company.Product.ViewModel.Design"
xmlns:design_vm="clr-namespace:Company.Product.ViewModel.Design"
mc:Ignorable="d" Name="MyView">
<UserControl.Resources>
    <ObjectDataProvider ObjectType="{x:Type design_vm:MyViewModel}" x:Key="DesignTime_DataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<Grid d:DataContext="{StaticResource DesignTime_DataSource}">
....
</Grid>
</UserControl>

Let your design time viewmodel inherit from the run time viewmodel, but mock up the data in the constructor. You may also have to do something to your run time view model so the design time viewmodel doesn't run the data access code.

让您的设计时viewmodel从运行时viewmodel继承,但是在构造函数中模拟数据。您可能还需要对运行时视图模型执行某些操作,因此设计时viewmodel不会运行数据访问代码。

#6


1  

It all depends on how you set up the binding between the view and the view-model. If it's initiated by the view in the constructor (which seems likely given the symptoms), you can check IsInDesignMode from there. Otherwise you need to provide a very quick overview of your architecture (or framework if you use any).

这完全取决于您如何设置视图和视图模型之间的绑定。如果它是由构造函数中的视图启动的(似乎可能出现症状),则可以从那里检查IsInDesignMode。否则,您需要提供对架构(或框架,如果您使用任何架构)的快速概述。

#1


30  

DependencyObject dep = new DependencyObject();
if (DesignerProperties.GetIsInDesignMode(dep))
{
    ...
}

#2


19  

Just to add to these suggestions, you probably want to optimize for production deployment.

只是为了添加这些建议,您可能希望针对生产部署进行优化。

If you need to check the design mode in the ViewModel, you should only do so when in DEBUG mode, otherwise the released version will always have to perform unnecessary checks.
When developing, if in design mode you can exit the method (or even stub out some fake data).

如果需要在ViewModel中检查设计模式,则只能在DEBUG模式下执行此操作,否则释放的版本将始终必须执行不必要的检查。在开发时,如果在设计模式下,您可以退出该方法(甚至存根一些假数据)。

Put this code as the first line of your constructor (or whatever code is being called):

将此代码作为构造函数的第一行(或者调用的任何代码):

C#:

C#:

#if DEBUG
    if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return;
#endif

VB:

VB:

#If DEBUG Then
    If DesignerProperties.GetIsInDesignMode(New DependencyObject()) Then Return
#End If

#3


7  

I thought I'll add to this as I've just looked up something I spotted in VS2015 and it provides an alternative solution. In the designer there is a button to "Disable project code".

我想我会加入这个,因为我只是查看了我在VS2015中发现的东西,它提供了另一种解决方案。在设计器中有一个“禁用项目代码”按钮。

I'm making the assumption that your ViewModel is being instantiated and doing stuff from your code behind. I know it breaks pure MVVM, but I've seen plenty of people do stuff like DataContext = new MyViewModel(); within the constructor in the code behind.

我假设您的ViewModel正在被实例化并从您的代码中执行操作。我知道它打破了纯粹的MVVM,但我看到很多人都在做像DataContext = new MyViewModel();在后面的代码中的构造函数内。

Toggling this button should solve that issue and helps to keep your code cleaner. Checkout MSDN for more information.

切换此按钮可以解决该问题,并有助于保持代码清洁。结帐MSDN以获取更多信息。

Here's the image from the MSDN documentation so you know what it looks like. I'm sure the link will break eventually, anyway.

这是MSDN文档中的图像,因此您可以了解它的外观。无论如何,我确信链接最终会破裂。

如何阻止我的ViewModel代码在设计器中运行?

I spotted this in VS2015, but not sure which edition this feature was added.

我在VS2015中发现了这一点,但不确定添加了哪个版本的功能。

As a side note, it also doubles up as a nice way to reload the designer. Albeit a slow one when I tried. Your milage may vary.

作为旁注,它也是一种重新加载设计师的好方法。虽然我尝试时速度很慢。你的milage可能会有所不同。

#4


4  

I use the following statement around code that I can only execute at application runtime and would otherwise cause an exception in the XAML designer.

我在代码中使用以下语句,我只能在应用程序运行时执行,否则会在XAML设计器中导致异常。

      if (System.Windows.Application.Current.MainWindow != null)

#5


3  

Put a design time data source in your XAML like this:

将设计时数据源放在XAML中,如下所示:

<UserControl x:Class="Company.Product.View.MyView"
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:vm="clr-namespace:Company.Product.ViewModel.Design"
xmlns:design_vm="clr-namespace:Company.Product.ViewModel.Design"
mc:Ignorable="d" Name="MyView">
<UserControl.Resources>
    <ObjectDataProvider ObjectType="{x:Type design_vm:MyViewModel}" x:Key="DesignTime_DataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<Grid d:DataContext="{StaticResource DesignTime_DataSource}">
....
</Grid>
</UserControl>

Let your design time viewmodel inherit from the run time viewmodel, but mock up the data in the constructor. You may also have to do something to your run time view model so the design time viewmodel doesn't run the data access code.

让您的设计时viewmodel从运行时viewmodel继承,但是在构造函数中模拟数据。您可能还需要对运行时视图模型执行某些操作,因此设计时viewmodel不会运行数据访问代码。

#6


1  

It all depends on how you set up the binding between the view and the view-model. If it's initiated by the view in the constructor (which seems likely given the symptoms), you can check IsInDesignMode from there. Otherwise you need to provide a very quick overview of your architecture (or framework if you use any).

这完全取决于您如何设置视图和视图模型之间的绑定。如果它是由构造函数中的视图启动的(似乎可能出现症状),则可以从那里检查IsInDesignMode。否则,您需要提供对架构(或框架,如果您使用任何架构)的快速概述。