问题描述
- wpf 中 timeline和附加属性奇怪的死循环
-
具体是这样的,我想建一个自定义list然后里面放timeline,在自定义的list中,我增加了两个附加属性,一个是 previous一个是next,用来给timeline项增加前一个后一个双向链接附加属性,
但增加的时候,会进行死循环,不知道为什么,如果给这个集合增加别的dependencyObj对象就一点问题也没有,如果增加timeline就进入死循环,代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Collections.ObjectModel;namespace WpfApplication21
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private List subs = new List();private void button1_Click(object sender, RoutedEventArgs e) { //问题:增加control没问题,但增加doubleanimation就出错,进入死循环 DependencyObject t; // t = new DoubleAnimation(); t = new Control(); subs.Add(t); int index = subs.IndexOf(t); if (subs.Count > 1) { DependencyObject previous = subs[index - 1]; //建立集合元素中的链接 SetNext(previous, t); SetPrevious(t, previous); } } public static object GetPrevious(DependencyObject obj) { return (object)obj.GetValue(PreviousProperty); } public static void SetPrevious(DependencyObject obj, object value) { obj.SetValue(PreviousProperty, value); } //上一个元素附加属性 public static readonly DependencyProperty PreviousProperty = DependencyProperty.RegisterAttached("Previous", typeof(object), typeof(MainWindow), new UIPropertyMetadata(null)); public static object GetNext(DependencyObject obj) { return (object)obj.GetValue(NextProperty); } public static void SetNext(DependencyObject obj, object value) { obj.SetValue(NextProperty, value); } //下一个元素附加属性 public static readonly DependencyProperty NextProperty = DependencyProperty.RegisterAttached("Next", typeof(object), typeof(MainWindow), new UIPropertyMetadata(null)); }
}
时间: 2024-10-31 10:16:06