如何在 Xamarin 中设置持久的 Android 应用程序属性(例如,allowBackup)? [英] How to set persistent Android application attributes (eg. allowBackup) in Xamarin?

查看:41
本文介绍了如何在 Xamarin 中设置持久的 Android 应用程序属性(例如,allowBackup)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个不错的帖子(例如这个) 指示如何在 AndroidManifest.xml 中进行设置.

There are several good posts (eg this) instructing how to set this in AndroidManifest.xml.

但是,我发现 Xamarin (Visual Studio) 在构建时自动(重新)生成 AndroidManifest.xml 文件,使用 [Activity()] 属性(== Properties > AndroidManifest) 以生成清单的 标签+属性.

However, I find that Xamarin (Visual Studio) automatically (re-)generates the AndroidManifest.xml file at build time, using the [Activity()] attributes (== Properties > Android Manifest) to generate the manifest's <application> tag+attribs.

因此,如果您手动编辑 AndroidManifest.xml 以添加 android:allowBackup 属性,它会在重建时丢失.

Thus if you manually edit the AndroidManifest.xml to add the android:allowBackup attrib, it will get lost upon rebuild.

另外,[Activity()] 不支持 allowBackupfullBackupOnly,所以你不能通过它来设置它们.

Additionally, [Activity()] doesn't support allowBackup or fullBackupOnly, so you can't set them them via that.

有什么办法可以解决这个问题?
我如何设置 android:allowBackup 以在重建中幸存下来?

What's the solution to this?
How can I set android:allowBackup in a way that survives rebuild?

推荐答案

正如 SushiHangover 所说,我们可以使用 Application Element 来实现这一点.

Just as SushiHangover mentioned, we can use Application Element to achieve this.

Android 清单还为您提供了一种为整个应用程序声明属性的方法.这是通过元素及其对应物 Application 自定义属性完成的.例如,将以下 Application 属性添加到 AssemblyInfo.cs 以指示该应用程序可以调试,其用户可读名称为 My App,并且它使用 Theme.Light 样式作为所有活动的默认主题:

The Android manifest also provides a way for you to declare properties for your entire application. This is done via the element and its counterpart, the Application custom attribute. For example, the following Application attribute is added to AssemblyInfo.cs to indicate that the application can be debugged, that its user-readable name is My App, and that it uses the Theme.Light style as the default theme for all activities:

[assembly: Application (Debuggable=true,   
                        Label="My App",   
                        Theme="@android:style/Theme.Light")]

此外,Application 元素并不是配置 属性的唯一方法.或者,您可以将属性直接插入到 Properties/AndroidManifest.xml 元素中.这些设置合并到位于 obj/Debug/android/AndroidManifest.xml 中的最终 元素.

In addition, the Application element is not the only way to configure <application> attributes. Alternately, you can insert attributes directly into the <application> element of Properties/AndroidManifest.xml. These settings are merged into the final <application> element that resides in obj/Debug/android/AndroidManifest.xml.

请注意,Properties/AndroidManifest.xml 的内容总是会覆盖自定义属性提供的数据.

Note that the contents of Properties/AndroidManifest.xml always override data provided by custom attributes.

这篇关于如何在 Xamarin 中设置持久的 Android 应用程序属性(例如,allowBackup)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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