WP8.1 C#Windows Phone 8.1后台任务WriteableBitmap线程错误 [英] WP8.1 C# Windows Phone 8.1 Background Task WriteableBitmap Thread Error

查看:120
本文介绍了WP8.1 C#Windows Phone 8.1后台任务WriteableBitmap线程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从后台任务更新乐队Me Tile图像,并且在WriteableBitmap上出现以下错误:

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)

WriteableBitmap似乎需要在UI线程上运行,但是我认为我无权访问UI线程,因为我需要在不打开应用程序的情况下更新磁贴.有替代解决方案吗?

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();
            }
        }

推荐答案

正如Rob Mslan在原始MSDN论坛帖子中提到的,请确保从

As mentioned by Rob Caplan in the original MSDN forum post, be sure to derive your background task from XamlRenderingBackgroundTask. Doing so will ensure that your background task's OnRun() method will be called on a "UI thread" (for the purposes of using UI-related types such as WriteableBitmap).

这篇关于WP8.1 C#Windows Phone 8.1后台任务WriteableBitmap线程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