LockScreen APIs in Windows 8 CP

因为时间问题,我只仓促的把我在google blog上写的原稿粘贴在这,而没有翻译,相信大家能看得明白,如有疑问可留言。

http://sdrv.ms/Kez9H1

I have not found a sample code from MSDN documents, so I wrote some test codes for research your this problem.

Since I'm not very familiar with Windows 8 new APIs and the Metro Style Application project dev, so I just try to make theWindows Form Application project can demonstrate the answer to you.

And now, I think my demo code can prove the document content:

            //GetImageStream Remarks: http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.userprofile.lockscreen.getimagestream.aspx
            //This method can be called only by apps that have set the "Picture Library Access" capability in the package manifest
            //**or bythe app that set this image on the lock screen.

            //OriginalImageFile Remarks: http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.userprofile.lockscreen.originalimagefile
            //This property retrieves only files. 
            //**If the image was set through a stream, this call will return E_FILE_NOT_FOUND.

using System;
using System.IO;
using System.Windows.Forms;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System.UserProfile;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IRandomAccessStream picStream = null;
        private async void BtnSetLockScreenImage_Click(object sender, EventArgs e)
        {
            await SetLockScreenImage();

            this.richTextBox1.Text = "AccountPictureChangeEnabled: " + Windows.System.UserProfile.UserInformation.AccountPictureChangeEnabled +
                "\nNameAccessAllowed: " + Windows.System.UserProfile.UserInformation.NameAccessAllowed;
        }

        private async System.Threading.Tasks.Task SetLockScreenImage()
        {
            StorageFolder storageFolder = KnownFolders.PicturesLibrary;
            StorageFile sampleFile = await storageFolder.GetFileAsync("abc.png");
            picStream = await sampleFile.OpenAsync(FileAccessMode.Read);
            await Windows.System.UserProfile.LockScreen.SetImageStreamAsync(picStream);
            picStream.Dispose();
        }

        private void BtnSaveAndRead_Click(object sender, EventArgs e)
        {
            SaveLockScreenImage();
            this.pictureBox1.ImageLocation = "1.png";
        }

        private void SaveLockScreenImage()
        {
            //GetImageStream Remarks: http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.userprofile.lockscreen.getimagestream.aspx
            //This method can be called only by apps that have set the "Picture Library Access" capability in the package manifest 
            //**or by the app that set this image on the lock screen.
            using (IRandomAccessStream imageStream = Windows.System.UserProfile.LockScreen.GetImageStream())
            {
                SaveStreamToFile(imageStream);
            }
        }
        DataReader dr;
        private void SaveStreamToFile(IRandomAccessStream stream)
        {
            try
            {
                dr = new DataReader(stream.GetInputStreamAt(0));
                DataReaderLoadOperation drlo = dr.LoadAsync((uint)stream.Size);
                drlo.Completed = new AsyncOperationCompletedHandler<uint>(SaveToFileCompleted);
            }
            catch (Exception)
            {
            }
        }
        void SaveToFileCompleted(IAsyncOperation<uint> asyncInfo, AsyncStatus asyncStatus)
        {
            if (asyncStatus == AsyncStatus.Completed)
            {
                uint size = asyncInfo.GetResults();
                byte[] bytesArray = new byte[size];
                dr.ReadBytes(bytesArray);
                FileStream fs = new FileStream("1.png", FileMode.Create);
                fs.Write(bytesArray, 0, bytesArray.Length);
                fs.Flush();
                fs.Close();
                dr.Dispose();
            }
        }

        private void BtnGetOriginalImageFile_Click(object sender, EventArgs e)
        {
            //OriginalImageFile Remarks: http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.userprofile.lockscreen.originalimagefile
            //This property retrieves only files. 
            //**If the image was set through a stream, this call will return E_FILE_NOT_FOUND.

            //So we can use the More PC Settings or the SetImageFileAsync function to set the image, and then call this method to get the image file.
            try
            {
                this.pictureBox2.ImageLocation = Windows.System.UserProfile.LockScreen.OriginalImageFile.AbsolutePath;
            }
            catch(FileNotFoundException) 
            {
                MessageBox.Show("If the image was set through a stream, this call will return E_FILE_NOT_FOUND."
                    + "/nPlease use SetImageFileAsync");
            }
        }

        private async void BtnSetImageFileAsync_Click(object sender, EventArgs e)
        {
            StorageFolder storageFolder = KnownFolders.PicturesLibrary;
            StorageFile sampleFile = await storageFolder.GetFileAsync("abc.png");
            await LockScreen.SetImageFileAsync(sampleFile);
        }
    }
}

 

I put my image abc.png into the Picture library do the test.

[Edit:

Build the project to x64 once you are on x64 Windows 8 CP.]

About the x64 problem was found after my friend asked me the same question on his x64 System, and we use the Process Monitor to capture what the API did, and there's a registry key is not in the 32bit path under x64 System, so we need build the application to x64 then run, it works. Since that key is undocumented, it is hard to say if this is a issue or just the engineer design it like this.(So I just do not mentioned that registry key name here, you can find it according to my way.)

挂上VPN就可以访问我的英文blog了: http://mikedoszhang.blogspot.com/2012/05/lockscreen-apis-in-windows-8-cp.html

posted @ 2012-05-29 17:55  Mike Dos Zhang  阅读(438)  评论(0编辑  收藏  举报