UwpDesktop!WPF也能開發Surface Dial


前段時間巨硬發布了一款新的輸入設備Surface Dial,配合Surface Studio使用簡直炫酷到沒朋友。

本人由於公司業務有幸參與了微軟的相關培訓,最大的收獲覺得是發現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大家自己研究下,也希望大家留言評論解答我的疑惑

鏈接: http://pan.baidu.com/s/1boJrIbH 密碼: h722




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM