C#代码更改锁屏并将文件分配给IStorage [英] C# code to change lockscreen and assign file to IStorage

查看:70
本文介绍了C#代码更改锁屏并将文件分配给IStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从wpf应用程序更改Windows 8锁屏。



我想出了从.netcore使用lockscreen类的方法,但这些方法需要IStorage作为参数。

我是c#的新手,我不知道如何将文件分配给Istorage。



如何加载文件Istorage变量???



谢谢

Hi I want to change Windows 8 lockscreen from wpf application.

I figured out way to use lockscreen class from .netcore but those methods take IStorage as arguments.
I am new to c# and I dont know how to assign files to Istorage.

How can I load file in Istorage variable???

Thank You

推荐答案

MSDN页面 [ ^ ]。只需下载C#源代码即可开始使用。



为了在链接失败时保留可用内容我给你写了一个快速示例 - 调整它为你需要自己。



There is example code available at the MSDN page[^]. Just download the C# source and give it a go.

In order to keep the stuff availabe when the Link should fail I wrote you a quick example up - Adjust it for yourself as ever needed.

using SDKTemplate;
using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.System.UserProfile;

private async void SetLockScreen()
{
    rootPage.NotifyUser("", NotifyType.StatusMessage); 
    FileOpenPicker imagePicker = new FileOpenPicker 
    { 
       ViewMode = PickerViewMode.Thumbnail, 
       SuggestedStartLocation = PickerLocationId.PicturesLibrary, 
       FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" } 
     }; 
 
     StorageFile imageFile = await imagePicker.PickSingleFileAsync(); 
     if (imageFile != null) 
     { 
         try 
         { 
            // Application now has access to the picked file, setting image to lockscreen.  This will fail if the file is an invalid format. 
            await LockScreen.SetImageFileAsync(imageFile); 
 
            // Retrieve the lock screen image that was set 
            IRandomAccessStream imageStream = LockScreen.GetImageStream(); 
            if (imageStream != null) 
            { 
               BitmapImage lockScreen = new BitmapImage(); 
               lockScreen.SetSource(imageStream); 
               LockScreenImage.Source = lockScreen; 
               LockScreenImage.Visibility = Visibility.Visible; 
             } 
             else 
             { 
               LockScreenImage.Visibility = Visibility.Collapsed; 
               rootPage.NotifyUser("Setting the lock screen image failed.  Make sure your copy of Windows is activated.", NotifyType.StatusMessage); 
             } 
           } 
           catch (Exception) 
           { 
               LockScreenImage.Visibility = Visibility.Collapsed; 
               rootPage.NotifyUser("Invalid image selected or error opening stream.", NotifyType.ErrorMessage); 
           } 
    } 
    else 
    { 
         LockScreenImage.Visibility = Visibility.Collapsed; 
         rootPage.NotifyUser("No file was selected using the picker.", NotifyType.StatusMessage); 
    }
}









请注意,使用新Win 8 API的Windows 8应用程序只能访问计算机上先前定义的位置列表。可以在 MSDN [ ^ ]。无论如何,如果您现在想要打开位于应用程序基目录中的文件,则需要使用以下行替换文件夹选择器:





Please not that Windows 8 Apps which are using the new Win 8 API are only capable of accesing a previously defined list of locations on your computer. The list of currently accesible directories can be found at the MSDN[^]. Whatsoever, if you now would like to open a file located in your applications base directory, you would need to replace the folder picker with the following lines:

StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile file = await storageFolder.GetFileAsync("yourdesiredimage.jpg");





这会让你的代码看起来像这样:





this would lead to let your code look somewhat like this:

using SDKTemplate;
using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.System.UserProfile;

private async void SetLockScreen()
{
    rootPage.NotifyUser("", NotifyType.StatusMessage); 
    StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
    StorageFile imageFile= await storageFolder.GetFileAsync("yourdesiredimage.jpg");
 
     if (imageFile != null) 
     { 
         try 
         { 
            // Application now has access to the picked file, setting image to lockscreen.  This will fail if the file is an invalid format. 
            await LockScreen.SetImageFileAsync(imageFile); 
 
            // Retrieve the lock screen image that was set 
            IRandomAccessStream imageStream = LockScreen.GetImageStream(); 
            if (imageStream != null) 
            { 
               BitmapImage lockScreen = new BitmapImage(); 
               lockScreen.SetSource(imageStream); 
               LockScreenImage.Source = lockScreen; 
               LockScreenImage.Visibility = Visibility.Visible; 
             } 
             else 
             { 
               LockScreenImage.Visibility = Visibility.Collapsed; 
               rootPage.NotifyUser("Setting the lock screen image failed.  Make sure your copy of Windows is activated.", NotifyType.StatusMessage); 
             } 
           } 
           catch (Exception) 
           { 
               LockScreenImage.Visibility = Visibility.Collapsed; 
               rootPage.NotifyUser("Invalid image selected or error opening stream.", NotifyType.ErrorMessage); 
           } 
    } 
    else 
    { 
         LockScreenImage.Visibility = Visibility.Collapsed; 
         rootPage.NotifyUser("No file was selected using the picker.", NotifyType.StatusMessage); 
    }
}





在回复此解决方案的评论时,使用硬编码的完整路径作为 C:\ Temp \Img.jpeg被认为是错误的编码实践,应该尽可能避免。特别是Windows 8,还有Windows 7和Vista有一种方法可以通过使用框架枚举或直接访问本地计算机变量(例如%APPDATA%)直接访问文件夹来获取文件夹的路径。





In reply to a comment on this solution, using hard-coded full paths as "C:\Temp\Img.jpeg" is considered bad coding practice and should be avoided whenever possible. Especially Windows 8, but also Windows 7 and Vista have a way to directly access a folder by either using a framework enumeration or accessing local computer variables (such as %APPDATA%) directly to get the path of a folder.


这篇关于C#代码更改锁屏并将文件分配给IStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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