在WPF应用程序中调试silverlight

编程入门 行业动态 更新时间:2024-10-28 09:28:12
本文介绍了在WPF应用程序中调试silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个WPF应用程序,其中包含一个加载Silverlight应用程序的webbrowser控件。我希望能够从视觉工作室(F5)启动应用程序,并将调试器附加到silverlight代码。但是,我没有运气这个。

I am developing a WPF app that contains a webbrowser control that loads a silverlight application. I would like to be able to launch the app from visual studio (F5) and have the debugger attach to the silverlight code. However, I've not had any luck with this.

我目前最好的做法是启动应用程序而不附加,然后一旦启动并运行,附加以手动方式将silverlight作为指定类型的代码进行调试,这样可以正常工作。 (当我使Web浏览器控件加载silverlight应用程序时,它将触发我的silverlight代码中的断点)。我已经写了一些宏自动化这个启动/附加有些,但它仍然不是最好的。

The best I can currently do is to launch the app without attaching, then once it is up and running, attach to the process manually with silverlight as the specified type of code to debug, and this works. (When I cause the web browser control to load the silverlight app, it will hit breakpoints in my silverlight code). I've written some macros to automate this launching/attaching somewhat, but it still isn't the best.

我试过将WPF应用程序指定为外部程序在启动/调试silverlight应用程序时运行,但Visual Studio附加到要调试托管.NET代码的进程。

I've tried specifying the WPF app as the external program to run when launching/debugging the silverlight app, but Visual Studio attaches to the process wanting to debug the managed .NET code.

任何想法?理想情况下,我真的很想附加到进程并调试托管的.NET和silverlight代码,但我不认为这是可能的。我真的很想在启动时自动附加到silverlight代码,以便我可以轻松地调试Silverlight应用程序的所有问题,包括加载时出现的问题。

Any ideas? Ideally, I would really like to attach to the process and debug both the managed .NET and the silverlight code, but I don't think this is possible. I'd really like to automatically be attached to the silverlight code at launch so that I can easily debug all issues with the silverlight app, including those that occur on load.

推荐答案

感谢您的想法Brandorf和脂肪。布兰多夫几乎让我到了我想去的地方,但是要求我的SL应用程序能够自己运行。我真的想要只有一个应用程序,这是一个wpf和silverlight,SL侧被调试。

Thanks for your ideas Brandorf and fatty. Brandorf's almost gets me to where I wanted to go, but does require that my SL app be capable of running on its own. I really want to have only the one app, which is both wpf and silverlight, with the SL side being debugged.

我问了这个问题很久以后(我忘了我在这里问过),我实际上拼凑了一个我真的很高兴的解决方案。我在应用程序的WPF / .NET端使用可视化工作室自动化,查找所有正在运行的visual studio实例,找出哪一个生成了我的exe(因为它通常位于vcproj / sln文件夹下面的文件夹中),然后使用visual studio自动化将VS附加到应用程序,调试silverlight代码。完成之后,我再加载我的silverlight内容。

A long time after I asked this question (I forgot I had asked it here), I actually pieced together a solution that I'm really happy with. I use visual studio automation within the WPF/.NET side of my app, to find all running instances of visual studio, figure out which one produced my exe (since it typically sits in a folder below the vcproj/sln folder), and then use visual studio automation to have that VS attach to the app, debugging silverlight code. After this is done, I then load my silverlight content.

它的工作效果很好。你最终会发现一个应用程序,并发现调试器每次运行时都会附加到自己(所以你可能只想在调试版本中使用这个代码,或者以某种方式关闭)。所以你只需要在visual studio中启动没有调试的ctrl-F5的应用程序(无需调试)。

It works really well. You end up with an app that goes and finds a debugger to attach to itself every time it runs (so you probably want this code only in a debug build, or somehow able to be turned off). So you just launch the app with ctrl-F5 (launch without debugging) from visual studio whenever you want to debug the silverlight side.

这是我的代码:

