using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.System.UserProfile;
using System.Threading.Tasks;
using Windows.Storage;
namespace test_set_Desktop_Background
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
//<
get Button >
Button btn = sender as Button;
string iFile = btn.Content.ToString();
//</
get Button >
//<
change Desktop Background >
bool ok = await
SetWallpaperAsync("desktop_Background_0" + iFile + ".jpg");
//</
change Desktop Background >
}
async Task<bool>
SetWallpaperAsync(string local_Background_File)
{
//------------<
SetWallpaperAsync() >------------
//*reference:
https://docs.microsoft.com/en-us/uwp/api/windows.system.userprofile.userprofilepersonalizationsettings
bool success = false;
if (UserProfilePersonalizationSettings.IsSupported())
{
//----< Profile Setting allowed >----
//< get File >
var uri = new Uri(this.BaseUri,
"Assets/Backgrounds/" + local_Background_File);
//"ms-appx:///Local/" + localAppDataFileName);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
//</ get File >
//< set profile >
UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
//</ set profile >
//< change Background >
success = await profileSettings.TrySetWallpaperImageAsync(file);
//</ change Background >
//----</ Profile Setting allowed >----
}
//<
result >
return success;
//</
result >
//------------</
SetWallpaperAsync() >------------
}
}
}
|