zoukankan      html  css  js  c++  java
  • uwp 的锁屏功能

    【UWP开发】自定义锁屏&桌面壁纸

    mtobeiyf
    2015.11.01 00:16:55字数 394阅读 1,249

    调用通用的API来设置桌面壁纸,可以实现很多有趣的功能。
    在Windows通用平台中,可以使Windows.System.UserProfile命名空间下的类UserProfilePersonalizationSettings来对系统的开始界面背景和锁屏壁纸进行操作。它可以修改锁屏壁纸和桌面壁纸,调用后会返回bool值,如果成功就是true,否则返回false。
    在调用UserProfilePersonalizationSettings类前,先访问一下IsSupported方法,看看是否支持该操作。然后通过Current属性可以获取到一个UserProfilePersonalizationSettings实例,之后你就可以设置壁纸了。
    调用TrySetLockScreenImageAsync方法设置锁屏壁纸,调用TrySetWallpaperImageAsync方法可以设置桌面壁纸,参数都是用来作为背景的图片文件的StorageFile

     
     

    下面通过实例来演示一下如何实现这个功能。

    因为UserProfilePersonalizationSettings被封装在Windows.System.UserProfile中,先引用

    using Windows.System.UserProfile
    

    设置壁纸

    UserProfilePersonalizationSettings setting 
      = UserProfilePersonalizationSettings.Current;
      //实例化对象            
    bool b = await setting.TrySetLockScreenImageAsync(file);
    

    其中TrySetLockScreenImageAsync(file)是尝试设置文件对象file为锁屏壁纸。

    在测试中,我是直接在项目根目录下添加了一个pic.jpg的1280*720的图片文件。(其实应该放Assets里面的)

    那么如何导入图片文件呢?
    需要用到StorageFileUri
    在Class下定义:

    private static Uri imgUri = new Uri("ms-appx:///pic.jpg");
    

    这样就有个Uri指向根目录下的pic.jpg文件。

    接下来要用到StorageFile来导入文件。

    还是要先引用

    using Windows.Storage;
    

    从imgUri导入文件,注意要用到异步

    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
    

    下面贴上主要源码。

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        
        private static Uri imgUri = new Uri("ms-appx:///pic.jpg");
        
        private async void button_Click(object sender, RoutedEventArgs e)
        {
            var msg = new MessageDialog("");
            if (!UserProfilePersonalizationSettings.IsSupported())
            {
                var mess = new MessageDialog("人品太差,不支持哦!");
                await mess.ShowAsync();
            }
            //获取文件
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
            //设置背景
            UserProfilePersonalizationSettings setting = UserProfilePersonalizationSettings.Current;        
            bool b = await setting.TrySetLockScreenImageAsync(file);
        }
    }

    实际  返回false

    C# code

    ----------------------------------------------

    private static Uri imgUri = new Uri("ms-appx:///Assets/pic.jpg");

            private async void Button_Click_1(object sender, RoutedEventArgs e)

            {

                var msg = new MessageDialog("");

                if (!UserProfilePersonalizationSettings.IsSupported())

                {

                    var mess = new MessageDialog("人品太差,不支持哦!");

                    await mess.ShowAsync();

                }

                img.Source = new BitmapImage(new Uri("ms-appx:///pic.jpg", UriKind.RelativeOrAbsolute));

                //获取文件

                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///pic.jpg", UriKind.RelativeOrAbsolute));

                //设置背景

                UserProfilePersonalizationSettings setting = UserProfilePersonalizationSettings.Current;

                bool b = await setting.TrySetLockScreenImageAsync(file);

                if (b)

                {

                    msg.Content = "set ok!";

                    await msg.ShowAsync();

                }

                else

                {

                    msg.Content = "can not set lock screen!";

                    await msg.ShowAsync();

                }

            }

  • 相关阅读:
    Codeforces Round 546 (Div. 2)
    Codeforces Round 545 (Div. 2)
    Codeforces Round 544(Div. 3)
    牛客小白月赛12
    Codeforces Round 261(Div. 2)
    Codeforces Round 260(Div. 2)
    Codeforces Round 259(Div. 2)
    Codeforces Round 258(Div. 2)
    Codeforces Round 257 (Div. 2)
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14259320.html
Copyright © 2011-2022 走看看