【问题标题】:wallpaper function in Xamarin.Android. How does is work and how to use it?Xamarin.Android 中的壁纸功能。它是如何工作的以及如何使用它?
【发布时间】:2020-03-20 17:30:27
【问题描述】:

在 UWP 应用程序中,我可以使用此代码将 StorageFile 设置为桌面和锁屏的设备壁纸:

public static async System.Threading.Tasks.Task ApplyWallpaper(StorageFile storageFile)
{
   await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(storageFile);
   await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(storageFile);
}

寻找 Xamarin.Android 我找到了 WallpaperManager,但我真的不知道如何准确地使用它。

static WallpaperManager myWallpaperManager;
public static void ApplyToDesktop(Bitmap bitmap)
{
   myWallpaperManager.SetBitmap(bitmap);
}

你能告诉我 Xamarin.Android 的 UWP 代码等效吗?

【问题讨论】:

标签: c# android xamarin bitmap wallpaper


【解决方案1】:

首先,在AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

然后,设置壁纸,sample 这里是Drawable 文件夹中的图片名称:

public void setWallpaper() {

    Bitmap bitmap = BitmapFactory.DecodeResource(Resources,Resource.Drawable.sample);

    WallpaperManager manager = WallpaperManager.GetInstance(ApplicationContext);

    manager.SetBitmap(bitmap);
    //or
    manager.SetBitmap(bitmap, null, true, WallpaperManagerFlags.Lock);

}

我上传了一个示例项目here,你可以查看。

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 2016-07-24
    • 2015-04-04
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多