调用函数而不引用对象
本文关键字:引用 对象 函数 调用 | 更新日期: 2023-09-27 18:28:05
C
#中的菜鸟,所以我在尝试使用系统事件参数调用C#中的函数时遇到问题。我知道问题不在于系统事件参数,但是我应该调用这种类型的函数的正确方法是什么?
错误 1:非静态字段需要对象引用, 方法或属性 'ConsoleApplication3.Program.Reject_call(对象, System.EventArgs('
错误 2:非静态字段需要对象引用, 方法或属性 'ConsoleApplication3.Program.Accept_call(对象, 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, "answerswer") == 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.
}
}
}
您正在从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
。
您需要
通过 new
运算符创建 Program
类的实例,或者将Reject_call
和Accept_call
标记为static
。