如何使用 Xamarin Forms 为外部应用程序指定完整的 android 包名称?

How to specify a full android package name for external apps using Xamarin Forms?

我在使用 Xamarin Forms 创建小部件配置时遇到问题 activity。我认为发生的事情是,在编译时,包的名称被主要 activity 和诸如此类的随机 #/string 替换。但是,如果我检查生成的 APK,我可以看到 <appwidget-provider ... android:configure="my.package.myClass".../> 没有更新。我觉得应该是<appwidget-provider ... android:configure="randomstring.myClass".../>。所以我相信这会阻止配置屏幕出现。有没有办法强制使用 Xamarin 更新此属性上的包名称?或者有没有办法在放置小部件期间调用它之前在代码中设置此属性?

这是我的配置(包名称已简化以帮助调试):

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="288dip"
    android:minHeight="72dip"
    android:resizeMode="horizontal"
    android:minResizeWidth="144dip"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/Widget"
    android:previewImage="@drawable/previewImage"
    android:configure="quickclip.QuickClipConfigActivity"

/>
namespace quickclip
{
    [Activity(Label = "QuickClipConfigActivity", Name= "quickclip.QuickClipConfigActivity", Exported=true )]
    [IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_CONFIGURE" })]
    public class QuickClipConfigActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Setup);
            SetResult(Result.Canceled);


            // Create your application here
        }
    }
}

[Activity] 属性的名称字段只是为了调试目的添加的。不管有没有,放置widget都不显示config界面,也不进入activity class(通过断点验证),widget也不显示。但是,如果我删除 android:configure="my.package.myClass" 我可以让小部件正常显示。

如果您没有明确指定它,Xamarin.Android 将计算一些 MD5 并使用它来为所有 Android 可调用包装程序添加前缀,并将该 MD5 用于命名空间。

为避免这种情况,您可以在类型上使用 RegisterAttribute

[Register("my.cool.namespace.MyType")]
public class MyType : SomeJavaType
{
}

对于 ActivityService,您可以像这样在 ActivityAttributeServiceAttribute 中使用 Name 属性:

[Activity(Label = "My Activity", Name = "my.cool.namespace.MyActivity")]
public class MyActivity : Activity
{
}

或者您可以将 ActivityAttributeRegisterAttribute 结合使用:

[Register("my.cool.namespace.MyActivity")]
[Activity(Label = "My Activity")]
public class MyActivity : Activity
{
}

这应该会在您的 AndroidManifest 中生成一个很好的条目,它很容易从布局文件或其他 Apps/Widgets.

使用