simultaneous storyboards in windows phone 7 - c#

I have looked everywhere I could think of, but I can't find the answer to this...
I want to make a sort of template Storyboard for windows phone 7 (xaml and c#), and then to use that storyboard to animate multiple objects the same way. (i.e. rotating all objects 90 degrees at the same time) Any ideas?
It has to be dynamically done. By that I mean that I want to assign different objects to several different storyboards during the program, so it can't be something static.
The main problem that I have faced up to this point is that a storyboard must be stopped before it can be used again, therefore they can't be simultaneously running. I was thinking however that if I could make temporary copies of the Storyboard, that I could run them all at the same time? Please give me any ideas that you have. Thanks in advance!
I initialize my objects (rectangles) in a grid as such:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Rectangle Name="rec1" Height="80" Width="10" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,30,140,0">
<Rectangle.RenderTransform>
<RotateTransform x:Name="rec1Transform" Angle="0" CenterX="5" CenterY="-10" />
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Name="rec2" Height="10" Width="80" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,55,0">
.
.
.
</Rectangle>
</Grid>
At this moment, I have 4 general storyboards that I want to use many times over, and I am using a series of buttons to imitate all of the animations. For example:
My Storyboards
private void rotate1(object parameter, object secondParameter, String Target)
{
oneTwo.Stop();
oneTwo.Children[0].SetValue(Storyboard.TargetNameProperty, Target);
oneTwo.Begin();
}
private void rotate2(object parameter, object secondParameter, String Target)
{
twoThree.Stop();
twoThree.Children[0].SetValue(Storyboard.TargetNameProperty, Target);
twoThree.Begin();
}
private void rotate1Inverse(object parameter, object secondParameter, string Target)
{
twoOne.Stop();
twoOne.Children[0].SetValue(Storyboard.TargetNameProperty, Target);
twoOne.Begin();
}
private void rotate2Inverse(object parameter, object secondParameter, string Target)
{
threeTwo.Stop();
threeTwo.Children[0].SetValue(Storyboard.TargetNameProperty, Target.ToString());
threeTwo.Begin();
}
My Simulation Buttons
//Rotate Buttons
private void button_Click1(object sender, RoutedEventArgs e)
{
rec1Transform.CenterY = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec1Transform");
}
private void button_Click2(object sender, RoutedEventArgs e)
{
rec2Transform.CenterX = -10;
rotate1(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec2Transform");
}
private void button_Click3(object sender, RoutedEventArgs e)
{
rec3Transform.CenterY = -10;
rotate1(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec3Transform");
}
private void button_Click4(object sender, RoutedEventArgs e)
{
rec4Transform.CenterX = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec4Transform");
}
private void button_Click5(object sender, RoutedEventArgs e)
{
rec5Transform.CenterY = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec5Transform");
}
private void button_Click6(object sender, RoutedEventArgs e)
{
rec6Transform.CenterX = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec6Transform");
}
private void button_Click7(object sender, RoutedEventArgs e)
{
rec7Transform.CenterY = -10;
rotate1(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec7Transform");
}
//Inverse Rotate Buttons
private void button_Click1_Inverse(object sender, RoutedEventArgs e)
{
rec1Transform.CenterY = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec1Transform");
}
private void button_Click2_Inverse(object sender, RoutedEventArgs e)
{
rec2Transform.CenterX = 90;
rotate2Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec2Transform");
}
private void button_Click3_Inverse(object sender, RoutedEventArgs e)
{
rec3Transform.CenterY = 90;
rotate2Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec3Transform");
}
private void button_Click4_Inverse(object sender, RoutedEventArgs e)
{
rec4Transform.CenterX = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec4Transform");
}
private void button_Click5_Inverse(object sender, RoutedEventArgs e)
{
rec5Transform.CenterY = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec5Transform");
}
private void button_Click6_Inverse(object sender, RoutedEventArgs e)
{
rec6Transform.CenterX = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec6Transform");
}
private void button_Click7_Inverse(object sender, RoutedEventArgs e)
{
rec7Transform.CenterY = 90;
rotate2Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec7Transform");
}
The problem is that I want to use these 4 basic storyboards SIMULTANEOUSLY on as many as 20 rectangles at a time, thereby animating all 20 rectangles in unison. But I don't want a static storyboard assigned to each rectangle, because the storyboard I want to apply to each rectangle changes every second or so. I will also be using all 4 storyboards at the same time. (i.e. I will use "oneTwo" for 3 of the rectangles, "twoThree" for 5 of the rectangles, "twoOne" for 8 of the rectangles, and "threeTwo" for 2 of the rectangles)
I hope that all makes sense. The program is based solely on animating many rectangles at the same time, and any help or suggestions would be greatly appreciated.

Try to bind the rectangles in a stackpanel
<Listbox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<Storyboard x:Name="FirstStoryBoard">
<DoubleAnimation Storyboard.TargetName="rectangle"
Storyboard.TargetProperty="Width"
From="0"
To="1"
//AutoReverse="True"
Duration="00:00:01" />
</Storyboard>
</StackPanel.Resources>
<Rectangle Name="rectangle" Fill={Binding colour}>
</Rectangle>
</StackPanel>
</ListBox.ItemTemplate>
</DataTemplate>
</Listbox>
Then Have a Binding in listbox with colours..refer binding in listbox items in web.
then with a button click start the story board. the storyboard children rectangles increases its width
FirstStoryboard.begin();

Related

How can I change the resize mode in the back end in WPF

I have set the initial resize mode of the window to be "NoResize" as shown below.
<Widnow ... WindowStyle="None" ResizeMode="NoResize">
I want to change the resize mode to "CanResizeWithGrip" after this button is clicked.
<Button x:Name="btnResize" Content="Click Me!" Click="BtnResize_Click"/>
And this is my event.
private void BtnResize_Click(object sender, RoutedEventArgs e)
{
}
private void BtnResize_Click(object sender, RoutedEventArgs e)
{
this.ResizeMode = ResizeMode.CanResizeWithGrip;
}

UWP C# Switching frame content issues and crashes

I've struggled with this setup for couple of days and found a somewhat solution but I think it not how it supposed to work.
Here is my xaml page setup:
<Page
...
<SplitView IsPaneOpen="True" DisplayMode="Inline" OpenPaneLength="300">
<SplitView.Pane>
<Grid>
<ToggleButton x:Name="Edit" IsEnabled="False" Checked="Edit_Checked" Unchecked="Edit_Unchecked"/>
</Grid>
</SplitView.Pane>
<Frame x:Name="RightFrame">
</Frame>
</SplitView>
</Page>
Code for toggle button:
private void Edit_Checked(object sender, RoutedEventArgs e)
{
RightFrame.Navigate(typeof(SubPage1));
}
private void Edit_Unchecked(object sender, RoutedEventArgs e)
{
RightFrame.Navigate(typeof(SubPage2));
}
So basically the toggle button switches the splitview content. The SubPages are just blank pages. The problem is that app crashes when navigation occurs.
I've noticed when I put this before invoking navigation:
RightFrame.Content = null;
Thread.Sleep(1000);
Then everything works fine. So I need to clear frame content and wait for it to finish I guess. But I think it should be done automatically. Could some one explain what am I doing wrong here and how it should be done?
Please verify your code in SubPage1 and SubPage2 which might be creating the crashes, you can clear the resources when the page gets unloaded.
Alternatively, you can try below code:
private void Edit_Checked(object sender, RoutedEventArgs e)
{
RightFrame.Content = new SubPage1();
}
private void Edit_Unchecked(object sender, RoutedEventArgs e)
{
RightFrame.Content = new SubPage2();
}

How to pause MediaElement when single tap/touch on the screen - UWP

I have to play a video using MediaElement. I want to pause the video when the user taps the screen. I found that there is double tapped event on MediaElement, but couldn't find any single touch event. How can I do this?
<MediaElement Name="videoMediaElement" AreTransportControlsEnabled="True" Stretch="Fill"
MediaOpened="videoMediaElement_MediaOpened" CurrentStateChanged="Media_State_Changed">
<MediaElement.TransportControls>
<MediaTransportControls Background="Red" Foreground="White"
IsStopButtonVisible="True" IsStopEnabled="True" IsTextScaleFactorEnabled="True"
IsPlaybackRateEnabled="True" IsPlaybackRateButtonVisible="True"
IsFastForwardButtonVisible="True" IsFastForwardEnabled="True"
IsFastRewindButtonVisible="True" IsFastRewindEnabled="True"/>
</MediaElement.TransportControls>
</MediaElement>
private async void Media_State_Changed(object sender, RoutedEventArgs args)
{
if (videoMediaElement.CurrentState == MediaElementState.Paused)
{
}
}
There are plenty of events you can use for that, e.g. the MouseLeftButtonDown or TouchDown events. It's as simple as that:
private void element_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
((MediaElement)sender).Pause();
}
private void element_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
{
((MediaElement)sender).Pause();
}
By #JetChopper:
private void element_PointerPressed(object sender, PointerRoutedEventArgs e)
{
((MediaElement)sender).Pause();
}

Cannot play media Element programmatically

I developed an app in Windows Phone 8. I have a list with mp3 from web and a mediaelement can't play them.
This is my code
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
}
void note1_MediaOpened(object sender, RoutedEventArgs e)
{
play.Play();
}
And xaml is :
<MediaElement HorizontalAlignment="Left" Height="100" Margin="167,538,0,0" VerticalAlignment="Top" Width="100" Name="play" AutoPlay="True"/>
I have try it in xaml and play it but programmatically not. Can someone help me ?
I do not think you need to Play inside MediaOpened event. Just execute .Play after assigning Source to the MediaPlayer
Just add this to button click event
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.Play();
}

Open and Close the same window on the same button click

On my MenuItem at the moment I open up a Window. Then if the person chooses to click on that MenuItem again, I need the Window that was open to close.
Then obviously, if they click it the third time it will open and so fourth.
XAML
<MenuItem x:Name="btnHelp" Click="btnHelp_Click" Foreground="#FF7E8385" FontFamily="Calibri" FontSize="18" Margin="110,10,0,0" Height="30" Width="70" Style="{x:Null}" BorderBrush="Transparent" Background="Transparent" Cursor="Hand"/>
Code behind
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
xamlHelp help = new xamlHelp();
help.Show();
}
You'll need to have the variable be an instance field, rather than a local variable, so it can be accessed between calls. At that point just close it if it exists, and recreate/show it if it doesn't:
private xamlHelp help = null;
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
if (help != null)
{
help.Close();
help = null;
}
else
{
help = new xamlHelp();
help.Show();
}
}
You can keep track of whether the help is currently showing and show/hide based on that,
private xamlHelp help = new xamlHelp();
private bool showingHelp = false;
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
if (showingHelp)
{
help.Hide();
}
else
{
help.Show();
}
}

Categories