Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# WPF-如何通过绑定停止计时器_C#_Wpf_Xaml_C# 4.0 - Fatal编程技术网

C# WPF-如何通过绑定停止计时器

C# WPF-如何通过绑定停止计时器,c#,wpf,xaml,c#-4.0,C#,Wpf,Xaml,C# 4.0,首先,感谢你抽出时间阅读这篇文章 我有一个计时器类,每60秒从我的SQL数据库下载一次“产品”。i、 e.检查可能已由其他用户编辑的更新产品 这是我的班级代码: public class GetProducts : INotifyPropertyChanged { public GetProducts() { Timer updateProducts = new Timer(); updateProducts

首先,感谢你抽出时间阅读这篇文章

我有一个计时器类,每60秒从我的SQL数据库下载一次“产品”。i、 e.检查可能已由其他用户编辑的更新产品

这是我的班级代码:

public class GetProducts : INotifyPropertyChanged
    {
        public GetProducts()
        {
            Timer updateProducts = new Timer();
            updateProducts.Interval = 60000; // 60 second updates
            updateProducts.Elapsed += timer_Elapsed;
            updateProducts.Start();
        }

        public ObservableCollection<Products> EnabledProducts
        {
            get
            {
                return ProductsDB.GetEnabledProducts();
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("EnabledProducts"));
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
公共类GetProducts:INotifyPropertyChanged
{
公共产品()
{
Timer updateProducts=新计时器();
updateProducts.Interval=60000;//60秒更新
updateProducts.Appeased+=计时器\u Appeased;
updateProducts.Start();
}
公共可观测收集支持产品
{
得到
{
返回ProductsDB.GetEnabledProducts();
}
}
无效计时器已过(对象发送器,ElapsedEventArgs e)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(“EnabledProducts”);
}
公共事件属性更改事件处理程序属性更改;
}
然后将其绑定到XAML(WPF)控件的tag属性:


Tag=“{Binding Source={StaticResource getProducts\u timer},Path=EnabledProducts,Mode=OneWay}”
这真的很有效。我的问题是,当控件所在的窗口或页面关闭时,无论发生什么,计时器都会继续滴答作响

当页面/控件不再可用时,有人能建议一种停止自动售票机的方法吗


再次感谢您抽出时间。非常感谢您提供的所有帮助。

首先,请参考计时器:

private Timer updateProducts;
public GetProducts()
{
    updateProducts = new Timer();
    ......
}
public void StopUpdates()
{
     updateProducts.Stop();
}
例如,创建另一个方法,
StopUpdates
,调用该方法将停止计时器:

private Timer updateProducts;
public GetProducts()
{
    updateProducts = new Timer();
    ......
}
public void StopUpdates()
{
     updateProducts.Stop();
}
现在在窗口的OnUnloaded事件中停止计时器:

private void MyPage_OnUnloaded(object sender, RoutedEventArgs e)
{
    var timer = this.Resources["getProducts_timer"] as GetProducts;
    if (timer != null)
        timer.StopUpdates();
}

首先,保持对计时器的引用:

private Timer updateProducts;
public GetProducts()
{
    updateProducts = new Timer();
    ......
}
public void StopUpdates()
{
     updateProducts.Stop();
}
例如,创建另一个方法,
StopUpdates
,调用该方法将停止计时器:

private Timer updateProducts;
public GetProducts()
{
    updateProducts = new Timer();
    ......
}
public void StopUpdates()
{
     updateProducts.Stop();
}
现在在窗口的OnUnloaded事件中停止计时器:

private void MyPage_OnUnloaded(object sender, RoutedEventArgs e)
{
    var timer = this.Resources["getProducts_timer"] as GetProducts;
    if (timer != null)
        timer.StopUpdates();
}

简单地有一个OnClosing事件并调用updateProducts.Stop()怎么样?简单地有一个OnClosing事件并调用updateProducts.Stop()怎么样?非常感谢Blachshma。您的解决方案非常有效。非常感谢您Blachshma。您的解决方案非常有效。