C# 修饰符static对于C中的此项错误无效#
我正在尝试在Xamarin表单中使用Azure的通知中心实现推送通知。目前我正在为Android做这件事,使用GCM,我也遵循这一点。但在“向droid项目添加推送通知”教程的第5步中,我得到了一个错误,即C# 修饰符static对于C中的此项错误无效#,c#,android,azure,xamarin.android,xamarin.forms,C#,Android,Azure,Xamarin.android,Xamarin.forms,我正在尝试在Xamarin表单中使用Azure的通知中心实现推送通知。目前我正在为Android做这件事,使用GCM,我也遵循这一点。但在“向droid项目添加推送通知”教程的第5步中,我得到了一个错误,即修改器static对此项无效。完成此步骤后,我的代码如下所示: protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Create a new ins
修改器static对此项无效。完成此步骤后,我的代码如下所示:
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;
}
}
然后简单地instance=this
insideOnCreate
请学习基本的C#语法。。。这看起来像是编译时错误(这意味着你不知道C#basics),它准确地告诉你哪里出了问题,在哪一行中……在OnCreate方法之外声明静态变量的可能重复消除了错误。但是我在下面方法的末尾得到了一个括号“}”:catch(异常e){CreateAndShowDialog(e.Message,“Error”);}它看起来像是在private void CreateAndShowDialog(String Message,String title)行之前
您需要一个结束括号}
来关闭OnCreate
方法。是的,非常感谢。这很有帮助。我被困在这个问题上已经有一段时间了。谢谢,非常感谢!