简体   繁体   English

C#由于循环依赖而在未引用的项目中调用方法

[英]C# Calling a method in non referenced project because of circular dependency

I have two class's controller and the CountDownTimer. 我有两个类的控制器和CountDownTimer。 The project with the controller references the project with the CountDownTimer class. 带有控制器的项目引用带有CountDownTimer类的项目。 I have methods(TickUpdate) in the CountDownTimer class which call methods in the controller class every time the timer counts down by aa second. 我在CountDownTimer类中有方法(TickUpdate),每当计时器倒数一秒时,该方法就会在控制器类中调用方法。 But because of circular dependency i cannot reference controller project in the CountDownTimer project. 但是由于循环依赖性,我无法在CountDownTimer项目中引用控制器项目。

So my question, is the any way to call the TickUpdate method from the countdown class? 所以我的问题是,从倒数计时类调用TickUpdate方法有什么办法吗?

using SailTimerClassLibrary;

namespace SailTimerUIProject
{
    public class Controller : ApplicationContext
    {
    //Store a reference to the UI
    internal frmMain MainUI { get; set; }

    private int seconds = 30;
    CountDownTimer timer;

    public Controller()
    {

        MainUI = new frmMain(this);

        //We can do any necessary checks or changes to the MainUI here before it becomes visible
        MainUI.Show();
        timer = new CountDownTimer(seconds);
        TickUpdate(("" + seconds / 60).PadLeft(2, '0') + "m:" + ("" + seconds % 60).PadLeft(2, '0') + "s");
    }

    internal void TickUpdate(string mmss)
    {
        MainUI.lblTimer.Text = mmss;
    }

    internal void StartTimer()
    {
        timer.StartTimer();
    }
}
}



namespace SailTimerClassLibrary
{
public class CountDownTimer : ICountDownTimer
{
    private int seconds; // Time in seconds
    private int reSetValue; // Time in seconds
    public System.Windows.Forms.Timer timer1;

    public CountDownTimer(int seconds)
    {
        this.seconds = seconds;
        reSetValue = seconds;
        timer1 = new System.Windows.Forms.Timer();
        timer1.Tick += new EventHandler(timer1_Tick); // Add Handler(timer1_Tick)
        timer1.Interval = 1000; // 1 second
        //TickUpdate(("" + seconds / 60).PadLeft(2, '0') + "m:" + ("" + seconds % 60).PadLeft(2, '0') + "s");
    }

    public void timer1_Tick(object sender, EventArgs e)
    {
        seconds--; // Decrement seconds
        if (seconds == 0) // Stop Timer at 0
        {
            timer1.Stop(); // Stop timer
        }
        else
        {
            //TickUpdate(convertSecondToMMSS());

            if (seconds % 60 == 0 || seconds >= 1 && seconds <= 10)
            {
                //TickUpdate(seconds);
            }
        }
    }

    public void StartTimer()
    {
        timer1.Start(); // Start Timer
    }

    public string convertSecondToMMSS()
    {
        TimeSpan t = TimeSpan.FromSeconds(seconds);
        string str = string.Format("{0:D2}m:{1:D2}s", //{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms
            t.Minutes,
            t.Seconds);

        return str;
    }

    public void StopTimer()
    {
        timer1.Stop();
    }

    public void ResetTimer()
    {
        timer1.Stop();
        seconds = reSetValue;
        //parent.TickUpdate(convertSecondToMMSS());
    }

    public void SetTimer(int seconds)
    {
        timer1.Stop();
        this.seconds = seconds;
        reSetValue = seconds;
        //parent.TickUpdate(convertSecondToMMSS());
    }
}
}

A few design issues here are hampering your ability. 这里的一些设计问题正在妨碍您的能力。

The CountDownTimer should probably be in an ancillary project or class library that both of these projects reference. CountDownTimer可能应该在这两个项目都引用的辅助项目或类库中。 This avoids the whole circular dependency problem. 这避免了整个循环依赖问题。 But what about controllers?! 但是控制器呢?
Well.... 好....

CountDownTimer should not know about controllers, or anything else for that matter! CountDownTimer不应该知道控制器或其他有关此问题的信息! It should expose an event of some kind that the controllers can add a handler to, and the controllers can update themselves . 它应该公开控制器可以向其添加处理程序的某种事件,并且控制器可以自行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM