close

此篇文章就講兩件事

1.怎麼Run msft釋出的UWP sensor sample程式 (以Light Sensor為例)

2.怎麼Run Windows.Devices.Sensors on WPF

若不符合需求可直接跳過本文

 

 

如何執行 msft sensor sample

 

Universal Windows Platform (UWP) app samples  裡面有很多UWP 獲取sensor data的sample

我選了LightSensor的來用。打開專案build 成功之後,需要產出package才有辦法把這個UWP安裝在機台上

產出package步驟:

image

 

取消勾選Enable automatic updates

image

 

依照自己的需求設定然後按Create,

image

 

把產出的package整包複製,(這裡面有兩個資料夾是因為我有產生過兩次package)

image

 

 

Add-AppDevPackage.ps1上按右鍵Run with PowerShell,

image

 

 

應該就可以看到程式出現在程式列表(要等個幾秒才會出現,不要急)

image

 

Run with PowerShell 若遇到下面的Error

----------------------------------------------------------------------------------------------------

Found bundle: F:\LightSensor\cs\AppPackages\LightSensor_1.0.0.0_Debug_Test\LightSensor_1.0.0.0_x86_x64_arm_Debug.appxbundle

Installing app...
Found dependency package(s):
F:\LightSensor\cs\AppPackages\LightSensor_1.0.0.0_Debug_Test\Dependencies\x86\Microsoft.NET.CoreRuntime.1.1.appx
F:\LightSensor\cs\AppPackages\LightSensor_1.0.0.0_Debug_Test\Dependencies\x86\Microsoft.VCLibs.x86.Debug.14.00.appx
F:\LightSensor\cs\AppPackages\LightSensor_1.0.0.0_Debug_Test\Dependencies\x64\Microsoft.NET.CoreRuntime.1.1.appx
F:\LightSensor\cs\AppPackages\LightSensor_1.0.0.0_Debug_Test\Dependencies\x64\Microsoft.VCLibs.x64.Debug.14.00.appx
Add-AppxPackage : Deployment failed with HRESULT: 0x80073CFD, A Prerequisite for an install could not be satisfied.

Unspecified error


NOTE: For additional information, look for [ActivityId] 07cf4434-0386-0002-6d64-cf078603d601 in the Event Log or use
the command line Get-AppPackageLog -ActivityID 07cf4434-0386-0002-6d64-cf078603d601

At F:\LightSensor\cs\AppPackages\LightSensor_1.0.0.0_Debug_Test\Add-AppDevPackage.ps1:388 char:13
+             Add-AppxPackage -Path $DeveloperPackagePath.FullName -Dep ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (F:\LightSensor\...ebug.appxbundle:String) [Add-AppxPackage], Exception
    + FullyQualifiedErrorId : DeploymentError,Microsoft.Windows.Appx.PackageManager.Commands.AddAppxPackageCommand

Error: Could not install the app.
Press Enter to continue...:

----------------------------------------------------------------------------------------------------

這是因為機台OS版號較舊的緣故。

請用msinfo32檢查你機台的os版號為何

然後去Project的Properties把Min Version改成機台版號再產生一次package,用這包安裝就沒問題了。

image

 

執行程式之後按Enable就可以得到light-sensor的值(若機台沒有light sensor會顯示在程式UI最下列)

 

 

如何在WPF使用 Windows.Devices.Sensors namespace

 程式列表裡面查SDK版本為何

image

 

 開啟Poject->Unload Project   ->  Edit SensorSample.csproj -> 加入TargetPlatformVersion

 版號填查到的SDK版本,若不符合,下一步將會失敗。

image

 

-> 重開Project and load project  會看到多一個Windows選項,此時程式就可以用Windows.devices.sensor namespace囉

image

 

若有No references were found in the windows sdk的問題

enter image description here

請參考這個解法

Copy WinMetadata folder from C:\WINDOWS\System32\WinMetadata to C:\WINDOWS\SysWOW64\WinMetadata.

and then re-open reference manager.

(詳細討論可參考 https://stackoverflow.com/questions/56560024/no-references-were-found-in-the-windows-sdk-windows-1903)

 

->實作:每秒印出LUX 到Log.txt裡面

public MainWindow()
        {
            InitializeComponent();


            LightSensor lightSensor = LightSensor.GetDefault();

            if (lightSensor != null)

            {
                while (true) {
                    Thread.Sleep(1000);
                    LightSensorReading reading = lightSensor.GetCurrentReading();
                    txtLuxValue.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
                    using (StreamWriter outputFile = new StreamWriter(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Log.txt"), true))
                    {
                        outputFile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "LUX="+ txtLuxValue.Text);
                    }
                }
            }
            else
            {
                using (StreamWriter outputFile = new StreamWriter(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Log.txt"), true))
                {
                    outputFile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "No light sensor found");
                }
            }
        }

 

參考資料

1.    裡面有簡單使用Windows.devices.sensor namespace 的程式碼 https://geeks.ms/jyeray/2012/07/

2.    Send a local toast notification from desktop C# apps

https://docs.microsoft.com/en-US/windows/uwp/design/shell/tiles-and-notifications/send-local-toast-desktop

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 iam9527 的頭像
    iam9527

    翁丸的育兒筆記

    iam9527 發表在 痞客邦 留言(0) 人氣()