当 MainWindow.xaml 打开时,运行项目需要很长时间 - WPF [英] Running project takes too long when MainWindow.xaml is open - WPF

查看:23
本文介绍了当 MainWindow.xaml 打开时,运行项目需要很长时间 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用实体框架和 WPF 启动了一个 MVVM 应用程序.在 MainWindow.xaml 我写了这个:

I have started an MVVM application with Entity Framework and WPF. In MainWindow.xaml I wrote this :

<Window x:Class="MVVMAttempt.App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="525"
        DataContext="{StaticResource StudentVM}">

在 App.xaml 中我这样写:

And in App.xaml I wrote this :

    <Application.Resources>
        <vms:StudentVM x:Key="StudentVM" xmlns:vms="clr-namespace:MVVMAttempt.App.ViewModels"/> 
  </Application.Resources>

项目运行正常.但是有一个问题.当 MainWindow.xaml 在 Visual Studio 上打开时,程序开始运行非常缓慢.我也收到以下错误:

The project is working correctly. But there is one problem. When MainWindow.xaml is open on Visual Studio, the program starts to run really slowly. Also I get the following error :

错误 1 ​​发生与网络相关或特定于实例的错误建立与 SQL Server 的连接.未找到服务器或无法访问.验证实例名称是否正确并且SQL Server 配置为允许远程连接.(提供者:SQL网络接口,错误:26 - 定位服务器/实例时出错指定) C:...MVVMAttempt\MVVMAttempt.App\App.xaml 3 9 MVVMAttempt.App

Error 1 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) C:..MVVMAttempt\MVVMAttempt.App\App.xaml 3 9 MVVMAttempt.App

我该如何解决这个问题?谢谢.

How can I fix this? Thanks.

推荐答案

原因: Designer 正在尝试初始化静态资源,尤其是您的 ViewModel 以满足绑定.然后你会从 Entity Framework 上下文初始化中得到一个异常.


Reason: Designer is trying to initialize static resources and particularly your ViewModel to satisfy bindings. Then you get an exception from Entity Framework context initialization.


如何解决:在您的 ViewModel 中使用 System.ComponentModel.DesignerProperties.IsInDesignTool 来区分现实生活和设计时初始化.>

How to fix: Use System.ComponentModel.DesignerProperties.IsInDesignTool in your ViewModel to distinguish real life and design time initialization.

if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
      // Initialize "fake" context here
}
else
{
     // EF context initialization
}


这乍一看似乎是一种开销,但如果您正在使用 Expression BlendVisual Studio 设计器,那么提供一些非常有用的虚拟数据只是为了了解您的控件在现实世界中的外观和感觉".<小时>另一种选择是在 xaml 中设置设计时 DataContext ,这将执行相同的技巧:


This might seem as an overhead at a firs glance but if you're working with Expression Blend and Visual Studio designer it is really useful to provide some dummy data just to have an idea how your control will "look and feel" in real world.


Another option is to set design-time DataContext in xaml which will do the same trick:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DataContext="{<Fake/design-time data context binding>}"

这篇关于当 MainWindow.xaml 打开时,运行项目需要很长时间 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