#if DEBUG using System; using System.Collections.Generic; using System.Collections; using System.Runtime.InteropServices; using System.IO; namespace Launcher { //The core methods in this class to find all running instances of VS are //taken/inspired from //www.codeproject/KB/cs/automatingvisualstudio.aspx class DebuggingAutomation { [DllImport("ole32.dll")] private static extern int GetRunningObjectTable(int reserved, out UCOMIRunningObjectTable prot); [DllImport("ole32.dll")] private static extern int CreateBindCtx(int reserved, out UCOMIBindCtx ppbc); ///<summary> ///Get a snapshot of the running object table (ROT). ///</summary> ///<returns> ///A hashtable mapping the name of the object ///in the ROT to the corresponding object ///</returns> private static Hashtable GetRunningObjectTable() { Hashtable result = new Hashtable(); int numFetched; UCOMIRunningObjectTable runningObjectTable; UCOMIEnumMoniker monikerEnumerator; UCOMIMoniker[] monikers = new UCOMIMoniker[1]; GetRunningObjectTable(0, out runningObjectTable); runningObjectTable.EnumRunning(out monikerEnumerator); monikerEnumerator.Reset(); while (monikerEnumerator.Next(1, monikers, out numFetched) == 0) { UCOMIBindCtx ctx; CreateBindCtx(0, out ctx); string runningObjectName; monikers[0].GetDisplayName(ctx, null, out runningObjectName); object runningObjectVal; runningObjectTable.GetObject(monikers[0], out runningObjectVal); result[runningObjectName] = runningObjectVal; } return result; } /// <summary> /// Get a table of the currently running instances of the Visual Studio .NET IDE. /// </summary> /// <param name="openSolutionsOnly"> /// Only return instances that have opened a solution /// </param> /// <returns> /// A list of the ides (as DTE objects) present in /// in the running object table to the corresponding DTE object /// </returns> private static List<EnvDTE.DTE> GetIDEInstances(bool openSolutionsOnly) { var runningIDEInstances = new List<EnvDTE.DTE>(); Hashtable runningObjects = GetRunningObjectTable(); IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator(); while (rotEnumerator.MoveNext()) { string candidateName = (string)rotEnumerator.Key; if (!candidateName.StartsWith("!VisualStudio.DTE")) continue; EnvDTE.DTE ide = rotEnumerator.Value as EnvDTE.DTE; if (ide == null) continue; if (openSolutionsOnly) { try { string solutionFile = ide.Solution.FullName; if (!String.IsNullOrEmpty(solutionFile)) { runningIDEInstances.Add(ide); } } catch { } } else { runningIDEInstances.Add(ide); } } return runningIDEInstances; } internal static void AttachDebuggerIfPossible() { if (System.Diagnostics.Debugger.IsAttached) { //Probably debugging host (Desktop .NET side), so don't try to attach to silverlight side return; } var ides = GetIDEInstances(true); var fullPathToAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location; var potentials = new List<EnvDTE.DTE>(); foreach (var ide in ides) { var solutionPath = ide.Solution.FullName; var topLevelSolutionDir = Path.GetDirectoryName(solutionPath); var assemblyName = fullPathToAssembly; if (assemblyName.StartsWith(topLevelSolutionDir, StringComparison.OrdinalIgnoreCase)) { potentials.Add(ide); } } EnvDTE.DTE chosenIde = null; //If you have multiple ides open that can match your exe, you can come up with a scheme to pick a particular one //(eg, put a file like solution.sln.pickme next to the solution whose ide you want to debug). If this is not a //concern, just pick the first match. if (potentials.Count > 0) { chosenIde = potentials[0]; } var dbg = chosenIde != null ? (EnvDTE80.Debugger2)chosenIde.Debugger : null; if (dbg != null) { var trans = dbg.Transports.Item("Default"); var proc = (EnvDTE80.Process2)dbg.GetProcesses(trans, System.Environment.MachineName).Item(Path.GetFileName(fullPathToAssembly)); var engines = new EnvDTE80.Engine[1]; engines[0] = trans.Engines.Item("Silverlight"); proc.Attach2(engines); } } } } #endif

更多推荐

在WPF应用程序中调试silverlight

本文发布于:2023-11-12 23:29:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   WPF   silverlight

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!