如何为 WPF 手动重新加载 Visual Studio 设计器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3728995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually reload the Visual Studio designer for WPF
提问by dthrasher
Is there any way to force the WPF designer to reload or refresh, without rebuilding the entire project or solution?
有没有办法强制 WPF 设计器重新加载或刷新,而无需重新构建整个项目或解决方案?
If there's an error, I get an option in the designer view to refresh the designer. But if I don't have an error, how can I get the designer to refresh after I've made a change?
如果出现错误,我会在设计器视图中获得一个选项来刷新设计器。但是如果我没有错误,我如何在我进行更改后让设计器刷新?
回答by MasterMastic
I'm a little late but this is the best solution1I've found so far: whenever the designer does stupid stuff, I just kill it's process.
我有点晚了,但这是迄今为止我找到的最好的解决方案1:每当设计师做了愚蠢的事情时,我就会终止它的进程。
Press Ctrl+Shift+Esc
Navigate to the Processes tab.
Kill XDesProc.exe
按Ctrl+ Shift+Esc
导航到进程选项卡。
杀XDesProc.exe
This also fixes issues for the properties window (like when it gets jammed and you can't type stuff into it).
这也解决了属性窗口的问题(例如当它被卡住并且您无法在其中输入内容时)。
1This is a solution for designer issues. Your issues may also be caused by compilation problems, in which case just right click on the solution in the solution explorer, and clean it. The reason behind it is that sometimes the compilation loses synchronicity with the generated files from XAML, and cleaning the solution just deletes those intermediate files; it's like a reset so your compilation can start off with a clean slate.
1这是设计师问题的解决方案。您的问题也可能是由编译问题引起的,在这种情况下,只需右键单击解决方案资源管理器中的解决方案,然后将其清理干净即可。其背后的原因是有时编译会失去与从 XAML 生成的文件的同步性,而清理解决方案只会删除那些中间文件;这就像重置一样,因此您的编译可以从头开始。
To do it fast:
要快速完成:
Comfortably it's usually the last one if sorted alphabetically.
When it is, it's almost like a ritual for me to quickly pop up the task manager, click any process, press End, Delete, Enter(done), Esc(exit task manager). Instead of restarting VS and waiting for all the loads & unloads, you can do that in 1-2 seconds.
如果按字母顺序排序,它通常是最后一个。
当是时,它几乎像一个仪式让我快速弹出任务管理器,单击任一过程中,按End,Delete,Enter(完成), Esc(退出任务管理器)。您可以在 1-2 秒内完成,而不是重新启动 VS 并等待所有加载和卸载。
回答by Contango
You can add this to the Tools
menu in Visual Studio.
您可以将其添加到Tools
Visual Studio的菜单中。
Once configured, use Tools..XAML Designer Restart
:
配置后,使用Tools..XAML Designer Restart
:
Alt+Tthen L
Alt+T然后L
I tried configuring it for Alt+Tthen Xbut this clashed with Tools..Choose ToolboX Items
.
我尝试将它配置为Alt+T然后X但这与Tools..Choose ToolboX Items
.
Update
更新
These days, I prefer to just hit Ctrl+Shift+Escto bring up the process manager, then Xto skip to XDesProc.exe
then Deleteto kill the rogue process(es).
这些天,我更喜欢点击Ctrl+ Shift+Esc来调出进程管理器,然后X跳到XDesProc.exe
然后Delete杀死流氓进程。
回答by Aleksandr Albert
In newer versions of Visual Studio there is an icon on the bottom of the designer to "Disable Project code". If you toggle this off and on it will reload the designer.
在较新版本的 Visual Studio 中,设计器底部有一个“禁用项目代码”图标。如果您关闭和打开它,它将重新加载设计器。
回答by Rick Sladkey
The Visual Studio designer attempts to keep the rendered view in sync with the XAML. That's the advertised behavior.
Visual Studio 设计器尝试使呈现的视图与 XAML 保持同步。这就是广告的行为。
- The first thing you should check is that there are no errors in the errors window. It may be something as simple as a missing angle bracket.
- The second thing to check is whether you have any code (other than your code-behind) which needs to be compiled for the designer to render your XAML correctly. This includes any of your own datatypes that you instantiate in XAML, any custom controls you have written (e.g. MyTextBlock derived from TextBlock), or any classes directly or indirectly in support of design-time data. If so, you need to rebuild your project.
- The last thing to check for is possible bugs in the designer. In spite of the advertised behavior, the designer may get out-of-sync due to bugs. In that close, close the XAML window and re-open it. Other tricks that might work are selecting the XAML tab and then the Design tab, or maximizing the XAML pane.
- 您应该检查的第一件事是错误窗口中没有错误。它可能像缺少尖括号一样简单。
- 要检查的第二件事是您是否有任何代码(除了您的代码隐藏)需要为设计器编译以正确呈现您的 XAML。这包括您在 XAML 中实例化的任何您自己的数据类型、您编写的任何自定义控件(例如从 TextBlock 派生的 MyTextBlock),或者直接或间接支持设计时数据的任何类。如果是这样,您需要重建您的项目。
- 最后要检查的是设计器中可能存在的错误。尽管有广告宣传的行为,但设计者可能会由于错误而失去同步。在关闭时,关闭 XAML 窗口并重新打开它。其他可能有用的技巧是选择 XAML 选项卡,然后选择设计选项卡,或者最大化 XAML 窗格。
As far as rebuilding your application goes, you don't need to do this as a habit. You only need to recompile it when the above conditions apply. Once they don't apply, you can just edit the XAML. Another way to say this is that if you haven't modified code, you shouldn't need to rebuild (modulo bugs).
就重建您的应用程序而言,您不需要将此作为一种习惯。仅当满足上述条件时,您才需要重新编译它。一旦它们不适用,您就可以编辑 XAML。另一种说法是,如果您没有修改代码,则不需要重建(模错误)。
回答by Rick Sladkey
I'm not sure, but I think a build will refresh your view in that situation.
我不确定,但我认为在这种情况下构建会刷新您的看法。
回答by rcube9
There is any event handled in that XAML file, then mostly it will not display the design preview from Visual Studio. If you want to see the design from Visual Studio, try with Command Binding instead of event, you will see the preview.
在该 XAML 文件中处理了任何事件,然后大多数情况下它不会显示来自 Visual Studio 的设计预览。如果您想从 Visual Studio 中查看设计,请尝试使用命令绑定而不是事件,您将看到预览。
回答by gstercken
At least in theory, the Design View is always in sync with the XAML, so no need to ever refresh it manually. May I ask under which circumstances would you want to reload it?
至少在理论上,设计视图始终与 XAML 同步,因此无需手动刷新它。请问在什么情况下要重装?
回答by Ronald
I'm not sure how this works in WPF editing, but with ASP.NET pages when the design view wont update i can do 2 things
我不确定这在 WPF 编辑中是如何工作的,但是对于 ASP.NET 页面,当设计视图不会更新时,我可以做两件事
- Exit Visual Studio and restart
- Go into source view (not split), type something and remove it (not by undoing, just delete or backspare) and save it. Then return to design view, usually the view has been updated then.
- 退出 Visual Studio 并重新启动
- 进入源代码视图(不拆分),输入一些内容并将其删除(不是通过撤消,只需删除或备用)并保存它。然后返回到设计视图,通常视图已经更新了。
回答by Sandra Rossi
For information, I had the same issue with the XAML Designer of Visual Studio Community 2017, i.e. sometimes the designer doesn't show anything, the easiest solution is then to close the XAML file and reopen it.
有关信息,我在 Visual Studio Community 2017 的 XAML 设计器中遇到了同样的问题,即有时设计器不显示任何内容,最简单的解决方案是关闭 XAML 文件并重新打开它。
I also frequently get the exception "An Unhandled Exception has occurred - Click here to reload the designer - Details: The XAML Designer has exited unexpectedly" (the click restarts the designer successfully).
我还经常收到异常“发生了未处理的异常 - 单击此处重新加载设计器 - 详细信息:XAML 设计器意外退出”(单击成功重新启动设计器)。
Note that, in this VS version, the process of the XAML designer is not named XDesProc.exe, but UwpSurface.exe. If you prefer or have to kill the process, then the designer shows the same exception as above, and you may restart it.
请注意,在此 VS 版本中,XAML 设计器的进程不是命名为 XDesProc.exe,而是UwpSurface.exe。如果您更喜欢或必须终止该进程,那么设计器会显示与上述相同的异常,您可以重新启动它。
回答by m.eslahi.k
When you add a new row of code or a new object, XAML designer is sync but I encountered non-sync behavior when a property of an object is changed.
当您添加新的代码行或新对象时,XAML 设计器是同步的,但是当对象的属性发生更改时,我遇到了非同步行为。
A tricky way is that when you change a property you only need to remove a ">" character from end of an instruction then retype it.
一个棘手的方法是,当您更改属性时,您只需要从指令末尾删除“>”字符,然后重新键入它。