위임자가 내 코드에서 작동하지 않는 이유를 알고있는 사람이 있습니까? 처음에는 양식을 표시하고 대리인을 사용하여 양식의 레이블을 업데이트하는 콘솔 응용 프로그램이 있습니다..Net 대리인이 작동하지 않습니다.
namespace DELEGATESAMPLEPROJECT
{
public class Program
{
public delegate void OnConfirmCall();
public OnConfirmCall confirmCall;
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(new Program().getReference()));
new Program().startFunctionCall();
}
public Program getReference(){
return this;
}
public void startFunctionCall(){
Console.WriteLine("Function Call Started!");
if(confirmCall != null){
Console.WriteLine("Function Call Executing...");
confirmCall();
}
}
}
}
FORM1
namespace DELEGATESAMPLEPROJECT
{
public partial class Form1 : Form
{
public Form1(Program thisProgramClass)
{
InitializeComponent();
thisProgramClass.confirmCall += saySomething;
}
public void saySomething()
{
Label1.Text = "Hello World!";
}
private void Form1_Load(object sender, EventArgs e)
{
Label1.Text = "Hi C#!";
}
}
}
당신은 내가 "Hello World!"
하지만 작동하지, 나는 무엇을 자사의 실종에 "Hi C#!"
을 변경하려고 볼 수있다?
두 개의 전혀 다른 'Program' 인스턴스를 사용하고 있기 때문입니다. 여기'Application.Run (새 Form1 (새 프로그램(). getReference())); '와'새로운 프로그램(). startFunctionCall();'을 사용하십시오. –
@ChristophKn 대답으로 게시하십시오 – slawekwin
@ ChristophKn - 프로그램 클래스의 이름을 변경해야합니까? – bernzkie