c# - Animation Methods, Simplification and Repairing -
context
i'm kinda new @ animating wpf stuff, i've played around library or 2 , "had" animation used window control in wpf, example of method, keep in mind method works:
public void animatefadewindow(object sender, double opacity, double period) { //tab item enw tab item (the sender casted type.) window win = (window)sender; win.opacity = 0; //using doubleanimation class, animation new isntancem use parameter opacity , set period timespan. doubleanimation animation = new doubleanimation(opacity, timespan.fromseconds(period)); //begin animation on object. win.beginanimation(window.opacityproperty, animation); }
problem
like said previously, code works, problem code was, of course, it's suited window control, won't work other controls, instance, tabitem, button or other control wanted use for, "upgraded" method , current method:
public void animatefade(object sender, double opacity, double period) { //using doubleanimation class, animation new isntancem use parameter opacity , set period timespan. doubleanimation animation = new doubleanimation(opacity, timespan.fromseconds(period)); object obj = sender.gettype(); if (obj tabitem) { tabitem tab = (tabitem)sender; tab.beginanimation(tabitem.opacityproperty, animation); } else if (obj label) { label lab = (label)sender; lab.beginanimation(label.opacityproperty, animation); } else if (obj window) { window win = (window)sender; win.opacity = 0; win.beginanimation(window.opacityproperty, animation); } }
this method doesn't work. don't know i'm doing wrong here, wondered if possibly out.
also, there easier way using propertyinfo class or reflection class?
thanks stack.
your issue nothing animation
.the problem comparing sender.type
while should compare sender
i.e. use if (sender tabitem)
instead of if (obj tabitem)
.
moreover, there no need compare sender tabitem
, lable
, window
, etc 1 one, uielements
! , since uielement
implements ianimatable
, need cast sender
uielement
, have general method applies animation control :
public void animatefade(object sender, double opacity, double period) { uielement element = (uielement)sender; element.opacity = 0; doubleanimation animation = new doubleanimation(opacity, timespan.fromseconds(period)); element.beginanimation(uielement.opacityproperty, animation); }
Comments
Post a Comment