使用Microsoft Band SDK读取和修改Microsoft Band的主磁贴背景图像

导入命名空间

Visual Basic.NET代码:

Imports Microsoft.Band
Imports Microsoft.Band.Tiles
Imports Microsoft.Band.Admin
Imports Microsoft.Band.Personalization
Imports System.IO
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports Microsoft.WindowsAPICodePack.Dialogs

通用变量

通过Microsoft.Band.IBandClient类型或Microsoft.Band.Admin.ICargoClient类型的变量BandClient,表示一个已经连接的Microsoft Band对象。

该对象通过BandConnectionManager模块封装。

Imports Microsoft.Band
Imports Microsoft.Band.Tiles
Imports Microsoft.Band.Admin
Imports Microsoft.Band.Personalization
Module BandConnectionManager
    Public BandClient As ICargoClient
    Public BandClientLimited As IBandClient
    Public CurrentBandClass As BandClass = BandClass.Unknown
End Module

读取Microsoft Band的主磁贴背景图像

通过Microsoft.Band.Admin.ICargoClient.GetMeTileImageAsync()Microsoft.Band.IBandClient.PersonalizationManager.GetMeTileImageAsync()方法,读取Microsoft.Band.Personalization.BandImage类型的主磁贴(Me Tile)背景图像。Microsoft.Band.Personalization.BandImage提供了ToWriteableBitmap()方法,将Microsoft.Band.Personalization.BandImage转换为System.Windows.Media.Imaging.WriteableBitmap

Visual Basic.NET代码:

Try
    Dim BandImage As BandImage
    BandImage = Await BandClient.PersonalizationManager.GetMeTileImageAsync()
    Dim BandImageBitmap As WriteableBitmap
    BandImageBitmap = BandImage.ToWriteableBitmap()
    CurrentBandMeTileImage = BandImageBitmap
    imgMeTileImage.Source = BandImageBitmap
    imgMeTileImage.Tag = "CurrentBandMeTileImage"
Catch ex As Exception
    MessageBox.Show("試圖獲取主動態磚背景圖片時發生例外情況: " & ex.Message, "錯誤", MessageBoxButton.OK, MessageBoxImage.Error)
    imgMeTileImage.Source = Nothing
    CurrentBandMeTileImage = Nothing
    imgMeTileImage.Tag = ""
End Try

设置Microsoft Band的主磁贴背景图像

对于Microsoft Band第一代而言,该设备需要将传入的图像修改为长310像素、宽(高)102像素的尺寸,且需使用Pbgra32进行像素编码。System.Windows.Media.Imaging.WriteableBitmap提供了ToBandImage()方法,将System.Windows.Media.Imaging.WriteableBitmap转换为Microsoft.Band.Personalization.BandImage

Visual Basic.NET代码:

Private Async Sub btnSetMeTileImage_Click(sender As Object, e As RoutedEventArgs) Handles btnSetMeTileImage.Click
    'Code adapted from unBand
    Try
        Dim BitmapSource As New BitmapImage()
        BitmapSource = imgMeTileImage.Source
        BitmapSource.DecodePixelHeight = 102
        BitmapSource.DecodePixelWidth = 310
        Dim Pbgra32Image = New FormatConvertedBitmap(BitmapSource, PixelFormats.Pbgra32, Nothing, 0)
        Dim NewMeTileImage As New WriteableBitmap(Pbgra32Image)
        Dim NewBandMeTileImage As BandImage = NewMeTileImage.ToBandImage()
        Await BandClient.PersonalizationManager.SetMeTileImageAsync(NewBandMeTileImage)
    Catch ex As Exception
        MessageBox.Show("試圖設置主動態磚背景圖片時發生例外情況: " & ex.Message, "錯誤", MessageBoxButton.OK, MessageBoxImage.Error)
    End Try
    Try
        Dim BandImage As BandImage
        BandImage = Await BandClient.PersonalizationManager.GetMeTileImageAsync()
        Dim BandImageBitmap As WriteableBitmap
        BandImageBitmap = BandImage.ToWriteableBitmap()
        CurrentBandMeTileImage = BandImageBitmap
        imgMeTileImage.Source = BandImageBitmap
        imgMeTileImage.Tag = "CurrentBandMeTileImage"
    Catch ex As Exception
        MessageBox.Show("試圖獲取主動態磚背景圖片時發生例外情況: " & ex.Message, "錯誤", MessageBoxButton.OK, MessageBoxImage.Error)
        imgMeTileImage.Tag = ""
    End Try
End Sub
it
除非特别注明,本页内容采用以下授权方式: Creative Commons Attribution-ShareAlike 3.0 License