How to set a transition property using Transitionals library - c#

I am trying to incorporate the Transitionals library into my window.
I am going to use two transitions:
If button one is pressed the transition is going to be the ExplosionTransition.
If button two is pressed the transition is going to be the RotateTransition to the left.
If button two is pressed again the transition is going to be the RotateTransition to the right.
XAML:
<transc:TransitionElement x:Name="TransitionBox" Transition="{Binding TransitionToUse}" />
ViewModel:
private Transition explosion_transition = new ExplosionTransition();
private Transition rotate_transition = new RotateTransition();
What I am wondering is how do you set the direction property for rotate_direction?

Initialize like so:
private RotateTransition rotate_transition = new RotateTransition();
instead of in the question places it into a Transition object. Then you can access the individual transitions properties, like so:
rotate_transition.Direction = RotateDirection.Right;
TransitionToUse = rotate_transition;

Related

Xamarin: how do I change the color of a button added in code?

My Xamarin app creates buttons in code. I can change the color of a button I click, as the button is referenced in its Clicked handler's sender argument, but I want to change the color of buttons that I didn't click. The problem is, how do I find the buttons I want to change?
I thought about using FindByName, but Name doesn't appear to be an attribute.
One way I can think of: loop through all of the buttons until I find the one with the StyleId of the desired button. Is there an easier way than that?
when you create a button in code you need to keep a reference to it, like you would with any C# object you want to reference later
Button MyButton = new Button { ... };
MyButton.BackgroundColor = Color.Purple;
if you need to access it from multiple places in your code, you should declare it as a class level variable to that it has scope throughout your class
If you want to find the button created in code behind without XAML, like #Jason said, use the variable name of the Button you created or set it directly.
Button button = new Button { ClassId = "button1", BackgroundColor = Color.Red };
If you want to access a view with FindByName, you need to create the button with x:Name in XAML.
There is an XAML example:
<Button x:Name="button1" ></Button>
And in your code behind you could do something like:
button1.BackgroundColor = Color.Red;
If you need to find a view in xaml for some reason, do this:
var button = this.FindByName<Button>("button1");
button.BackgroundColor = Color.Red;

Detect a jump in track bar

I am trying to detect if the user did not click directly on the slider but somewhere on the track bar where it makes the slider value jump by LargeChange property. I am trying to get the value the slider was on before the jump occurred. So for example, if the slider is on the value 40 and the user makes it jump to 140, I need to know the initial value before it was changed.
I know I can use variables to keep track of the previous values, but I have a lot of TrackBars so I will end up having a lot of variables which I am trying to avoid.
Is there some kind of event for the TrackBar I can use to detect the jump or get the initial value before it has been changed by the user? Right now, I'm using the MouseDown event, but that gives me the value of wherever I click on the TrackBar instead of where it was at.
Talking about this TrackBar (the slider being the thing you can move left and right):
A solution that jumps to mind is to use a dictionary. You don't want to have many variables, but a dictionary is only one variable. And I don't believe you having so many trackbars that the memory for the dictionary should be any problem.
So declare a member in your Form class like
private readonly Dictionary<TrackBar, int> trackBarValue = new Dictionary<TrackBar, int>();
And in the handler for the ValueChanged event of the TrackBars you can do
private void TrackBarsValueChanged(object sender, EventArgs e)
{
TrackBar clickedBar = sender as TrackBar;
if (clickedBar == null) return;
if (!trackBarValues.ContainsKey(clickedBar))
trackBarValues[clickedBar] = clickedBar.Value;
if (Math.Abs(trackBarValues[clickedBar] - clickedBar.Value) > clickedBar.LargeChange)
{
// yes, it was a big change
}
// store the current value
trackBarValues[clickedBar] = clickedBar.Value;
}
I don't know if you currently have a different handler for each single trackbar. Since that would mean to repeat that code in every handler, it is possibly a good idea to add this single handler to the events of all trackbars.
See here for how to execute code when a variable changes.
Try something like that, but have the code set something like OldVar equal to TrackVar2, TrackVar2 being the value of TrackBar1 (your actual trackbar int) but only update it after OldVar has been updated first.
This should give you what you want, there is likely a way better solution but this should work.

