系统相关
首页 > 系统相关> > Windows Phone ScheduleAgent在设置的时间段内获取图像

Windows Phone ScheduleAgent在设置的时间段内获取图像

作者:互联网

我试图按照Windows Phone tutorial进行调整,以适应我的需求.我正在尝试每X秒抓取一张新图片(调试时用30张),并将其设置为新的锁屏背景.我创建了一个ScheduleAgent对象来调用它,但是,它似乎正在跳过一个函数.在MainPage.xaml.cs(运行完全相同的功能)中时,它不会跳过此操作.

ScheduleAgent.cs

        static ScheduledAgent()
    {
       ....<irrelevant code>....

    protected override void OnInvoke(ScheduledTask task)
    {

        .... < Code went here that shouldn't matter > ....
        }
        Debug.WriteLine("Imagename = " + imageName);


            DownloadImagefromServer("https://s3-us-west-2.amazonaws.com/qoutescreen/0" + imageName + ".jpg");

m.ReleaseMutex();
            }

        // If debugging is enabled, launch the agent again in one minute.
        // debug, so run in every 30 secs
        #if(DEBUG_AGENT)
        ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(30));
        System.Diagnostics.Debug.WriteLine("Periodic task is started again: " + task.Name);
        #endif

        // Call NotifyComplete to let the system know the agent is done working.
        NotifyComplete();
    }
    //===============================================================================
    private async void LockScreenChange(string filePathOfTheImage, bool isAppResource)
    {
        if (!LockScreenManager.IsProvidedByCurrentApplication)
        {
            // If you're not the provider, this call will prompt the user for permission.
            // Calling RequestAccessAsync from a background agent is not allowed.
            await LockScreenManager.RequestAccessAsync();
        }

        // Only do further work if the access is granted.
        if (LockScreenManager.IsProvidedByCurrentApplication)
        {
            // At this stage, the app is the active lock screen background provider.
            // The following code example shows the new URI schema.
            // ms-appdata points to the root of the local app data folder.
            // ms-appx points to the Local app install folder, to reference resources bundled in the XAP package
            var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
            var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);

            // Set the lock screen background image.
            LockScreen.SetImageUri(uri);

            // Get the URI of the lock screen background image.
            var currentImage = LockScreen.GetImageUri();
            System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString());                
        }
    }
            private async Task DownloadImagefromServer(string imgUrl)
    {
        Debug.WriteLine("Attempting to Get Image from Server...");
        WebClient client = new WebClient();

        try
        {
            // I GET A "Cannot await Void" problem here?!?!?
            //=================================================================
            var result = await client.OpenReadAsync(new Uri(imgUrl, UriKind.Absolute));
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(result);
            return result;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);

        }

        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        //client.OpenReadAsync(new Uri(imgUrl, UriKind.Absolute));
    }

    //------------------------------------------------------------------
    //THIS FUNCTION IS NEVER HIT WHEN RUN IN THE ScheduleAgent.cs CODE, HOWEVER
    // WHEN IT IS RUN INT HE MainPage.xaml.cs CODE (Which is a copy/paste) IT RUNS FINE
    // WHY IS IT NOT HITTING THIS? HOW CAN I FIX IT?
    //------------------------------------------------------------------
    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(e.Result);
        //img.Source = bitmap;

        // Create a filename for JPEG file in isolated storage.
        String tempJPEG = "DownloadedWalleper.jpg";

        // Create virtual store and file stream. Check for duplicate tempJPEG files.
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(tempJPEG))
            {
                myIsolatedStorage.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

            StreamResourceInfo sri = null;
            Uri uri = new Uri(tempJPEG, UriKind.Relative);
            sri = Application.GetResourceStream(uri);

            //BitmapImage bitmap = new BitmapImage();
            //bitmap.SetSource(sri.Stream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

            //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }
        LockScreenChange("DownloadedWalleper.jpg", false);
    }
}

解决方法:

我建议使用“ await”关键字.这将导致代码暂停并等待操作完成,而不必担心互斥或其他线程阻塞策略.

不幸的是,Windows Phone缺少WebClient上的非异步方法,因此有一些设置是从this answer偷来的.

To use async with WebClient, install the 07002 NuGet package, and then you can use OpenReadTaskAsync

private async void DownloadImagefromServer(string imgUrl)
{
    Debug.WriteLine("Attempting to Get Image from Server...");
    WebClient client = new WebClient();

    var result = await client.OpenReadTaskAsync(new Uri(imgUrl, UriKind.Absolute));

    BitmapImage bitmap = new BitmapImage();
    bitmap.SetSource(result);
    //img.Source = bitmap;

    ... rest of your code from OpenReadCompleted goes here
}

因此,发生的事情是执行在处理结果之前等待“ OpenRead”调用完成,然后返回控制.

标签:windows-phone-8,c
来源: https://codeday.me/bug/20191122/2060114.html