编译时错误 "The name 'pubnub' does not exist in the current context"

Compile time error "The name 'pubnub' does not exist in the current context"

我还没有在 C# 中使用 in years and when I did it was . I'm trying to set up a simple 概念证明,我真的很挣扎。我创建了一个新的表单应用程序(我希望能够使用按钮控制提要,start/stop/etc)。然后我打开 program.cs,它给了我以下代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace feedtest
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

        }
    }

我已经安装了 using package manager and I've added the using reference, but that's where things go sour. The first step in the documentation says to "initialize the API" 使用以下行:

Pubnub pubnub = new Pubnub("demo", "demo");

我终于想通了,我把它放在了 Static Void Main() 中,但现在我好像掉进了兔子洞。在修改了文档中的一些内容并忽略其他内容之后,我处于以下几点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using PubNubMessaging.Core;

namespace feedtest
{
    static class Program
    {
        private static string result;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            Pubnub pubnub = new Pubnub("demo", "demo");

            pubnub.Subscribe<string>(
               "my_channel",
                DisplaySubscribeReturnMessage,
                DisplaySubscribeConnectStatusMessage,
                DisplayErrorMessage
            );


        }

        static void DisplayErrorMessage(PubnubClientError obj)
        {
            throw new NotImplementedException();
        }

        static void DisplaySubscribeConnectStatusMessage(string obj)
        {
            throw new NotImplementedException();
        }

        static void DisplaySubscribeReturnMessage(string obj)
        {
            Console.WriteLine("SUBSCRIBE REGULAR CALLBACK:");
            Console.WriteLine(result);
            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object subscribedObject = (object)deserializedMessage[0];
                    if (subscribedObject != null)
                    {
                        //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                        string resultActualMessage = pubnub.JsonPluggableLibrary.SerializeToJsonString(subscribedObject);
                    }
                }
            }
        }
    }

}

当前错误是

The name 'pubnub' does not exist in the current context

我该如何解决这个错误?我是不是该?我什至接近?我选错起点了吗?有谁知道任何好的简单、pubnub 教程?

pubnub 超出了您尝试使用它的范围。像你一样将它用作局部变量,你需要在你使用它的相同方法中声明它。

如果你想在两种方法中使用相同的pubnub,那么你需要将它放在其他地方或传递它。可能在您的程序中将其声明为静态字段。

private static Pubnub pubnub = new Pubnub("demo", "demo");