【问题标题】:Calling an function without reference to object [duplicate]在不引用对象的情况下调用函数[重复]
【发布时间】:2014-10-28 16:59:29
【问题描述】:

C# 中的菜鸟,所以我在尝试使用系统事件参数调用 C# 中的函数时遇到问题。我知道问题不在于系统事件参数,但我应该调用这种类型的函数的正确方法是什么?

Error1:非静态字段需要对象引用, 方法或属性 'ConsoleApplication3.Program.Reject_call(object, System.EventArgs)'

Error2:非静态字段需要对象引用, 方法或属性 'ConsoleApplication3.Program.Accept_call(object, System.EventArgs)'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Conversation;
using Microsoft.Lync.Model.Conversation.AudioVideo;

namespace ConsoleApplication3
{
    class Program
    {
    //holds the Lync client instance
    private static LyncClient _client;
    //holds the reference to the conversation associated with this window
    private Conversation conversation;
    //self participant's AvModality
    private AVModality avModality;

    private static void Main(string[] args)
    {
        //Obtains the lync client instance
        _client = LyncClient.GetClient();
        consoleInputReadRoutine();
        _client.ConversationManager.ConversationAdded += ConversationManager_ConversationAdded;
        _client.ConversationManager.ConversationRemoved += ConversationManager_ConversationRemoved;

        Console.ReadLine();
    }

    static void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e)
    {
        e.Conversation.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += Program_ModalityStateChanged;
    }

    static void Program_ModalityStateChanged(object sender, ModalityStateChangedEventArgs e)
    {
        Console.WriteLine("Modality state changed " + String.Format("{0} => {1}", e.OldState, e.NewState));
    }

    static void consoleInputReadRoutine()
    {

        bool done = false;
        while(!done){

            string input = Console.ReadLine(); // Get string from user
            Console.WriteLine("user input {0}\n", input);

            // Set exit condition.
            if(String.Compare(input, "exit") == 0){
                done = true;
            }
            else if (String.Compare(input, "reject") == 0)
            {
                //Do a reject call.
                Reject_call(null, new EventArgs());
            }
            else if(String.Compare(input, "answer") == 0)
            {
                //Do a answer call.
                Accept_call(null, new EventArgs());
            }
        }
    }

    // Accepts an incoming call: AvModality.Accept()
    private void Accept_call(object sender, EventArgs e)
    {
        //accepts an incoming invite (syncronous operation)
        avModality.Accept();
    }

    // Rejects an incoming call: AvModality.Reject()
    private void Reject_call(object sender, EventArgs e)
    {
        //rejects an incoming invite (which will disconnect the call)
        //the ModalityDisconnectReason may be used to specify a reason to the caller side
        //the reason may be shown on the Lync client conversation window
        avModality.Reject(ModalityDisconnectReason.Decline);
    }

    static void ConversationManager_ConversationRemoved(object sender, ConversationManagerEventArgs e)
    {
        //Exit out of program if you wish after conversation is removed.
    }
    }
}

【问题讨论】:

  • 如果你想在没有类实例的情况下调用/访问函数/字段/属性,那么函数必须是静态的。

标签: c#


【解决方案1】:

您正在从 static 一个 (static void consoleInputReadRoutine) 调用实例方法 (private void Accept_call)。

鉴于这是一个控制台应用程序,您可能只想将 Accept_call 设为静态。一般来说,如果这是一个普通对象,你会想要让所有东西都变成非静态的。

private static void Accept_call(object sender, EventArgs e)

private static void Reject_call(object sender, EventArgs e)

这些函数使用的类成员也需要是静态的。尤其是avModality

【讨论】:

  • 并使 AVModality avModality 静态
  • @Andez 谢谢,包括在内。
【解决方案2】:

您需要通过new 运算符创建Program 类的实例,或者将Reject_callAccept_call 标记为static

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2015-01-26
    相关资源
    最近更新 更多