当平台目标设置为任何 CPU(如 WPF 项目)时,代码行为很奇怪?
此代码应该为我提供 FilterInfo 对象的集合,代表所选类别中的连接设备,在本例中是网络摄像头。
FilterInfoCollection collection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
我在控制台应用程序中对此进行了测试,效果很好,但是当我将代码引入 WPF 时,它只返回一个对象。
我尝试直接在 xaml 代码隐藏和绑定到 xaml 控件的视图模型中使用它,但结果相同。
这太令人沮丧了 提前致谢=)
编辑:
我找到了原因,WPF项目“平台目标”设置为“任何CPU”,控制台应用程序设置为x86。 当我将控制台应用程序“平台目标”设置为“任何 cpu”时,它的作用完全相同。
但不幸的是,当设置为 x86 时,WPF 项目将无法运行
This code should give me a collection of FilterInfo objects, representing the connected devices in the chosen category, which in this case is webcams.
FilterInfoCollection collection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
I tested this in a console application, and it worked great, but when i bring the code into WPF, it only returns one object.
I tried using it directly in xaml codebehind and in viewmodel bound to xaml controls, but same result.
This is so bloody frustrating
Thanks in advance =)
EDIT:
I found the cause, the WPF project "Platform Target" is set to "Any CPU", and the console app is set to x86.
When i set the console app "Platform target" to "any cpu" it does exactly the same.
But unfortunately, the WPF project wont run when set to x86
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里进行猜测,但听起来 FilterInfoCollection 根据驱动程序是 x64 还是 x86 返回差异。
如果强制它在 x86 模式下编译,那么它能够检索所有 x86 驱动程序。
任何 CPU 模式都将在 x64 或 x86 中执行,具体取决于安装的操作系统。如果是 64 位操作系统,则将以 x64 模式运行。这意味着它只会返回基于 x64 的驱动程序,听起来您的系统上只安装了一个驱动程序。
关于在 x86 平台目标上运行 WPF 应用程序时出现的问题,您应该提供更多详细信息,例如您看到的错误。这里不应该有问题: 在我的 x64 笔记本电脑上将平台目标设置为 x86 会阻止某些 WPF 类型正确加载
Taking a guess here but it sounds like the FilterInfoCollection is returning a difference based on whether the drivers are x64 or x86.
If you force it to compile in x86 mode then it's able to retrieve all of the x86 drivers.
Any CPU mode is going to execute in either x64 or x86 depending on the OS installed. If it's a 64bit OS, then it will run in x64 mode. This means it's only going to return x64 based drivers, of which it sounds like there is just one installed on your system.
Regarding issues in running a WPF application with a platform target of x86, you should provide more detail such as what error(s) you are seeing. There shouldn't be an issue here: Setting platform target to x86 on my x64 laptop prevent some WPF types to load properly
您是否尝试过将 Target Framework 从 4.0 更改为 3.5?因为我遇到了类似的问题,当我更改目标框架时,情况就很好了。
Have you tried to change Target Framework from 4.0 to 3.5? Cause I had a similar issue and when I changed target framework it was good.
我尝试在同一解决方案中创建一个新的 WPF 项目,现在它可以工作了。
有时我非常讨厌代码... =P
不过还是感谢您的帮助!
如果这种事情再次发生,我会睁大眼睛。
I tried creating a new WPF project in the same solution, and now it works.
Sometimes i hate code with a passion... =P
Thanks for your help though!
I will keep my eyes open if this thing happens again.