在不引用对象的情况下调用函数

时间:2014-10-28 16:59:29

标签: c#

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

  

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

     

错误2:非静态字段需要对象引用,   方法或属性' 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.
    }
    }
}

2 个答案:

答案 0 :(得分:3)

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

鉴于这是一个控制台应用程序,您可能只想让Accept_call静态。通常,如果这是普通对象,您可能希望将所有内容都设置为非静态。

private static void Accept_call(object sender, EventArgs e)

private static void Reject_call(object sender, EventArgs e)

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

答案 1 :(得分:1)

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