Page 1 of 1

[Unity] FindTypeResource C++ Runtime Error

Posted: 29 Jul 2014, 00:15
by rcalt2vt
Hey All, I keep getting a C++ Runtime Error every time I call FindTypeResource<Storyboard>("FadeOut") or FindTypeResource<Storyboard>("FadeIn") in the below example. Any thoughts on what I am doing wrong? I'm honestly out of ideas.

UIEscapeScreen.cs
using Noesis;
using System;

namespace FiveGui
{
    [Noesis.Extended]
    [Noesis.UserControlSource("Assets/GUI/GUIControls/UIEscapeScreen.xaml")]
    public class UIEscapeScreen : Noesis.UserControl
    {
        private Grid _screenPnl;

        public void OnPostInit()
        {
            _screenPnl = FindName<Grid>("_ScreenRoot");
        }

        public void ToggleEscapeScreen()
        {
            if (_screenPnl.GetVisibility() == Visibility.Visible)
            {
                Storyboard board = FindTypeResource<Storyboard>("FadeOut");
                board.Begin(this);
            }
            else
            {
                Storyboard board = FindTypeResource<Storyboard>("FadeIn");
                board.Begin(this);
            }
        }
    }
}
UIEscapeScreen.xaml
<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"
	xmlns:noesis="clr-namespace:NoesisGUIExtensions" xmlns:five="clr-namespace:FiveGui"
	mc:Ignorable="d" x:Class="FiveGui.UIEscapeScreen" x:Name="_UIEscapeScreenControl" d:DesignWidth="1920" d:DesignHeight="1080">

	<Grid x:Name="_ScreenRoot" Background="#00000000" Height="1080" Visibility="Collapsed">
		<Grid.Resources>
		    <ResourceDictionary>
                <Storyboard x:Key="FadeIn">
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="_ScreenRoot" Storyboard.TargetProperty="(UIElement.Visibility)">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="_ScreenRoot" Storyboard.TargetProperty="(UIElement.Opacity)">
                        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                        <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0.5"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="_ContentAreaTrans" Storyboard.TargetProperty="X">
                        <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-300"/>
                        <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>

                <Storyboard x:Key="FadeOut">
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="_ScreenRoot" Storyboard.TargetProperty="(UIElement.Visibility)">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="{x:Static Visibility.Visible}"/>
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.4" Value="{x:Static Visibility.Collapsed}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="_ContentAreaTrans" Storyboard.TargetProperty="X">
                        <EasingDoubleKeyFrame KeyTime="0:0:0.0" Value="0"/>
                        <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-300"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="_ScreenRoot" Storyboard.TargetProperty="(UIElement.Opacity)">
                        <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0.5"/>
                        <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </ResourceDictionary>
		</Grid.Resources>

        <StackPanel x:Name="_ContentArea" HorizontalAlignment="Left" Width="300" Margin="0,0,0,0" Background="#7F4B3737" RenderTransformOrigin="0.5,0.5">
            <StackPanel.RenderTransform>
                <TranslateTransform x:Name="_ContentAreaTrans" X="-300"/>
            </StackPanel.RenderTransform>
            <Button x:Name="_ResumeGame" Content="Resume Game" FontSize="18.667" Margin="20,80,20,10" />
            <Button x:Name="_SaveGame" Content="Save Game" FontSize="18.667" Margin="20,10" />
            <Button x:Name="_LoadGame" Content="Load Game" FontSize="18.667" Margin="20,10" />
            <Button x:Name="_Options" Content="Options" FontSize="18.667" Margin="20,40,20,10" />
            <Button x:Name="_LeaveGame" Content="Leave Game" FontSize="18.667" Margin="20,40,20,10" />
            <Button x:Name="_ExitGame" Content="Exit Game" FontSize="18.667" Margin="20,10" />
        </StackPanel>
	</Grid>
</UserControl>

Re: [Unity] FindTypeResource C++ Runtime Error

Posted: 29 Jul 2014, 03:34
by sfernandez
Hi,

Resources are normally stored in a ResourceDictionary using string keys, so you should be using the FindStringResource() function instead.

The resources that are stored in a ResourceDictionary using a type key are the Styles and the DataTemplates, when they do not provide an x:Key string, but only have set the TargetType property. You should use FindTypeResource() in these cases.

Re: [Unity] FindTypeResource C++ Runtime Error

Posted: 29 Jul 2014, 15:43
by sfernandez
Anyway, to avoid confusion and to match WPF API, we are going to remove these funtions and provide something like this in the FrameworkElement:
T FindResource<T>(object key);
That would be used like this:
// XAML
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" />

// C#
Style style = element.FindResource<Style>("MyButtonStyle");
// XAML
<Style TargetType="{x:Type Button}" /> or
<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}" />

// C#
Style style = element.FindResource<Style>(typeof(Button));

Re: [Unity] FindTypeResource C++ Runtime Error

Posted: 29 Jul 2014, 16:34
by rcalt2vt
Ok, I was wondering about the differences between those two functions. However, using FindStringResource also throws the same C++ Runtime Error.

Re: [Unity] FindTypeResource C++ Runtime Error

Posted: 29 Jul 2014, 17:50
by sfernandez
Looking at your code, you are performing the search in the UserControl object while the resources are stored in the child Grid. Resources are searched upwards in the tree, so you need to change your code to this:
public void ToggleEscapeScreen()
{
    if (_screenPnl.GetVisibility() == Visibility.Visible)
    {
        Storyboard board = _screenPnl.FindStringResource<Storyboard>("FadeOut");
        board.Begin(this);
    }
    else
    {
        Storyboard board = _screenPnl.FindStringResource<Storyboard>("FadeIn");
        board.Begin(this);
    }
}

Re: [Unity] FindTypeResource C++ Runtime Error

Posted: 29 Jul 2014, 22:35
by rcalt2vt
Oh, wow ya that makes a lot of sense. And that also does not throw an error either. Thanks again.