Storyboard.begin() throws an InvalidOperationException - c#

I am not sure what is going on here. I am following the example provided by Microsoft. Everything is done on the back end because I need to decide if you user should enter stuff into a text field or should the text field value be displayed as normal text. The code is as follows:
nameInput.Name = "inputName";
nameInput.Text = "Journey Name";
nameInput.KeyUp += onNameInput;
ColorAnimation animation = new ColorAnimation();
animation.From = Colors.Blue;
animation.To = Colors.White;
animation.Duration = new Duration(TimeSpan.FromMilliseconds(100));
animation.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget(animation, nameInput);
Storyboard.SetTargetProperty(animation, new PropertyPath(TextBlock.ForegroundProperty));
storyBoard.Children.Add(animation);
journeyStackPanel.Children.Add(nameInput);
ClockState state = storyBoard.GetCurrentState();
storyBoard.Begin(); //<---Crashes here
I am following the
http://msdn.microsoft.com/en-us/library/cc672995(v=vs.95).aspx
example. I am not sure what is going on, unfortunately the debugger does not spit out any more information. Maybe I am missing a step? I am sorry that I am being a little vague but this is all the information I have on the issue.
Any help is greatly appreciated!!

I was able to replicate this problem on the latest WP8 SDK, with the following error message generated:
ColorAnimation cannot be used to animate property Foreground due to
incompatible type.
I believe this is because you're trying to change the Foreground property of the TextBox to a Color object, but Foreground is actually a Brush object, hence the Type Mismatch error. Instead, you have to change the Color property of the Foreground object.
Try this instead:
Storyboard.SetTargetProperty(animation, new PropertyPath("(Foreground).(Color)"));

Related

WPF Animate Grid.VisibilityProperty in code

What animation class would allow me to change the Visibility (not opacity) of a Grid object with a Storyboard instance in code (not XAML)?
So that I can set the to, from, and duration properties before adding it to the storyboard.
You can use an ObjectAnimationUsingKeyFrames with some DiscreteObjectKeyFrame.
You can find an example here. The only work to do is translating that to C# code. (Which shouldn't be a huge problem.)
This is the code necessary to animate the visibility.
DiscreteObjectKeyFrame dk;
ObjectAnimationUsingKeyFrames ok;
ok = new ObjectAnimationUsingKeyFrames();
dk = new DiscreteObjectKeyFrame();
Storyboard.SetTarget(ok, myGrid);
Storyboard.SetTargetProperty(ok, new PropertyPath(Grid.VisibilityProperty));
dk.KeyTime = TimeSpan.FromSeconds(0.1);
dk.Value = Visibility.Hidden;
ok.KeyFrames.Add(dk);
sb.Children.Add(ok);

Custom Animation Port to Universal App from WP7 fails with SetTargetProperty

Porting custom animation code from WP7 to store app. WP7 code successfully performed a page flip animation of a border object with a bunch of text boxes on it (that was a page to be flipped.) In the below code Storyboard.SetTargetProperty does not compile complaining that it wants a string:
DoubleAnimation anima = new DoubleAnimation
{
To = pageHostBaseIndex + 1,
Duration = CalculateFractionalDuration(pageHostBaseIndex + 1)
};
Storyboard storyboard = new Storyboard();
Storyboard.SetTarget(anima, this.PageTransition);
Storyboard.SetTargetProperty(anima, new PropertyPath(PageTransition.FractionalBaseIndexProperty));
storyboard.Children.Add(anima);
storyboard.Completed += Storyboard_Completed;
storyboard.Begin();
PageTransition derives from DependencyObject, it contains a DependencyProperty called FractionalBaseIndexProperty.
I tried putting in the string "PageTransition.FractionalBaseIndexProperty" as well as constructing a PropertyPath string. I also tried "(PageTransition).(FractionalBaseIndexProperty)" these all compile but fail with the exception:
No installed components were detected.
Cannot resolve TargetProperty PageTransition.FractionalBaseIndexProperty on specified object.
at Windows.UI.Xaml.Media.Animation.Storyboard.Begin()
I also tried EnableDependentAnimation = true, and making PageTransition derive from Timeline instead of DependencyObeject but these had no effect (same error.)
The eventual animation is a little complex but I don't think it's getting that far. Seems like a Silverlight to Universal difference in objects acceptable for binding to a storyboard or in something with the path. I'll bet there's a more XAML friendly way to do this now but at this point I'm trying to minimize the port and I'd like to keep the feel of the currently animation.
Thoughts?
After hours of tinkering and searching and then reverting some of my previous tinkerings I finally got past this. This thread got me thinking:
Windows 8 - Animating custom property in code-behind.
My border objects were already declared in XAML but my storyboard, animation and PageTransition objs weren’t.
So I added the following XAML in my UserControl.Resources:
<Storyboard x:Name="storyboard">
<DoubleAnimation x:Name="anima">
</DoubleAnimation>
</Storyboard>
<local:FlipTransition x:Key="Foo"></local:FlipTransition>
I used FlipTransition because in my case the PageTransition is an abstract class (so XAML wouldn’t let me instantiate it.) FlipTranstion derives from PageTransition.
Along with that I had to re-create new storyboards and animation objects (see below) with the same names as the ones in the above XAML (I don’t know why the ones I instantiated in XAML wouldn’t work.)
I also had to set EnableDependentAnimation = true
And last, the Path in SetTargetProperty had to change to PageTransition. FractionalBaseIndex (instead of the original PageTransition.FractionalBaseIndexProperty.)
DoubleAnimation anima = new DoubleAnimation
{
To = pageHostBaseIndex + 1,
Duration = CalculateFractionalDuration(pageHostBaseIndex + 1),
EnableDependentAnimation = true
};
Storyboard storyboard = new Storyboard();
Storyboard.SetTarget(anima, this.PageTransition);
Storyboard.SetTargetProperty(anima, "PageTransition.FractionalBaseIndex");
When all these came together it worked.

