本人由于公司业务有幸参与了微软的相关培训,最大的收获觉得是发现wpf居然也可以开发dial, wpf居然可以使用uwp的api!
不卖关子,关键就是名为“uwpdesktop”的一个nuget,在我们的wpf程序中添加这个nuget就可以了。上一篇文章中写的wpf起调uwp也借助了这个nuget。
由于我们的现有解决方案是拿wpf做的,所以培新间歇我直奔主题,就问老外这个东西滋不滋磁wpf,得到的答案是肯定的。那怎么用呢,有没有demo呢,还真有,那再麻烦您拷给我吧,,,于是有了下面的demo
我还是直接贴代码吧,
public mainwindow() { initializecomponent();//setradialcontrollerconfiguration();var interop = (iradialcontrollerinterop)system.runtime.interopservices.windowsruntime.windowsruntimemarshal .getactivationfactory(typeof(radialcontroller)); guid guid = typeof(radialcontroller).getinterface(iradialcontroller).guid; window window = window.getwindow(this);var wih = new windowinterophelper(window); intptr hwnd = wih.handle;// create a reference to the radialcontroller.radialcontroller = interop.createforwindow(hwnd, ref guid);string iconfilename = assets\\dial_icon_custom_item.png;string filepath = system.io.path.combine(appdomain.currentdomain.basedirectory, iconfilename);var getitemimageoperation = storagefile.getfilefrompathasync(filepath); getitemimageoperation.completed += new asyncoperationcompletedhandler<storagefile>(addmenuitemfromimage);// declare input handlers for the radialcontroller.radialcontroller.rotationchanged += radialcontroller_rotationchanged; ; }
还有个很关键的类radialcontrollerinterfaces.cs
[system.runtime.interopservices.guid(787cdaac-3186-476d-87e4-b9374a7b9970)] [system.runtime.interopservices.interfacetype(system.runtime.interopservices.cominterfacetype.interfaceisiinspectable)]interface iradialcontrollerconfigurationinterop { radialcontrollerconfiguration getforwindow(intptr hwnd, [system.runtime.interopservices.in]ref guid riid); } [system.runtime.interopservices.guid(1b0535c9-57ad-45c1-9d79-ad5c34360513)] [system.runtime.interopservices.interfacetype(system.runtime.interopservices.cominterfacetype.interfaceisiinspectable)]interface iradialcontrollerinterop { radialcontroller createforwindow(intptr hwnd, [system.runtime.interopservices.in]ref guid riid); }
这个类提供了createforwindow方法可以让我们创建radialcontroller对象,个人觉得这个接口很玄乎,上边的guid特性(这个是叫特性吧?)是写死的,问了工程师说这个不用改,这不就很坑了,如果不是有个demo,我怎么知道怎么写,,,
作为弱鸡,这里我也有问题请教大家:
1.这些特性的作用,平时自己写代码貌似只用过【datacontract】
[system.runtime.interopservices.guid(1b0535c9-57ad-45c1-9d79-ad5c34360513)] [system.runtime.interopservices.interfacetype(system.runtime.interopservices.cominterfacetype.interfaceisiinspectable)]
2.mainwindow中代码
var interop = (iradialcontrollerinterop)system.runtime.interopservices.windowsruntime.windowsruntimemarshal .getactivationfactory(typeof(radialcontroller));
总觉得阅读性这么差呢,看不懂,或许是我从未见过,对这个方法不熟?
微软工程师给我的这个demo我发现有两个bug(他本人也表示还没写完,是dirty demo),一个是setradialcontrollerconfiguration这个方法里异常,不能用,导致不能初始化dial的菜单,每次启动应用都会往菜单里
添加一项;另一个是焦点问题,当长按dial选择了其他菜单,例如音量,再返回我们的demo,发现此时dial仍然控制的是音量
由于本人太弱,就不多说了,末尾附上dr.neil的demo大家自己研究下,也希望大家留言评论解答我的疑惑
链接: 密码: h722
以上就是wpf开发surface dial实例教程的详细内容。
