我正在尝试使用 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;
}
}
然后简单地在OnCreate
内instance = this;
。
关于c# - C# 中修饰符 static 对于此项错误无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106146/