从BackgroundMediaPlayer更新UWP LiveTile

时间:2016-08-23 14:49:13

标签: c# uwp live-tile background-audio

我正在使用NotificationsExtentions和NotificationsVisualizationLibrary来更新我的实时图块。我不知道如何在跟踪更改时从BackgroundMediaPlayer更新我的Live磁贴,因为我无法在此线程中使用Dispatcher.RunAsync,而且我的代码根本不会更改实时磁贴。 我的LiveTile XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<tile>
  <visual branding="nameAndLogo">
    <binding template="TileMedium" hint-textStacking="center">
      <image src="TileMediumImageSource" placement="peek" hint-crop="circle" />
      <text hint-style="base" hint-align="center">TileMediumtext</text>
      <text hint-style="captionSubtle" hint-align="center">TileMediumSubText</text>
    </binding>

    <binding template="TileWide">
      <group>
        <subgroup hint-weight="33">
          <image src="TileWideImageSource" hint-crop="circle" />
        </subgroup>
        <subgroup hint-textStacking="center">
          <text hint-style="title">TileWideText</text>
          <text hint-style="subtitleSubtle">TileWideSubText</text>
        </subgroup>
      </group>
    </binding>

    <binding template="TileLarge" hint-textStacking="center">
      <group>
        <subgroup hint-weight="1" />
        <subgroup hint-weight="2">
          <image src="TileLargeImageSource" hint-crop="circle" />
        </subgroup>
        <subgroup hint-weight="1" />
      </group>
      <text hint-style="title" hint-align="center">TileLargeText</text>
      <text hint-style="subtitleSubtle" hint-align="center">TileLargeSubText</text>
    </binding>
  </visual>
</tile>

这是我在BackgroundMedia中的C#代码:

public async void Run(IBackgroundTaskInstance taskInstance)
{
    //This function make background audio ready to go
    //Get deferal of background task and make BackgroundMedia ready to go
    _deferral = taskInstance.GetDeferral();
    #region _systemMediaTransportControl
    systemMediaControls = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
    systemMediaControls.IsEnabled = true;
    systemMediaControls.IsPlayEnabled = true;
    systemMediaControls.IsPauseEnabled = true;
    systemMediaControls.IsNextEnabled = false;
    systemMediaControls.IsFastForwardEnabled = false;
    systemMediaControls.IsPreviousEnabled = false;
    systemMediaControls.IsRewindEnabled = false;
    systemMediaControls.DisplayUpdater.Type = MediaPlaybackType.Music;
    #endregion
    systemMediaControls.ButtonPressed += SystemMediaControls_ButtonPressed;
    BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
    BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
    PlayList.CurrentItemChanged += PlayList_CurrentItemChanged;
    PlayList.ItemFailed += PlayList_ItemFailed;
    taskInstance.Canceled += TaskInstance_Canceled;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(await FileIO.ReadTextAsync(await StorageFile.GetFileFromApplicationUriAsync(
        new Uri("ms-appx:///LiveTile.xml"))));
    //Set Medium tile
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileMediumImageSource", "Assets/Square150x150Logo.scale-200.png"));
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileMediumtext", "4Khoune"));
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileMediumSubText", "NextGen live tile"));
    //Set Wide Tile 
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileWideImageSource", "Assets/Wide310x150Logo.scale-200.png"));
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileWideText", "4Khoune"));
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileWideSubText", "NextGen live tile"));
    //Set latge tile
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileLargeImageSource", "Assets/620x620.png"));
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileLargeText", "4Khoune"));
    xmlDoc.LoadXml(xmlDoc.GetXml().Replace("TileLargeSubText", "NextGen live tile"));

    var tup = TileUpdateManager.CreateTileUpdaterForApplication();
    tup.Update(new TileNotification(xmlDoc));

}

0 个答案:

没有答案