开发者

How can I start a DispatcherTimer when my custom control is loaded?

开发者 https://www.devze.com 2023-01-14 21:06 出处:网络
I need to be able 开发者_运维技巧start a timer when my custom control is loaded. However, I need to be able to do this in the abstract class that all controls of this type inherit from.The control doe

I need to be able 开发者_运维技巧start a timer when my custom control is loaded. However, I need to be able to do this in the abstract class that all controls of this type inherit from.


The control does have a Loaded event that you can attach to in your abstract base class in order to start the timer.

// in ctor of abstract control
_timer = new DispatcherTimer(...);
Loaded += (s,e) => _timer.Start();


Not sure what's your issue is? There are three timers available in .NET and you should be probably able use any of them. Check this article to understand choices and differences between them.


Do you mean a control as abstract one. Like below?
Try the following:

public partial class MainWindow : Window
{
    MyControl mycontrol;

    public MainWindow()
    {
        InitializeComponent();

        //// Your logics...

        mycontrol.Dispatcher.BeginInvoke((Action)(() =>
            {
                 /// Try invoking timer here...
            }));
    } 
}

public abstract class MyControl: Control
{

}

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消