c# - C# 中修饰符 static 对于此项错误无效

标签 c# android azure xamarin.android xamarin.forms

我正在尝试使用 Xamarin 表单中的 Azure 通知中心来实现推送通知。目前我正在使用 GCM 针对 Android 执行此操作,并且遵循 this link 。但在“向 droid 项目添加推送通知”教程的第 5 步中,我收到修饰符静态对此项目无效 的错误。完成此步骤后,我的代码如下所示:

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Create a new instance field for this activity.
        static MainActivity instance = null;
        // Set the current instance of MainActivity.
        instance = this;

        global::Xamarin.Forms.Forms.Init (this, bundle);
        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
        LoadApplication (new App ());
        try
        {
            // Check to ensure everything's setup right
            GcmClient.CheckDevice(this);
            GcmClient.CheckManifest(this);

            // Register for push notifications
            System.Diagnostics.Debug.WriteLine("Registering...");
            GcmClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
        }
        catch (Java.Net.MalformedURLException)
        {
            CreateAndShowDialog("There was an error creating the Mobile Service. Verify the URL", "Error");
        }
        catch (Exception e)
        {
            CreateAndShowDialog(e.Message, "Error");
        }
        private void CreateAndShowDialog(String message, String title)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.SetMessage(message);
        builder.SetTitle(title);
        builder.Create().Show();
    }
    // Return the current activity instance.
    public static MainActivity CurrentActivity
    {
        get
        {
            return instance;
        }
    }
}

我是 C# 和 Xamarin 表单的新手,无法弄清楚我哪里出了问题。

最佳答案

您需要在函数外部声明静态变量。

private static MainActivity instance = null;
// Return the current activity instance.
public static MainActivity CurrentActivity
{
    get
    {
        return instance;
    }
}

然后简单地在OnCreateinstance = this;

关于c# - C# 中修饰符 static 对于此项错误无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106146/

相关文章:

c# - C# 中的桶排序 - 怎么做?

android - 如何在 android 复选框检查更改上接收事件?

android - 显示来自 BroadcastReceiver 的状态栏通知

azure - ARM 模板循环依赖问题

c# - 将 div 元素发送到电子邮件时丢失样式 (C# asp.net)

c# - Monotouch 和异步 CTP

android - 在 VMWare 上删除 Android x86 中的 Google App 验证通知

azure - 访问 token 已被错误的受众获取

python - 使用 Azure-Monitoring-libraries-for-python 监视 Azure-Monitor 中的 azure-resources 并获取这些指标

c# - 这些异步调用中的哪一个与另一个不同?