简体   繁体   中英

Cannot apply animation to parent frame from page WPF c#

I have a page within a Frame .What i am trying to do is to apply an storyboard/animation from the page to the parent Frame . Generally, from user controls, i used the following code to get the parent :

var parent = (Frame)this.Parent;

But if i use the same code in my page to get the parent frame and apply the animation :

private void Goback_MouseDown(object sender, MouseButtonEventArgs e)
{
Storyboard sb = new Storyboard;
sb = this.FindResource("HideMainframe");
var parent = (Frame)this.Parent;
Storyboard.SetTarget(sb, parent);
sb.Begin();
}

Storyboard

 <Storyboard x:Key="HideMainframe" Storyboard.TargetProperty="Opacity" >
        <DoubleAnimation Duration="0:0:0.5" To="0" >
            <DoubleAnimation.EasingFunction>
                <CircleEase  EasingMode="EaseIn"/>
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>

i get an exception : No target was specified for 'System.Windows.Media.Animation.DoubleAnimation'. .On the web,i came to know about VisualTreeHelper class but before i'ld go for that, i want to know,why my code is not working ? Or to be specific,why can't i get the parent frame from the page within ?

So it looks like Pages navigated in frames don't contain a "Parent" value, which is why the method you specified doesn't work, but walking the VisualTreeHelper does. What you are trying to achieve can be easily simplified by passing the Frame through the Page Constructor though.

Your Pages Code Behind

Frame f;
public Page1(Frame frame)
{
    f = frame;
    InitializeComponent();
}

Your button logic

private void Goback_MouseDown(object sender, MouseButtonEventArgs e)
{
   Storyboard sb = (Storyboard)this.TryFindResource("HideMainframe");
   Storyboard.SetTarget(sb, f);
   sb.Begin();
}

So when navigating the frame, you'll just pass it through.

myFrame.Navigate(new Page1(myFrame)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM