Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于另一个方法中的字符串命名Dispatcher?_C#_Wpf_Dispatchertimer - Fatal编程技术网

C# 基于另一个方法中的字符串命名Dispatcher?

C# 基于另一个方法中的字符串命名Dispatcher?,c#,wpf,dispatchertimer,C#,Wpf,Dispatchertimer,我正在尝试生成16个唯一的Dispatchers,而不必为每个Dispatchers重新创建代码。然而,我不知道如何根据转换为按下按钮字符串的内容动态命名它们 最初,我是单独设置每个计时器,但最终导致太多代码无法维护。现在,我有了一个方法,当单击16个按钮中的一个按钮时触发,该方法将为按钮的内容设置一个字符串,并将其传递给第二个方法以设置Dispatcher计时器 我不能只在按钮单击方法中命名计时器并传递它,因为它告诉我它已经在一个封闭范围中用于定义本地或参数。我试图通过将字符串“timer”连

我正在尝试生成16个唯一的Dispatchers,而不必为每个Dispatchers重新创建代码。然而,我不知道如何根据转换为按下按钮字符串的内容动态命名它们

最初,我是单独设置每个计时器,但最终导致太多代码无法维护。现在,我有了一个方法,当单击16个按钮中的一个按钮时触发,该方法将为按钮的内容设置一个字符串,并将其传递给第二个方法以设置Dispatcher计时器

我不能只在按钮单击方法中命名计时器并传递它,因为它告诉我它已经在一个封闭范围中用于定义本地或参数。我试图通过将字符串“timer”连接到变量名的末尾来命名计时器,但它不喜欢这样

按一下按钮

        public void StartBtnClicked(object sender, RoutedEventArgs e)
        {
            string btn = (sender as Button).Content.ToString();
            string timerName = btn + "timer";
            DispatcherTimerSetup(btn);
        }
设置计时器

        public void DispatcherTimerSetup(string passedBtn)
        {
            DispatcherTimer passedBtn + "Timer" = new DispatcherTimer();
        }
我现在的目标是将计时器命名为“Button1ContentTimer”。我将使用计时器在完成时触发事件&它们将有不同的时间跨度。我还将实现一个start/stop all按钮,这就是我命名每个按钮的原因,这样我就可以在start/stop all方法中一次调用所有按钮

编辑:

我现在正在创建计时器&将它们添加到字典中。计时器的名称都相同,但包含的字符串将不同

        public void DispatcherTimerSetup(string btn)
        {
            Dictionary<string, DispatcherTimer> timerDict =
                new Dictionary<string, DispatcherTimer>(); //Set up a dictionary to house all the timers in

            DispatcherTimer timer = new DispatcherTimer();

            try
            {
                timerDict.Add(btn, timer);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("This timer is already running");
            }
        }
public void dispatchermersetup(字符串btn)
{
字典时间记录=
new Dictionary();//设置一个字典,以便容纳所有计时器
调度程序计时器=新调度程序();
尝试
{
timerDict.Add(btn,定时器);
}
捕获(异常)
{
Show(“此计时器已在运行”);
}
}
StopAll方法将接收字典并对其中的每个计时器进行迭代


        static public void StopAll(Dictionary<string, DispatcherTimer> timerDict)
        {
            foreach(KeyValuePair<string,DispatcherTimer> entry in timerDict)
            {

            }
        }

静态公共void StopAll(字典timerDict)
{
foreach(timerDict中的KeyValuePair条目)
{
}
}
我唯一剩下的问题是如何真正停止这些计时器?以前,我只调用timerName.Stop();多次,每个计时器使用不同的计时器名称

但是现在计时器的名称都一样了&在字典中,我不知道如何访问它们。我试过:

        static public void StopAll(Dictionary<string, DispatcherTimer> timerDict)
        {

            foreach(KeyValuePair<string,DispatcherTimer> entry in timerDict)
            {
                timerDict.Remove(DispatcherTimer);
            }
        }
静态公共void StopAll(字典timerDict)
{
foreach(timerDict中的KeyValuePair条目)
{
timerDict.Remove(调度程序);
}
}

,但它告诉我Dispatchermer是在给定上下文中无效的类型。我甚至不确定从字典中删除它是正确的做法,这会阻止它吗?或者我需要用另一种方式来处理这个问题?我觉得应该有一种方法可以按顺序从字典中调用每个Dispatchermer元素,但我还没有弄清楚这一点。

以下是文档的链接:

要停止计时器,你可以这样做。请注意,仅从字典中删除计时器并不会停止它

static public void StopAll(Dictionary<string, DispatcherTimer> timerDict)
{
    foreach(var timer in timerDict.Values) timer.Stop();
    timerDict.Clear();
}

static public void StopTimer(string TimerName, Dictionary<string, DispatcherTimer> timerDict)
{
    if (timerDict.ContainsKey(TimerName)
    {
        timerDict[TimerName].Stop();
        timerDict.Remove(TimerName);
    }        
}
静态公共void StopAll(字典timerDict)
{
foreach(timerDict.Values中的var timer)timer.Stop();
timerDict.Clear();
}
静态公共void StopTimer(字符串TimerName、字典timerDict)
{
if(timerDict.ContainsKey(TimerName)
{
timerDict[TimerName].Stop();
timerDict.Remove(TimerName);
}        
}

为什么不使用一个?为什么计时器需要单独的名称?所有计时器都使用相同的勾号方法,您想识别它们吗?@Clemens,我以前从未使用过字典,现在正在阅读。@Tomtom,我(想我)的原因是什么需要单独命名它们主要是为了Stop all方法。如果我让它们的名称保持不变,我知道它们仍然会在独立的线程中运行,但我不知道如何在Stop all方法中分别调用它们。如果不需要它们的名称,则可以将它们全部放在
列表中
,如果要停止,可以迭代此列表p全部