Wpf 在以编程方式添加的控件上使用情节提要动画
我正在尝试将一个新控件淡入应用程序的“应用程序”区域,该区域是在删除现有控件后以编程方式添加的。我的代码如下所示:Wpf 在以编程方式添加的控件上使用情节提要动画,wpf,animation,storyboard,Wpf,Animation,Storyboard,我正在尝试将一个新控件淡入应用程序的“应用程序”区域,该区域是在删除现有控件后以编程方式添加的。我的代码如下所示: void settingsButton_Clicked(object sender, EventArgs e) { ContentCanvas.Children.Clear(); // Fade in settings panel NameScope.SetNameScope(this, new NameSc
void settingsButton_Clicked(object sender, EventArgs e)
{
ContentCanvas.Children.Clear();
// Fade in settings panel
NameScope.SetNameScope(this, new NameScope());
SettingsPane s = new SettingsPane();
s.Name = "settingsPane";
this.RegisterName(s.Name, s);
this.Resources.Add(s.Name, s);
Storyboard sb = new Storyboard();
DoubleAnimation settingsFade = new DoubleAnimation();
settingsFade.From = 0;
settingsFade.To = 1;
settingsFade.Duration = new Duration(TimeSpan.FromSeconds(0.33));
settingsFade.RepeatBehavior = new RepeatBehavior(1);
Storyboard.SetTargetName(settingsFade, s.Name);
Storyboard.SetTargetProperty(settingsFade, new PropertyPath(UserControl.OpacityProperty));
ContentCanvas.Children.Add(s);
sb.Children.Add(settingsFade);
sb.Begin();
}
但是,当我运行此代码时,会出现错误“不存在用于解析名称'settingsPane'的适用名称范围。”
我可能做错了什么?我非常确定我已经正确地注册了所有内容:(我不会与名称范围等发生冲突,而是宁愿使用Storyboard.SetTarget
var b = new Button() { Content = "abcd" };
stack.Children.Add(b);
var fade = new DoubleAnimation()
{
From = 0,
To = 1,
Duration = TimeSpan.FromSeconds(5),
};
Storyboard.SetTarget(fade, b);
Storyboard.SetTargetProperty(fade, new PropertyPath(Button.OpacityProperty));
var sb = new Storyboard();
sb.Children.Add(fade);
sb.Begin();
我同意,在这种情况下使用名称范围可能是错误的。使用SetTarget比使用SetTargetName更简单、更容易 如果它对其他人有帮助,下面是我用来突出显示表中某个特定单元格的内容,突出显示会衰减为零。这有点像添加新答案时的StackOverflow突出显示
TableCell cell = table.RowGroups[0].Rows[row].Cells[col];
// The cell contains just one paragraph; it is the first block
Paragraph p = (Paragraph)cell.Blocks.FirstBlock;
// Animate the paragraph: fade the background from Yellow to White,
// once, through a span of 6 seconds.
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
p.Background = brush;
ColorAnimation ca1 = new ColorAnimation()
{
From = Colors.Yellow,
To = Colors.White,
Duration = new Duration(TimeSpan.FromSeconds(6.0)),
RepeatBehavior = new RepeatBehavior(1),
AutoReverse = false,
};
brush.BeginAnimation(SolidColorBrush.ColorProperty, ca1);
我在begin方法中使用此参数解决了问题,请尝试:
sb.Begin(this);
因为名称已在窗口中注册。这可能有点奇怪,但我的解决方案是使用两种方法:
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTarget(DA, myRect);
sb.Begin(this);
在这种情况下,没有错误
看看我用过的代码
int n = 0;
bool isWorking;
Storyboard sb;
string myObjectName;
UIElement myElement;
int idx = 0;
void timer_Tick(object sender, EventArgs e)
{
if (isWorking == false)
{
isWorking = true;
try
{
myElement = stackObj.Children[idx];
var possibleIDX = idx + 1;
if (possibleIDX == stackObj.Children.Count)
idx = 0;
else
idx++;
var myRect = (Rectangle)myElement;
// Debug.WriteLine("TICK: " + myRect.Name);
var dur = TimeSpan.FromMilliseconds(2000);
var f = CreateVisibility(dur, myElement, false);
sb.Children.Add(f);
Duration d = TimeSpan.FromSeconds(2);
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
sb.Children.Add(DA);
myObjectName = myRect.Name;
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTarget(DA, myRect);
Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
sb.Begin(this);
n++;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " " + DateTime.Now.TimeOfDay);
}
isWorking = false;
}
}
我在回答这个问题时遇到了麻烦,因为我们的目标是.NET 3.0;尽管SetTarget已添加到.NET 3.0 SP2中,但这仅适用于.NET 3.5安装程序;因此,如果您想支持.NET 3.0 SP1,我需要使用Carlos的解决方案更改为“sb.Begin(this)”。这说明了一种情况,
SetTarget
不起作用,RegisterName
/SetTargetName
组合是必要的。+1它完全解决了问题,无需编写任何新代码。由于我的故事板是在app.xaml资源中定义的,我需要在SB.Begin(app.Current.MainWindow)时添加参数;在我的例子中,我需要为它提供InnerControl:FrameworkElement InnerControl=this.degents(1).First()作为FrameworkElement;Storyboard IndeterminateStoryboard=VisualStateManager.GetVisualStateGroups(InnerControl).OfType().Single(x=>x.Name==“CommonStates”).States.OfType().Single(x=>x.Name==“不确定”).Storyboard;不确定性Storyboard.Begin(innerControl);