Object does not match target type

I have a TableLayoutPanel with a grid of PictureBox controls within it. I'm trying to find a shortcut way to change them all to Label controls instead of manually deleting each one and placing new controls in each cell.
I thought I could go into the designer code and find/replace PictureBox with Label, but now I get an
"Object does not match target type"
error in Visual Studio's error list. I can't view the designer page now either. Is this not allowed? If it is allowed, what's the right way to do it?
If you take a closer look at the generated code:
label1:
this.label1 = new System.Windows.Forms.Label();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 163);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
pictureBox1:
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(97, 75);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
My guess is that the
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
is changed by you into something like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
which doesn't work, and results in designer issues. Object does not match target type.
so, apply the changes you already did, remove the lines like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.label1)).EndInit();
and I think you're good to go.
Don't change designer code. That stuff is automatically generated. Not only can your changes cause unexpected behavior, but they can get over-written as well.
I would attempt to make a change or 2 to your form, or whatever your designer is behind, and hope it regenerates all it's code.
You can delete all the picture boxes in the designer, then add all the labels in the _load event (or another convenient event). That way it will be easier to change next time.
As Haxx illustrated, you will have to clean-up the extra initialization PictureBox requires as well. The error you received is a interface casting error. In your case, as Haxx guessed, the Label control doesn't implement the ISupportInitialize interface.
Unlike most, I am not afraid of changing designer code in the interest of expediency, for what you are doing, it is ok to do so. Just know your objects, check-in prior to doing so, and don't put custom code in there!

C# equivalent of Delphi's DisableControls/EnableControls

What is the C# equivalent of Delphi's DisableControls/EnableControls methods (used to disable updating of databound controls while iterating through the underlying dataset)? I have googled for half an hour and did not find an answer...
I have a list box and a rich edit box bound to a binding source, but I need to do an operation that iterates through the entire dataset, and both controls get updated as I move through the underlying dataset. In Delphi this is easy enough: enclose the block that does the iteration between DisableControls and EnableControls. I can't find the C#/.NET equivalent, and I have looked really hard!
IIRC, setting Enabled to false does not prevent the controls from reacting to data changes in WinForms.
Collection-bound controls like the ListBox typically have methods BeginUpdate() and EndUpdate() which temporarily disable visual updates.
Also, the property mentioned by DarkSquirrel might be worth a look
I don't have access to Visual Studio right now, so I can't test this, but look through the methods for the control instance. Code such as:
// set the Enabled property of
// the controls to False; this should
// disable the controls for user access
listBox.Enabled = False;
richEditBox.Enabled = False;
// perform iteration
// and other operations
// set the Enabled property back
// to True
listBox.Enabled = True;
richEditBox.Enabled = True;
The exact name of the property may differ slightly, but I'm pretty sure that this is what it is.
I assume you are using WinForms, in that case you can try using the methods SuspendLayout/ResumeLayout.
Code sample from MSDN:
private void AddButtons()
{
// Suspend the form layout and add two buttons.
this.SuspendLayout();
Button buttonOK = new Button();
buttonOK.Location = new Point(10, 10);
buttonOK.Size = new Size(75, 25);
buttonOK.Text = "OK";
Button buttonCancel = new Button();
buttonCancel.Location = new Point(90, 10);
buttonCancel.Size = new Size(75, 25);
buttonCancel.Text = "Cancel";
this.Controls.AddRange(new Control[]{buttonOK, buttonCancel});
this.ResumeLayout();
}
So far I know, you don't need to Disiable/EnableControls in C#, since this type of DataSet doesn't work with a current cursor, like Delphi TDataSets.

Adding controls to TabItems dynamically at runtime in C#

So, here is my issue, im working in a Wpf application, so i dont have all of the standard windows form controls.. so im attempting to use a windowsformshost, to hold a webbrowser inside of a tabitem. So here is what i have:
Tab t = new Tab();
Browser newbrowse = new Browser(t);
WindowsFormsHost host = new WindowsFormsHost();
Grid g = new Grid();
host.Child = newbrowse;
newbrowse.Dock = DockStyle.Fill;
g.Children.Add(host);
t.Header = "New Tab";
t.Content = g;
tabControl1.Items.Add(t);
now, Tab, and Browser, are just my custom implementations of the controls, and they both are tested and working. So that is not the issue. Now as far as i can see, that code should work. But I'm left staring at a blank tab page. Note that this will need to be in the codebehind and cant be included in the WPF itself.
Thanks for any input! :) Cheers
EDIT: Note that i have also tried the operation with the standard, controls.tabitem, and forms.webbrowser to the same effect
I just tried this, and apparently what breaks your plan is the line:
newbrowse.Dock = DockStyle.Fill;
Comment it out, and watch how it suddenly works!

Categories