C++/CLI、XAML 和事件处理程序
我是 Windows 世界的新手,我想我在一个问题上迷失了方向。我希望获得具有 C++/CLI、WPF 和 XAML 经验的人的一些建议。
我有一些 win32 代码,并且需要运行 WPF GUI。我找到了这个使用 C++/CLI 的 MS 演练示例。我根据自己的目的对其进行了调整,效果很好。
接下来,我想放弃编程式 WPF 内容并使用 XAML。这样我就可以将 XAML 交给设计师,并让自己脱离 UI 设计循环,而我肯定不属于这个循环。在阅读了 MSDN 上的 WPF 和 Win32 互操作 的“WPF 互操作项目”部分后,我决定使用 XamlReader::Load
选项并在运行时加载未编译的 XAML。我的 XAML 标记是一个 Canvas
UIElement
,我以编程方式将其添加为根 Grid
C++/CLI 元素的子元素。这很好用。
现在我想向 XAML 中的控件添加事件处理程序。这就是我开始遇到麻烦的地方。我确信,95% 的问题是我对 Windows 世界的无知造成的。
我从 Rob Relyea 的页面开始,概述了各种 XAML 和事件处理程序选项。
我决定尝试将 XAML 编译为 C# DLL。它与我在运行时 Load 案例中使用的 XAML 基本上相同。我实例化该对象并以编程方式将其添加为子对象,就像以前一样。但是……除了一扇黑色的窗户,我什么也没得到。也不会抛出异常。我很困惑。
我的问题是,我是否走在正确的道路上? XAML-and-event-handlers 页面说您可以使用 .Net Framework 4 中未编译的 XAML 中定义的事件处理程序。我是否应该硬着头皮转到 VS 2010(我目前在 VS 2008),这样我就可以使用.Net Framework 4 并只坚持使用未编译的 XAML?这样做有什么问题吗?
或者,如果您确实认为编译的 C# DLL 是一个合理的路径,您对如何调试我遇到的问题有什么想法吗?
或者,是否有更好且完全不同的方法?
预先感谢您的建议。
波莉
I'm new to the Windows world, and I think I'm getting lost in the weeds on a problem. I'd love some advice from people with experience with C++/CLI and WPF and XAML.
I have some win32 code, and I need to run a WPF GUI. I found this MS walkthrough sample, which uses C++/CLI. I adapted it to my purposes, and it works great.
Next, I wanted to rip out the programmatic WPF stuff and use XAML instead. This is so I can hand off the XAML to a designer person and take myself out of the UI design loop, where I most assuredly don't belong. After reading the "WPF Interoperation Projects" section of WPF and Win32 Interoperation on MSDN, I decided to go with the XamlReader::Load
option and load uncompiled XAML at runtime. My XAML markup is a Canvas
UIElement
which I programmatically add as a child of my root Grid
C++/CLI element. This works great.
Now I want to add event handler to controls in the XAML. This is where I have started to run into trouble. I'm sure that my general ignorance of the Windows world is 95% of what's killing me here.
I started with Rob Relyea's page outlining the various XAML-and-event-handler options.
I decided to try compiling the XAML as a C# DLL. It's basically the same XAML as what I used in the runtime Load case. I instantiate the object and programmatically add as it as a child, just like before. But ... I get nothing but a black window. No exceptions get thrown either. I'm baffled.
My question is, am I even headed down the right path? The page on XAML-and-event-handlers says you can use event handlers defined in uncompiled XAML in .Net Framework 4. Should I bite the bullet and just go to VS 2010 (I'm presently on VS 2008) so I can use .Net Framework 4 and just stick with uncompiled XAML? Are there any gotchas with doing things that way?
Or, if you do think the compiled C# DLL is a reasonable path, do you have any ideas on how I can debug the problems I'm having?
Or, is there a better and completely different approach?
Thanks in advance for your advice.
Polly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这个问题的正确答案取决于一些只有您可以决定的问题,但我首先假设您的 C++ 代码库足够大且复杂,值得保留。
除此之外,下一个决策点是您保留的 C++ 中是否有 UI(可能是 GDI)代码,还是只有非 UI 代码。如果您试图仅保留非 UI 代码,那么我会考虑将更多 UI 责任推入 C# 中。也许您甚至用 C# 构建视图、事件处理程序,甚至视图模型。这将使您能够更好地利用 VS 工具。
如果您有大量的 C++ UI 代码需要保留,那么您当前的路径更有意义。我不认为这是不可能的,但你将面临很大的挑战。这里的关键示例是 Visual Studio 2010。它是混合应用程序的首屈一指的示例,并且并排有 GDI 和 WPF,这与我见过或听说过的任何其他应用程序不同。我发现一系列非常有趣的博客文章描述了 Visual Studio 团队为实现此集成所做的某些方面:Visual Studio 博客。
我还看到了这个视频 Henry Sowizral 关于在表达式设计中使用 WPF 重新面对 C++,我本人没有见过,但讨论了将 WPF UI 置于现有的 MFC C++ 应用程序之上。
祝你好运。
对于您问题的第一部分,我没有任何具体建议,只是说,在 C# 中承担更多责任将允许您在必要时构建一个小型存根应用程序,这对诊断问题大有帮助。
I think the right answer for this depends on some issues that only you can decide, but I'll start with the assumption that your C++ code base is big and complex enough that it is worth preserving.
Beyond that the next decision point is do you have UI (perhaps GDI) code in the C++ your preserving or only non-UI code. If you are attempting to preserve only non-UI code then I would consider pushing more UI responsibilty into C#. Perhaps you go so far as to build your views, event handlers, and maybe even view models in C#. This will enable you to take better advantage of the VS tooling.
If you've got extensive UI code in C++ to preserve then your current path makes a more sense. I don't think it will be impossible, but you'll have quite a challenge ahead of you. The key example here is Visual Studio 2010. It is the premiere example of a mixed application and has GDI and WPF side by side unlike any other app I've ever seen or heard of. There is a series of blog posts that I found pretty interesting that describe some aspects of what the Visual Studio team did to achieve this integration at The Visual Studio Blog.
I also came across this video Henry Sowizral on Refacing C++ with WPF in Expression Design that I have not seen myself, but discusses putting a WPF UI on top of an existing MFC C++ app.
Good luck.
I don't have any specific advice on the first part of your question other than to say that putting more responsibility in C# would allow you to build a small stub app if necessary which could go a long way toward diagnosing problems.
感谢大家的回复。关于卡在 C# DLL 上的问题,我找到了这个 C++/CLI 示例: http://msdn.microsoft.com/en-us/library/aa970266.aspx。使用它,我发现了我的错误,并且能够毫无问题地加载 WPF。
然而,加载 C# DLL 的全部动机是我了解这是以编程方式附加事件处理程序的方法。按照 AresAvatar 的建议,我发现我可以使用 FindName 来附加处理程序——两者都在 C# DLL 中,但它也适用于我原来的松散 XAML 方法。所以,我根本不需要 C# DLL!
现在一切都运转良好。再次感谢您的所有帮助和建议。
Thanks to everyone for the responses. On the matter of getting stuck on the C# DLL, I found this C++/CLI sample: http://msdn.microsoft.com/en-us/library/aa970266.aspx. Using that, I found my error, and was able to load the WPF without problems.
However, the whole motivation for loading the C# DLL was that I had understood that that was the way to attach event handlers programmatically. Following AresAvatar's suggestion, I found that I could use FindName to attach the handlers -- both within the C# DLL, but it also worked with my original loose-XAML approach. So, I didn't need the C# DLL after all!
It's all working nicely now. Again, thanks for all of your help and suggestions.