GUI.Button how do I activate the on hover effects with code?

I'm working on my menu, here I have a couple of classes (represented as GUI buttons) that the player can pick. I have already made them highlighted on hover and also it registers when a class is selected.
The issue I don't know how to solve is, I want the selected class to stay "hovered" after click so it remains highlighted and the player is aware he has selected that class.
if (GUI.Button (new Rect (Screen.width / 2 - 625, 500, 200, 30), "The Cunning", buttonStyle))
{
classValgt = "1";
// Some code here that makes the button appear as if hovered with the effects I added via my GUI style
}
You Can Only Use Hover Effect with GUI Skin or GUI Style,go through the documentation below.
http://docs.unity3d.com/Manual/class-GUIStyle.html
http://docs.unity3d.com/Manual/class-GUISkin.html
In the Project Panel In Unity go to create and select GUISkin ,and make public GUIStyle type Variable in your script and pass your GUISkin on it and code like the Example Below:-
public class ExampleClass : MonoBehaviour
{
public GUIStyle style;
void OnGUI()
{
GUI.skin.label = style;
GUILayout.Label("This is a label.");
Debug.Log("" + GUI.skin.button.hover.textColor);
}
}
I would approach this by using two GUIStyles and a bool "selected" for each object represented for your button. Test the state of your bool before drawing the button and pass the correct style (selected/unselected) to it.
Even better if you could use GUI.Toggle instead of a button (you can change the style to resemble a simple button instead of a toggle). It is way more elegant as it controls selected/unselected status automatically and uses just one style (hold state for unselected and OnHold state for selected).

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.

C# unhandled exception of type 'System.StackOverflowException

Hopefully I can explain this OK. I have a countdown Timer - A user enters the time, etc and picks if they want to open min size or max size from Radio buttons. Depending on which they choose it will load either Min form or Max form where the time will value entered in the UserControl form will be passed across and start to countdown. Now there were buttons for pause/stop,reset etc on the User Control form. I want these to be instead on the Min/Max forms. I was hoping the easiest way for me to do this would be hide the button on the user control form and then try and wire it up to a button on min/max form so if they were pressed it preform like the button on user control was pressed. However I am getting the error in title - it highlights the statement below in yellow (next statement that will be executed)....
(note - this line of code is in the Min form - I need to declare a new instance of it so i can call the function PauseMinClick (the Pause button on MinForm) _ I am wanting it to call the btnPauseClick function which is in CountdownUserControl).
private CountdownUserControl CU = new CountdownUserControl();
private void PauseMin_Click(object sender, EventArgs e)
{
CU.btnPause_Click(sender, e);
}
and highlights the one in green below (next statement to execute when this thread returns from current function)...
private Min _Min = new Min();
(this is in my CountdownUserControl class - note i need an instance of it here to pass across the values which have to countdown. Does anyone know what i should be doing to resolve this easily? Ideally I dont want to have to re-write lots of code - I would just like to get it working with the buttons on the new forms Max/Min but wired up as if they were being pressed on the UserControl form (where they all work fine).
Many Thanks - Colly
It sounds like you have this:
class CountdownUserControl
{
private Min _Min = new Min();
// Other stuff...
}
class Min
{
private CountdownUserControl CU = new CountdownUserControl();
// Other stuff...
}
In other words, to create an instance of Min, you need to create an instance of CountdownUserControl... which in turn needs to create an instance of Min... which in turn needs to create an instance of CountdownUserControl... Do you see why you have a problem?
It's not really clear to me what you're trying to achieve, but this is the cause of the problem. Perhaps one of the classes should take a parameter in its constructor to allow it to refer to an instance of the other?
You say this is in your CountdownUserControl class? If so, this is the problem:
private CountdownUserControl CU = new CountdownUserControl();
It creates a new CountdownUserControl, which creates a CountdownUserControl, which.....etc until the stack overflows

Categories