-1

I am trying to update the band Me Tile image from a background task and i get the following error at WriteableBitmap:

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
   at Windows.UI.Xaml.Media.Imaging.WriteableBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight)
   at BackgroundTaskCS.UpdateBandTask.<LoadImage>d__f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

It looks like the WriteableBitmap needs to run on the UI Thread but I don't think I have access to UI thread since I need to update the tile without the app being open. Is there an alternative solution to this?

Below is the task I am using. Thanks in advance.

I started this question here and was directed to stackoverflow because of the band.

public async void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

             await UpdateMeTile();
            Debug.WriteLine("CS Task Run");

            _deferral.Complete();
        }

        private static async Task UpdateMeTile()
        {

            /// <summary>
            /// Connect to Microsoft Band and change the wallpaper.
            /// </summary>

            try
            {
                // Get the list of Microsoft Bands paired to the phone.
                IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();
                if (pairedBands.Length < 1)
                {
                    return;
                }

                // Connect to Microsoft Band.
                using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
                {
                    // Change the wallpaper.
                    await bandClient.PersonalizationManager.SetMeTileImageAsync(await LoadImage("ms-appx:///Assets/SampleMeTileImage.jpg"));

                }

            }
            catch (Exception ex)
            {
                //this.textBlock.Text = ex.ToString();
                Debug.WriteLine("!!!!!!!!!!!!!!!!!!Exception. {0}", ex);
            }

        }


        private static async Task<BandImage> LoadImage(string uri)
        {
            StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));

            using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
            {

                WriteableBitmap bitmap = new WriteableBitmap(1, 1);
                await bitmap.SetSourceAsync(fileStream);
                bitmap.ToBandImage();
                return bitmap.ToBandImage();
            }
        }
4

1 回答 1

0

正如 Rob Caplan 在原始 MSDN 论坛帖子中所提到的,请务必从XamlRenderingBackgroundTask派生您的后台任务。这样做将确保您的后台任务的 OnRun() 方法将在“UI 线程”上调用(为了使用 UI 相关类型,例如 WriteableBitmap)。

于 2015-05-15T20:24:56.743 回答