#

UWP: Hintergrund von Windows 10 Desktop ändern

 

Ein C# Code-Beispiel für Windows UWP, mit dem man ganz einfach den Hintergrund von Windows 10 ändern kann.

 

Betrifft: UWP Universal Windows Platform App

Windows 10

TrySetWallpaperImageAsync

 

Video Tutorial:

 

Die Änderung des Hintergrunds wird mit einem einzigen Befehl ausgeführt

//< change Background >

profileSettings.TrySetWallpaperImageAsync(file);

//</ change Background >

 

 

Unter Visual Studio:

 

 

Im Beispiel wird der Hintergrund vom Windows 10 Desktop ausgewechsel wenn man auf einen der 5 Buttons klickt:

 

Hintergrund Windows 10 Desktop bei Button 3

Hintergrund Windows 10 Desktop bei Button 3

 

Hintergrund Windows 10 Desktop bei Button 4

 

Code als Beispiel

C# Code des MainPage.xaml.cs

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() >------------

        }

    }

}

 

 

 

Xaml

<Page

    x:Class="test_set_Desktop_Background.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:test_set_Desktop_Background"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" Height="271" Width="354" >

 

    <Grid >

        <Button Content="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Button_Click" Height="43" Width="134"/>

        <Button Content="2" HorizontalAlignment="Left" Margin="10,58,0,0" VerticalAlignment="Top" Click="Button_Click" Width="134"/>

        <Button Content="3" HorizontalAlignment="Left" Margin="10,95,0,0" VerticalAlignment="Top" Click="Button_Click" Width="134"/>

        <Button Content="4" HorizontalAlignment="Left" Margin="10,132,0,0" VerticalAlignment="Top" Click="Button_Click" Width="134"/>

        <Button Content="5" HorizontalAlignment="Left" Margin="10,169,0,0" VerticalAlignment="Top" Click="Button_Click" Width="134"/>

    </Grid>

</Page>

 

 

Mobile

.

123movies