Adding physicsobjetccontroller behaviour to usercontrol crashs app

Jan 21, 2010 at 1:55 PM
Edited Jan 21, 2010 at 1:56 PM

Adding physicsobjetccontroller behaviour  to usercontrol crashs app with the following error message:

 Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Windows.Media.DrawingVisual' to type 'System.Windows.UIElement'.

My layout is set to canvas (WPF)

Thanks

 

Coordinator
Jan 21, 2010 at 2:00 PM

It looks like you're using a WPF DrawingVisual object and probably applying physics behaviors to it inside your UserControl.

And when I originally created the Behaviors, it was for Silverlight which does not have DrawingVisual. Instead, I used UIElement as the base level class for applying Physics.

Do you know which element in your UserControl is a DrawingVisual? Can you replace it to see if you're OK after that?

Jan 21, 2010 at 3:28 PM

Thanks for the reply - i am relatively new to WPF and expression blend - do you know how to find out which element is a drawing visual?

 

Thanks

Coordinator
Jan 21, 2010 at 3:43 PM

If you want, and its not huge, you could post the XAML and code for your user control here on the forum and I could take a peek. DrawingVisual's are often created in code I think...

Jan 21, 2010 at 3:54 PM

 

<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"
	xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"
	xmlns:pb="clr-namespace:Spritehand.PhysicsBehaviors;assembly=Spritehand.PhysicsBehaviors"
	x:Class="UserControl2"
	x:Name="UserControl" MinHeight="51.417" MinWidth="145.667" Height="51.417">

	<Canvas x:Name="LayoutRoot" Width="166.667">
		<Canvas x:Name="canvascust1" Width="56.167" Height="40.834" d:LayoutOverrides="HorizontalAlignment">
		




			<Canvas MinWidth="145.667" Width="145.667" Height="51.51">
				<Rectangle x:Name="rectangle" Stroke="Black" RadiusX="2" RadiusY="2" StrokeThickness="0" MinWidth="120.417" MinHeight="39.917" Width="145.75" Height="51.51">
					<Rectangle.Fill>
						<LinearGradientBrush EndPoint="0.153,1.772" StartPoint="0.153,-2.961">
							<LinearGradientBrush.RelativeTransform>
								<TransformGroup>
									<ScaleTransform CenterX="0.136" CenterY="0.666" ScaleX="0.429" ScaleY="0.198"/>
									<SkewTransform AngleX="0" AngleY="0" CenterX="0.136" CenterY="0.666"/>
									<RotateTransform Angle="0" CenterX="0.136" CenterY="0.666"/>
									<TranslateTransform X="0.384" Y="0.106"/>
								</TransformGroup>
							</LinearGradientBrush.RelativeTransform>
							<GradientStop Color="#FF555555" Offset="0.448"/>
							<GradientStop Color="#E5000000"/>
						</LinearGradientBrush>
					</Rectangle.Fill>
				</Rectangle>
			
				<TextBox x:Name="textdoc1" TextWrapping="Wrap" Background="#7FFFFFFF" BorderBrush="#00000102" MinWidth="116" FontSize="12" Width="138.666" Height="23.527" Canvas.Left="2.834" Canvas.Top="24.233"/>
				<Label x:Name="labeldoc1" HorizontalContentAlignment="center" Foreground="White" Content="" MinWidth="114.75" Height="26.464" HorizontalAlignment="Center" FontSize="13.333" Canvas.Left="15.458" Canvas.Top="-2.331"/>
				<Rectangle Stroke="Black" StrokeThickness="0" RadiusX="2" RadiusY="2" Width="16.083" Height="51.51" Canvas.Left="149.75">
					<Rectangle.Fill>
						<LinearGradientBrush EndPoint="0.153,1.772" StartPoint="0.153,-2.961">
							<LinearGradientBrush.RelativeTransform>
								<TransformGroup>
									<ScaleTransform CenterX="0.136" CenterY="0.666" ScaleX="0.429" ScaleY="0.198"/>
									<SkewTransform AngleX="0" AngleY="0" CenterX="0.136" CenterY="0.666"/>
									<RotateTransform Angle="0" CenterX="0.136" CenterY="0.666"/>
									<TranslateTransform X="0.384" Y="0.106"/>
								</TransformGroup>
							</LinearGradientBrush.RelativeTransform>
							<GradientStop Color="#FF555555" Offset="0.448"/>
							<GradientStop Color="#E5000000"/>
						</LinearGradientBrush>
					</Rectangle.Fill>
				</Rectangle>
				<Canvas x:Name="maggrid" Width="9.994" Height="9.514" Canvas.Left="152.94" Canvas.Top="2.419">
					<Ellipse x:Name="ellipse" Stroke="White" Width="7.581" Height="7.581" Canvas.Left="2.152"/>
					<Path x:Name="path" Stretch="Fill" Stroke="White" Data="M150.75,17.75 L145.6113,22.888701" Height="3.572" Width="3.567" Canvas.Left="0.395" Canvas.Top="5.942" d:LayoutOverrides="HorizontalAlignment"/>
				</Canvas>
			</Canvas>
		




		</Canvas>
	</Canvas>
</UserControl>

 


Thanks

 

I add the physics to the usercontrol in my main project

 

Coordinator
Jan 21, 2010 at 4:14 PM

Ah - I guess I would need to see the whole project - I'm not seeing any Physics Behaviors in there and you mentioned you were adding them in your main project - does that mean you're adding them at runtime with code?

 

Jan 22, 2010 at 12:33 PM

No, i am not adding them at runtime.

My main app has a canvas and i add the usercontrol directly to the canvas and then drag the object behaviour onto the user control.

If i drag the object behaviour onto something else on my canvas like a button or a rectangle the project runs fine, but when i drag it onto the usercontrol the project loads and then crashes instantly....

The porect is pretty big so if i pasted it here it would take up a few pages.... I could always post snippets...

Thanks..