Page 1 of 1

Are VisualStates supported in UserControl?

Posted: 18 Mar 2014, 07:44
by timashev
Hi,

I'm trying to use VisualStates in my UserControl, but when I call VisualStateManager.GoToState nothing happens:
VisualStateManager.GoToState(this, "SomeState", false);
My UserControl is designed in Blend 2013, so it should be the correct XAML, it looks like this:
<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	x:Class="My.UserControls.RealtimeData"
	x:Name="realtimeDataRoot"
	d:DesignWidth="450" d:DesignHeight="245">

	<Border x:Name="realtimeDataPanel">
		<VisualStateManager.VisualStateGroups>
			<VisualStateGroup x:Name="RealtimeDataVisualStateGroup">
				<VisualState x:Name="InitialState"/>
				<VisualState x:Name="InitialAppleState">
					<Storyboard>
						<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="coordinatesPanel">
							<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
						</ObjectAnimationUsingKeyFrames>
					</Storyboard>
				</VisualState>
				<VisualState x:Name="InProgressState">
					<Storyboard>
						<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="coordinatesPanel">
							<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
						</ObjectAnimationUsingKeyFrames>
					</Storyboard>
				</VisualState>
				<VisualState x:Name="FailedState">
					<Storyboard>
						<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="coordinatesPanel">
							<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
						</ObjectAnimationUsingKeyFrames>
					</Storyboard>
				</VisualState>
				<VisualState x:Name="OKState"/>
			</VisualStateGroup>
		</VisualStateManager.VisualStateGroups>
		<Viewbox>
			<StackPanel HorizontalAlignment="Center">
				<Grid x:Name="coordinatesPanel" HorizontalAlignment="Center" VerticalAlignment="Center">
					[...]
				</Grid>
			</StackPanel>
		</Viewbox>
	</Border>
</UserControl>
And the code behind is:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Noesis;

namespace My.UserControls
{

[Noesis.Extended]
[Noesis.UserControlSource("Assets/Script/RealtimeData.xaml")]
public class RealtimeData : Noesis.UserControl
{
    public enum RealtimeState { InitialState, InitialAppleState, InProgressState, FailedState, OKState };
...
    private void OnStateChanged(RealtimeState state)
    {
        Debug.Log("RealtimeState = " + state.ToString());
        VisualStateManager.GoToState(this, state.ToString(), false);
    }
}
OnStateChanged is actually called, I see the debug output, but the visibility of the "coordinatesPanel" control is not changed. I tried to animate other properties like TextBlock.Text--it doesn't work as well.

Am I right that I use 'this' as a root element in UserControl to switch the VisualStates?

Re: Are VisualStates supported in UserControl?

Posted: 18 Mar 2014, 12:22
by sfernandez
It seems that we are not able to find the VisualStateGroup when using VisualStateManager.GoToState() on a UserControl. We will try to fix it as soon as possible.

Meanwhile you should use then VisualStateManager.GoToElementState() by providing the root element that contains the VisualStateGroup. In your example, that root would be the Border named "realtimeDataPanel".
namespace My.UserControls
{

[Noesis.Extended]
[Noesis.UserControlSource("Assets/Script/RealtimeData.xaml")]
public class RealtimeData : Noesis.UserControl
{
    public enum RealtimeState { InitialState, InitialAppleState, InProgressState, FailedState, OKState };

    private Noesis.Border stateGroup;

    public void OnPostInit()
    {
        this.stateGroup = FindName<Noesis.Border>("realtimeDataPanel");
    }

    private void OnStateChanged(RealtimeState state)
    {
        Debug.Log("RealtimeState = " + state.ToString());
        VisualStateManager.GoToElementState(this.stateRoot, state.ToString(), false);
    }
}

}

Re: Are VisualStates supported in UserControl?

Posted: 18 Mar 2014, 15:45
by timashev
Thanks a lot! It works fine now!