I'm coding an app using xamarin, I have some states defined and the thing changes nicely when changing the IsEnabled (for instance).
Now I'd like to have this state change to be an animation, I checked online and everybody leads to VisualStateGroup.Transitions but intellisense keeps saying is not recogniced.. I guess this is only a WPF thing..
Is there an alternative for Xamarin ?
This is how it looks so far:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:DataType="FrameWork:CardModel"
x:Class="Viernes_ElJuego.CardViewer" IsEnabled="False">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition To="Disabled" GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState Name="Normal"/>
<VisualState Name="Disabled">
<VisualState.Setters>
<Setter Property="WidthRequest" Value="100"/>
<Setter TargetName="Cover" Property="BoxView.Opacity" Value="1"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentView.Content>
...[my content]
As I said, if I remove the transitions thing, the rest works as expected. Just the transition is instantanous and "ugly"
Related
I am working on UWP (Universal Windows Platform app). I have issue regarding Mobile view. I set an image background in my login page of the application .Which is properly visible in all view(Tabulate and desktop) but when i open application in Emulator(Mobile view) the background image is not visible and background is black.
code:
<Grid x:Name="login_page">
<Grid.Background>
<ImageBrush Stretch="UniformToFill" ImageSource="Images/LoginImages/login_bg01.png"/>
</Grid.Background>
VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="420" />
</VisualState.StateTriggers>
<VisualState.Setters>
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="320" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="txtname.Height" Value="40"></Setter>
<Setter Target="pwdbox.Height" Value="40"></Setter>
<Setter Target="txturl.Height" Value="40"></Setter>
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="720" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="pwdbox.Height" Value="60"></Setter>
<Setter Target="txturl.Height" Value="60"></Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Name="maingrid">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
//Here define three textblock
<StackPanel Grid.Row="4" Orientation="Horizontal" Margin="0,0,0,0">
<TextBlock x:Name="uname">
<TextBlock x:Name="email">
<TextBlock x:Name="password">
</StackPanel>
/Grid>
</Grid>
Image
Quickly create a new universal app for testing. Add just the grid background to see if it works, if not simply use an image. Otherwise add xaml of your app step by step to the new test app until it stops working. Btw you might also want to take a look at RelativePanel.
Why dont you add an image control in the grid itself ?
<Grid x:Name="login_page">
<Image x:Name="Image1" Source="Images/LoginImages/login_bg01.png"/>
..........
/>
I have experienced issues previously with setting the path to an Image or ImageSource in the relative format, ex. Path/To/Image.png. To solve this, append the ms-appx:/// protocol to the path, so instead of having
<ImageBrush Stretch="UniformToFill" ImageSource="Images/LoginImages/login_bg01.png"/>
you have
<ImageBrush Stretch="UniformToFill" ImageSource="ms-appx:///Images/LoginImages/login_bg01.png"/>
I'd like to make an image button. With a image for the default status and a different image for the pressed status. With no other pressed highlights.
How can I implement that?
You can achieve that by defining your own style for Button control. I'll show you my example.
First, I have added two images to my project.
Normal.png
Pressed.png
Then, I have definied new style in App.xaml file based on default Button style.
<Application.Resources>
<ImageBrush x:Name="ImageButtonNormal" ImageSource="Assets/ImageButton/Normal.png" />
<ImageBrush x:Name="ImageButtonPressed" ImageSource="Assets/ImageButton/Pressed.png" />
<Style x:Key="ImageButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Grid" Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ImageButtonPressed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border"
Background="{StaticResource ImageButtonNormal}" >
<ContentPresenter x:Name="ContentPresenter"
AutomationProperties.AccessibilityView="Raw"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
As you can see, I have definied 2 ImageBrush objects for 2 button states. The look of the button is defined as Border with some content. We want to change the background of the Border. VisualStateGroups will handle this. I have added Storyborad in VisualState named Pressed to change the background of the Border just by switching ImageBrush. Simple!
The last thing you have to do is to apply the new style to your Button in XAML. I did it in MainPage.xaml.
<Button Style="{StaticResource ImageButtonStyle}" />
You can user "PointerPressed" and "PointerReleased" events of the Image control to do that and change the Source property in each one. This will make Image flickers, so you can solve this problem by checking this Link
I don't know if I named the question right but here it is
First of all I am very new to Blend and never used it before in my life. I decided to make a simple animation today and things happened but not as expected.
I'm animating a button and the animation is just a simple 360 degrees flip on the Y axis. I created the animation for a specific button. It worked exactly how I wanted with this code
TiltAnimation.Begin();
However when I tried to create a ControlTemplate with this animation this happened. I need to click and hold the button in order for the animation to finish. I set the animation to play on a specific state: 'Pressed' (to be more specific it was pressed -> * which I suppose means 'from pressed state to any state'). Am I doing something wrong or am I missing something
Note: I'm experimenting on the windows phone platform.
If you need any code tell me in the comments
EDIT:Here is the XAML code from the page and from the App.xaml
Page code:
<Button Grid.Column="1"
Height="60"
Width="60"
BorderThickness="1"
Background="White"
Opacity="0.8"
Template="{StaticResource ButtonControlTemplate1}"
Click="PlayButton_Click">
</Button>
App.xaml code:
<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
<Grid x:Name="grid">
<Grid.Resources>
<Storyboard x:Name="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<Grid.Projection>
<PlaneProjection/>
</Grid.Projection>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="Pressed" GeneratedDuration="0">
<VisualTransition.GeneratedEasingFunction>
<BackEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
<Storyboard>
<DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="grid"/>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image x:Name="image" Source="/Assets/Icons/Previous.png">
<Image.Projection>
<PlaneProjection/>
</Image.Projection>
</Image>
<Ellipse Height="Auto" Width="Auto" Stroke="White" StrokeThickness="4"/>
</Grid>
</ControlTemplate>
I made a simple button template from the rectangle with "Make into control../Button" command. Now, I need several buttons from that template but each button must have different background image. I tried to do that in Blend4 but when I change the button background, button holds to a template background (or none, whichever is set in template), ignoring the image I have set for that specific button.
Button Template:
btnMenu (No brush)
-rectangle (No brush)
-[ContentPresenter] (some text)
I apreciate any advice.
Style:
<Style x:Key="btnStyleMenuHome" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="btnMenu" Width="90" Height="70" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.Background>
<ImageBrush Stretch="None"/>
</Grid.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(TileBrush.Stretch)" Storyboard.TargetName="rectangle">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Stretch>Uniform</Stretch>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" RadiusY="5" RadiusX="5" StrokeThickness="0" Width="90" Height="70" VerticalAlignment="Center" HorizontalAlignment="Center">
<Rectangle.Fill>
<ImageBrush Stretch="None" ImageSource="images/someImage.png"/>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter VerticalAlignment="Bottom" Margin="7,0" d:LayoutOverrides="Width" Height="25" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<ImageBrush Stretch="None"/>
</Setter.Value>
</Setter>
</Style>
Have a look at Custom UserControl Property used by child element
I don't know why I'm having so much trouble doing this, it shouldn't be hard, but I must be Blend-incompetent. Can someone give me the xaml for an image style where the image is at 60% opacity, on mouseover fades in to 100, mouseout back to 60% and onclick glows for a 0,2 sec.
Or just tell me how to do in blend?
thank you
Solution turned out to be simple enough:
<Style x:Key="FadeImageButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" Width="16" Height="16">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.2"/>
<VisualTransition GeneratedDuration="0:0:0.2" To="Normal"/>
<VisualTransition GeneratedDuration="0:0:0.2" To="MouseOver"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You are certainly not incompetent. Images do not have states, so a style is not the answer.
The only styles you can create for images are for one fixed state, so you could add the 60% opacity, but not much else.
Your options are:
Create EnterImage and LeaveImage
storyboards that are played with ControlStoryboardAction behaviours (on MouseEnter and MouseLeave events).
Create a custom behaviour and attach that to the images.
Place the image in another control that has states (maybe a button)
Place the image in a user control with an image property
Create a custom control
The simplest is option 1, but it requires attaching several properties to each image so more drags and clicks to author them.
If you let us know which option you prefer I may be able to post an example.