I am new with Windows Phone animation and using the below code but it give me compile error:
'System.Windows.Controls.Button' does not contain a definition for 'BeginAnimation' and no extension method 'BeginAnimation' accepting a first argument of type 'System.Windows.Controls.Button' could be found (are you missing a using directive or an assembly reference?)
Which reference I am missing?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
private void button1_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation();
da.From = 30;
da.To = 100;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
button1.BeginAnimation(Button.HeightProperty, da);
}
The UIElement.BeginAnimation method does not exist in WP7. Instead, you will need to create a storyboard as follows:
private void button1_Click(object sender, RoutedEventArgs e)
{
var sb = new Storyboard();
var db = CreateDoubleAnimation(30, 100,
button1, Button.HeightProperty, TimeSpan.FromMilliseconds(1000));
sb.Children.Add(db);
sb.Begin();
}
private static DoubleAnimation CreateDoubleAnimation(double from, double to,
DependencyObject target, object propertyPath, TimeSpan duration)
{
var db = new DoubleAnimation();
db.To = to;
db.From = from;
db.Duration = duration;
Storyboard.SetTarget(db, target);
Storyboard.SetTargetProperty(db, new PropertyPath(propertyPath));
return db;
}
Related
I'm learning wpf and at the same time developing an app with it. I'm having a hard time figuring out how i can run something when a doubleanimation (Or other sorts) is done. For instance:
DoubleAnimation myanim = new DoubleAnimation();
myanim.From = 10;
myanim.To = 100;
myanim.Duration = new Duration(TimeSpan.FromSeconds(3));
myview.BeginAnimation(Button.OpacityPropert, myanim);
//Code to do something when animation ends
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;
namespace app
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation widthbutton = new DoubleAnimation();
widthbutton.From = 55;
widthbutton.To = 100;
widthbutton.Duration = new Duration(TimeSpan.FromSeconds(1.5));
button1.BeginAnimation(Button.HeightProperty, widthbutton);
DoubleAnimation widthbutton1 = new DoubleAnimation();
widthbutton1.From = 155;
widthbutton1.To = 200;
widthbutton1.Duration = new Duration(TimeSpan.FromSeconds(1.5));
button1.BeginAnimation(Button.WidthProperty, widthbutton1);
widthbutton.Completed += new EventHandler(myanim_Completed);
}
private void myanim_Completed(object sender, EventArgs e)
{
//your completed action here
MessageBox.Show("Animation done!");
}
}
}
How is this accomplishable? I have read quite a few other posts about this, but they all explain it using xaml, however i would like to do it using c# code. Thanks!
You can attach an event handler to the DoubleAnimation's Completed event.
myanim.Completed += new EventHandler(myanim_Completed);
private void myanim_Completed(object sender, EventArgs e)
{
//your completed action here
}
Or, if you prefer it inline, you can do
myanim.Completed += (s,e) =>
{
//your completed action here
};
Remember to attach the handler before starting the animation otherwise it won't fire.
My Form contains a button and a chart added as shown below.
My code is built such that a separate thread continuously gets data from the sender (which is being sent using the UDP protocol of communication), processes it and adds it to the global GLineSeries object 'gls'. GLineSeries is basically a class of the library which is basically just a list of the datapoints of the graph. My aim is that when the button is clicked this series is added to the chart in the form (cartesianChart1) and the plot shows. This is done using the line cartesianChart1.Series.Add(gls); The code for this is shown below (Form1.cs file)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.WinForms;
using LiveCharts.Wpf;
using LiveCharts.Defaults;
using LiveCharts.Geared;
using System.Windows.Shell;
namespace livecharts_example
{
public partial class Form1 : Form
{
LiveCharts.WinForms.CartesianChart cartesianChart1 = new LiveCharts.WinForms.CartesianChart();
GLineSeries gls;
Thread t;
public Form1()
{
InitializeComponent();
cartesianChart1.Dock = DockStyle.Fill;
this.Controls.Add(cartesianChart1);
t = new Thread(() => {
UdpClient dataUdpClient = new UdpClient(90);
string carIP = "127.0.0.1";
IPEndPoint carIpEndPoint = new IPEndPoint(IPAddress.Parse(carIP), 0);
Byte[] receiveBytes;
gls = new GLineSeries();
gls.Values = new GearedValues<ObservablePoint>();
while (true)
{
receiveBytes = dataUdpClient.Receive(ref carIpEndPoint);
ObservablePoint op = new ObservablePoint(BitConverter.ToInt32(receiveBytes, 0), BitConverter.ToSingle(receiveBytes, 8));
gls.Values.Add(op);
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
cartesianChart1.Series.Add(gls);
}
}
}
The problem is that when the button is pressed the program jumps to the program.cs file and throws the error as shown below. I also tried aborting the thread 't' and then adding the lineseries to the chart but the error still arises. Can someone please help?
The following code worked. Thanks for all the suggestions
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.WinForms;
using LiveCharts.Wpf;
using LiveCharts.Defaults;
using LiveCharts.Geared;
using System.Windows.Shell;
using System.Windows;
using System.Collections.ObjectModel;
namespace livecharts_example
{
public partial class Form1 : Form
{
LiveCharts.WinForms.CartesianChart cartesianChart1 = new LiveCharts.WinForms.CartesianChart();
Thread t;
static GLineSeries gls;
public Form1()
{
InitializeComponent();
cartesianChart1.Dock = DockStyle.Fill;
this.Controls.Add(cartesianChart1);
t = new Thread(() => {
UdpClient dataUdpClient = new UdpClient(90);
string carIP = "127.0.0.1";
IPEndPoint carIpEndPoint = new IPEndPoint(IPAddress.Parse(carIP), 0);
Byte[] receiveBytes;
cartesianChart1.Invoke(new Action(() =>
{
Form1.gls = new GLineSeries(); Form1.gls.Values = new GearedValues<ObservablePoint>();
}));
while (true)
{
receiveBytes = dataUdpClient.Receive(ref carIpEndPoint);
ObservablePoint op = new ObservablePoint(BitConverter.ToInt32(receiveBytes, 0), BitConverter.ToSingle(receiveBytes, 8));
cartesianChart1.Invoke(new Action(() => {
gls.Values.Add(op);
}));
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
cartesianChart1.Invoke(new Action(()=> {
cartesianChart1.Series.Add(gls);
}));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Media;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace Calculator_Assessment
{
...
private void mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Random rand = new Random();
string copyPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "/Desktop/meme" + rand.Next() + ".mp4";
for (int i = 0; i < 3; i++)
{
copyPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "/Desktop/meme" + rand.Next() + ".mp4";
File.Copy("meme.mp4", copyPath, true);
}
e.Cancel = true;
new MainWindow(0).Show();
}
}
}
There is my code, basically when the user tries to close the application it runs another instance of itself etc etc. This work perfectly when loaded from the .exe itself but when called from a batch file;
#echo off
start "Calculator Assessment.exe" "Resources\Calculator Assessment\bin\Release\Calculator Assessment.exe"
It doesn't work. Any ideas? All the program does when loaded from this batch (and when i try to exit), is hang for a sec and then seemingly crash.
The problem likey comes from the line
File.Copy("meme.mp4", copyPath, true);
Your program will only check the current working directory for the file meme.mp4. You need to either ensure the working directory is set to the folder of your executable in the batch file or use a absolute path for the file you are trying to read.
private void mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Random rand = new Random();
for (int i = 0; i < 3; i++)
{
var copyPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"meme" + rand.Next() + ".mp4");
var sourceDir = Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
File.Copy(Path.Combine(sourceDir, "meme.mp4"), copyPath, true);
}
e.Cancel = true;
new MainWindow(0).Show();
}
I also updated your example to use Environment.GetFolderPath instead of reading the USERPROFILE env variable.
How could i update the windows audio playback agent properties like title and artist, live ,when it is playing. I want to update the title every 20 seconds without stopping the music which is playing? please help. I tried this but its stopping and replaying at every 20 seconds which is annoying. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.BackgroundAudio;
using System.Threading;
using System.Windows.Threading;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
DispatcherTimer timer = new DispatcherTimer();
string Artist;
string Song;
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
timer.Interval = TimeSpan.FromSeconds(20);
timer.Start();
timer.Tick += timer_Tick;
}
void timer_Tick(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
client.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.Now.ToString();
client.DownloadStringAsync(new Uri("http://air-online2.hitfm.md/status_hitfm.xsl"));
client.DownloadStringCompleted += client_DownloadStringCompleted;
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string[] FullTitle = e.Result.Substring(166).Split('-');
Artist= FullTitle[0];
if(Artist.Contains(":"))
{
Artist=FullTitle[0].Replace(":",string.Empty);
}
Song = FullTitle[1].Replace(" ", string.Empty);
if (BackgroundAudioPlayer.Instance.PlayerState != PlayState.Playing)
{
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri("http://air-online2.hitfm.md/hitfm.mp3"), Artist, Song, null, null);
BackgroundAudioPlayer.Instance.Play();
}
else
{
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri("http://air-online2.hitfm.md/hitfm.mp3"), Artist, Song, null, null);
}
}
}
}
I want to use TeeChart (http://www.teechart.net/) ScrollPager tool in my WPF project, but it doesn’t work. (TeeChart.WPF.dll version 4.1.2012.2287)
http://img190.imageshack.us/img190/4900/scrwpf.png
namespace TeeChart
{
using Steema.TeeChart.WPF.Themes;
using Steema.TeeChart.WPF.Tools;
using Steema.TeeChart.WPF.Styles;
using Steema.TeeChart.WPF.Drawing;
public partial class MainWindow
{
private Line _series;
private ScrollPager _tool;
BlackIsBackTheme _black;
public MainWindow()
{
InitializeComponent();
_series = new Line();
tChart1.Series.Add(_series);
tChart1.Chart.Aspect.View3D = false;
tChart1.Header.Visible = false;
tChart1.Legend.Visible = false;
_series.FillSampleValues(500);
_black = new BlackIsBackTheme(tChart1.Chart);
_black.Apply();
_tool = new ScrollPager();
tChart1.Tools.Add(_tool);
_tool.Series = _series;
_black = new BlackIsBackTheme(_tool.SubChartTChart.Chart);
_black.Apply();
_tool.SubChartTChart.Panel.Pen.Visible = false;
_tool.SubChartTChart.Panel.Bevel.Inner = BevelStyles.None;
_tool.SubChartTChart.Panel.Bevel.Outer = BevelStyles.None;
}
}
}
In WinForms project it works fine.
http://img692.imageshack.us/img692/4527/scrwinforms.png
namespace TeeChartWinForms
{
using System.Windows.Forms;
using Steema.TeeChart.Drawing;
using Steema.TeeChart.Styles;
using Steema.TeeChart.Themes;
using Steema.TeeChart.Tools;
public partial class Form1 : Form
{
private Line _series;
private ScrollPager _tool;
BlackIsBackTheme _black;
public Form1()
{
InitializeComponent();
_series = new Line();
tChart1.Series.Add(_series);
tChart1.Chart.Aspect.View3D = false;
tChart1.Header.Visible = false;
tChart1.Legend.Visible = false;
_series.FillSampleValues(500);
_black = new BlackIsBackTheme(tChart1.Chart);
_black.Apply();
_tool = new ScrollPager();
tChart1.Tools.Add(_tool);
_tool.Series = _series;
_black = new BlackIsBackTheme(_tool.SubChartTChart.Chart);
_black.Apply();
_tool.SubChartTChart.Panel.Pen.Visible = false;
_tool.SubChartTChart.Panel.Bevel.Inner = BevelStyles.None;
_tool.SubChartTChart.Panel.Bevel.Outer = BevelStyles.None;
}
}
}
Thanks,
Alex
Seems that your problem doesn't appear using next code:
Code c#:
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 Steema.TeeChart.WPF;
using Steema.TeeChart.WPF.Styles;
using System.IO;
using System.Windows.Markup;
using Steema.TeeChart.WPF.Tools;
using Steema.TeeChart.WPF.Drawing;
using Steema.TeeChart.WPF.Themes;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
private Steema.TeeChart.WPF.Styles.Line series;
private ScrollPager tool;
BlackIsBackTheme black;
public MainWindow()
{
InitializeComponent();
series = new Steema.TeeChart.WPF.Styles.Line();
tChart1.Series.Add(series);
tChart1.Chart.Aspect.View3D = false;
tChart1.Header.Visible = false;
tChart1.Legend.Visible = false;
series.FillSampleValues(500);
black = new BlackIsBackTheme(tChart1.Chart);
black.Apply();
tool = new ScrollPager(tChart1.Chart);
tool.Series = series;
black = new BlackIsBackTheme(tool.SubChartTChart.Chart);
black.Apply();
tool.SubChartTChart.Panel.Pen.Visible = false;
tool.SubChartTChart.Panel.Bevel.Inner = BevelStyles.None;
tool.SubChartTChart.Panel.Bevel.Outer = BevelStyles.None;
}
}
}
code xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tc="clr-namespace:Steema.TeeChart.WPF;assembly=TeeChart.WPF"
Title="MainWindow" SizeToContent="WidthAndHeight" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="516" d:DesignWidth="492">
<Grid >
<AdornerDecorator Margin="25,12,27,65">
<tc:TChart Name="tChart1" Height="226" Width="412" />
</AdornerDecorator>
</Grid>
</Window>
And I get the same results as winforms. Please try again if using my code your problem are solved or it still appears. If your problem doesn't solve could you please send a simple example project we can run "as-is" to reproduce the problem here at steema.net/upload?
Thanks,
Best regards
Sandra