I'm trying to add a Viewport2DVisual3D to a Viewport3D in code, but the visual isn't showing up. Any help understanding why not would be appreciated. The following is the code for the main window.
Is it sufficient to just add the Viewport2DVisual3D to the children of the Viewport3D in order for it to be rendered?
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(temp);
}
public void temp(object sender, RoutedEventArgs e)
{
Viewport2DVisual3D test = new Viewport2DVisual3D();
MeshGeometry3D testGeometry = new MeshGeometry3D();
Vector3D CameraLookDirection = Main_Target_CameraOR20.LookDirection;
// Calculate the Positions based on the Camera
Point3DCollection myPoint3DCollection = new Point3DCollection();
myPoint3DCollection.Add(new Point3D(-1, 1, 0));
myPoint3DCollection.Add(new Point3D(-1, -1, 0));
myPoint3DCollection.Add(new Point3D(1, -1, 0));
myPoint3DCollection.Add(new Point3D(1, 1, 0));
testGeometry.Positions = myPoint3DCollection;
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(new Point(0, 0));
myPointCollection.Add(new Point(0, 1));
myPointCollection.Add(new Point(1, 1));
myPointCollection.Add(new Point(1, 0));
testGeometry.TextureCoordinates = myPointCollection;
Int32Collection triangleIndicesCollection = new Int32Collection();
triangleIndicesCollection.Add(0);
triangleIndicesCollection.Add(1);
triangleIndicesCollection.Add(2);
triangleIndicesCollection.Add(2);
triangleIndicesCollection.Add(3);
triangleIndicesCollection.Add(0);
testGeometry.TriangleIndices = triangleIndicesCollection;
DiffuseMaterial myDiffuseMaterial = new DiffuseMaterial(Brushes.White);
Viewport2DVisual3D.SetIsVisualHostMaterial(myDiffuseMaterial, true);
Transform3DGroup myTransform3DGroup = new Transform3DGroup();
ScaleTransform3D myScaleTransform3D = new ScaleTransform3D();
myScaleTransform3D.ScaleX = 2;
myScaleTransform3D.ScaleY = 2;
myScaleTransform3D.ScaleZ = 2;
TranslateTransform3D myTranslateTransform3D = new TranslateTransform3D();
myTranslateTransform3D.OffsetX = -27;
myTranslateTransform3D.OffsetY = 13;
myTranslateTransform3D.OffsetZ = 6;
RotateTransform3D rotateTransform = new RotateTransform3D()
{
Rotation = new AxisAngleRotation3D
{
Angle = -50,
Axis = new Vector3D(0, 1, 0)
}
};
myTransform3DGroup.Children.Add(myTranslateTransform3D);
myTransform3DGroup.Children.Add(myScaleTransform3D);
myTransform3DGroup.Children.Add(rotateTransform);
test.Transform = myTransform3DGroup;
Button myButton = new Button();
myButton.Content = "Test Button";
test.Material = myDiffuseMaterial;
test.Geometry = testGeometry;
test.Visual = myButton;
ZAM3DViewport3D.Children.Add(test);
}
}
It turns out that the problem was the Offset value. So, it is sufficient to add the child to the Viewport3D to have it render. Cheers
Related
So I want to pass data when I clicked an Canvas. So I have this code;
Canvas event_canvas = new Canvas();
event_canvas.Background = new SolidColorBrush(Color.FromRgb(66, 70, 77));
event_canvas.Width = 250;
event_canvas.Height = 60;
event_canvas.Margin = new Thickness(40, 0, 0, 0);
event_canvas.HorizontalAlignment = HorizontalAlignment.Left;
event_canvas.VerticalAlignment = VerticalAlignment.Top;
event_canvas.Cursor = Cursors.Hand;
#endregion
#region Grid (event_grid)
Grid event_grid = new Grid();
event_grid.Width = 250;
event_grid.Height = 60;
#endregion
#region TextBlock (event_text)
TextBlock event_text = new TextBlock();
event_text.VerticalAlignment = VerticalAlignment.Center;
event_text.HorizontalAlignment = HorizontalAlignment.Center;
event_text.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
event_text.Text = e.name;
#endregion
event_grid.Children.Add(event_text); // Add the textblock to the grid
event_canvas.Children.Add(event_grid); // Add the grid to the canvas
grid_events.Children.Add(event_canvas); // Add the canvas to the main grid.
// Click event registration
event_canvas.MouseLeftButtonDown += Event_canvas_MouseLeftButtonDown;
And then in the trigger;
private void Event_canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Page pg = new EventDetailPage();
// Replaces all the content!!!!!
this.Content = pg;
//throw new NotImplementedException();
}
I tried to add this;
var param = ((TextBlock)sender).Text;
Page pg = new EventDetailPage(param);
But that code doesn't work, it throws an error that I can't get a value.
How can I fix this issue?
Cast the sender argument to Canvas and then access the Grid through the Canvas' Children collecton and the TextBlock through the Grid's Children collecton:
private void Event_canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas canvas = (Canvas)sender;
Grid event_grid = canvas.Children[0] as Grid;
TextBlock event_text = event_grid.Children[0] as TextBlock;
string text = event_text.Text;
...
}
I have the following code in my WPF application:
public MainWindow()
{
InitializeComponent();
p1 = new Point(50, 50);
p2 = new Point(355, 50);
p3 = new Point(50, 355);
p4 = new Point(355, 355);
Loaded += (x, y) => Draw();
//ContentRendered += (x, y) => Draw();
}
I create a Polygon shape and try to get its bounding rectangle several ways:
private void Draw()
{
Polygon polygon = new Polygon();
polygon.Stroke = System.Windows.Media.Brushes.White;
polygon.Points.Add(p1);
polygon.Points.Add(p2);
polygon.Points.Add(p3);
polygon.Points.Add(p4);
canvas.Children.Add(polygon);
boundingRect = polygon.TransformToVisual(canvas).TransformBounds(new Rect(polygon.RenderSize));
boundingRect = polygon.TransformToVisual(polygon).TransformBounds(System.Windows.Controls.Primitives.LayoutInformation.GetLayoutSlot(polygon));
boundingRect = polygon.RenderTransform.TransformBounds(new Rect(polygon.RenderSize));
boundingRect = GetRectOfObject(polygon);
boundingRect = polygon.RenderedGeometry.Bounds;
}
private Rect GetRectOfObject(FrameworkElement _element)
{
Rect rectangleBounds = new Rect();
rectangleBounds = _element.RenderTransform.TransformBounds(new Rect(0, 0, _element.ActualWidth, _element.ActualHeight));
return rectangleBounds;
}
However, I always get {0;0;0;0}.
On debug, I can see the points but its size is rendered 0:
How should I calculate the correct way?
According to the MSDN remarks of the Measure method, calling UpdateLayout will obtain the necessary data (though it should be treated carefully), so this way the measuring works:
private void Draw()
{
Polygon polygon = new Polygon();
polygon.Stroke = System.Windows.Media.Brushes.White;
polygon.Points.Add(p1);
polygon.Points.Add(p2);
polygon.Points.Add(p3);
polygon.Points.Add(p4);
canvas.Children.Add(polygon);
polygon.UpdateLayout();
boundingRect = polygon.TransformToVisual(canvas).TransformBounds(new Rect(polygon.RenderSize));
}
Thanks #ChrisF for the hints.
How can I make WPF's storyboard stop/pause at a pre-defined time after it start? For example 20 seconds after started?
If I'm correctly understood your question, from my project I change smoothly UI element from one to another element in defined seconds.
void ScreenSelector(FrameworkElement CurrentElement, FrameworkElement ToNextElement, bool Side)
{
if (_Storyboard != null) _Storyboard.Stop(this);
Thickness _TicknessLeft = new Thickness(Width, 0, -Width, 0);
Thickness _TicknessRight = new Thickness(-Width, 0, Width, 0);
Thickness _TicknessClient = new Thickness(0, 0, 0, 0);
TimeSpan _TimeSpanStarting = TimeSpan.FromSeconds(0);
TimeSpan _TimeSpanStopping = TimeSpan.FromSeconds(0.5);
KeyTime _KeyTimeStarting = KeyTime.FromTimeSpan(_TimeSpanStarting);
KeyTime _KeyTimeStopping = KeyTime.FromTimeSpan(_TimeSpanStopping);
ToNextElement.Margin = Side ? _TicknessRight : _TicknessLeft;
ToNextElement.Visibility = Visibility.Visible;
Storyboard _StoryboardTemp = new Storyboard();
ThicknessAnimationUsingKeyFrames _CurrentThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames();
_CurrentThicknessAnimationUsingKeyFrames.BeginTime = _TimeSpanStarting;
Storyboard.SetTargetName(_CurrentThicknessAnimationUsingKeyFrames, CurrentElement.Name);
Storyboard.SetTargetProperty(_CurrentThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
_CurrentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(_TicknessClient, _KeyTimeStarting));
_CurrentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(Side ? _TicknessLeft : _TicknessRight, _KeyTimeStopping));
_StoryboardTemp.Children.Add(_CurrentThicknessAnimationUsingKeyFrames);
ThicknessAnimationUsingKeyFrames _NextThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames();
_NextThicknessAnimationUsingKeyFrames.BeginTime = _TimeSpanStarting;
Storyboard.SetTargetName(_NextThicknessAnimationUsingKeyFrames, ToNextElement.Name);
Storyboard.SetTargetProperty(_NextThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
_NextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(Side ? _TicknessRight : _TicknessLeft, _KeyTimeStarting));
_NextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(_TicknessClient, _KeyTimeStopping));
_StoryboardTemp.Children.Add(_NextThicknessAnimationUsingKeyFrames);
_StoryboardTemp.Completed += (EventHandler)delegate(object sender, EventArgs e)
{
CurrentElement.Visibility = Visibility.Hidden;
_Storyboard = null;
};
_Storyboard = _StoryboardTemp;
BeginStoryboard(_StoryboardTemp);
}
Is it possible to animate the "old school" way, in codebehind, instead of xaml?
I just want an arrow that points to something with a 'bounce effect' which I could easily do in my own for loop. But I do not know how to refresh or do a timer delay, inside the loop. I already placed the image into position in codebehind. All I want to do is this simple animation...
public void validationArrow()
{
var validationArrow = new Image();
validationArrow.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(validationArrow);
validationArrow.Stretch = Stretch.None;
validationArrow.VerticalAlignment = System.Windows.VerticalAlignment.Top;
validationArrow.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
var arrowPosition = new TranslateTransform { X = 0, Y = 0 };
validationArrow.RenderTransform = arrowPosition;
validationArrow.Name = "validationArrow";
for (int i = 150; i >= 0; i--)
{
arrowPosition.X = i;
validationArrow.RenderTransform = arrowPosition;
// how can I refresh screen and do some timing here?
}
}
There's no school like the old school ;)
Here, this should help you on your way. You can play with the millisecond and Y translation values being passed to the BuildEasing method to change the 'bounce' effect's speed and distance.
private void RunStoryboard()
{
var arrowImage = new Image();
arrowImage.RenderTransform = new CompositeTransform();
arrowImage.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(arrowImage);
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(BuildKeyFrame(arrowImage));
storyboard.Begin();
}
private DoubleAnimationUsingKeyFrames BuildKeyFrame(Image target)
{
DoubleAnimationUsingKeyFrames kf = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(kf, target);
Storyboard.SetTargetProperty(kf, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));
kf.KeyFrames.Add(BuildEasing(100, 10));
kf.KeyFrames.Add(BuildEasing(200, 0));
kf.KeyFrames.Add(BuildEasing(300, 10));
kf.KeyFrames.Add(BuildEasing(400, 0));
kf.KeyFrames.Add(BuildEasing(500, 10));
kf.KeyFrames.Add(BuildEasing(600, 0));
return kf;
}
private EasingDoubleKeyFrame BuildEasing(int ms, int value)
{
return new EasingDoubleKeyFrame()
{
KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, ms)),
Value = value
};
}
I am getting the following exception for code that used to work in silverlight 3 but has stopped working since upgrading to silverlight 4:
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var OpenFileDialog = new OpenFileDialog();
OpenFileDialog.Filter = "*.jpg|*.jpg";
if (OpenFileDialog.ShowDialog() == true)
{
var file = OpenFileDialog.Files.ToArray()[0];
ScaleStreamAsBitmap(file.OpenRead(), 200);
}
}
public static WriteableBitmap ScaleStreamAsBitmap(Stream file, int maxEdgeLength)
{
file.Position = 0;
var src = new BitmapImage();
var uiElement = new System.Windows.Controls.Image();
WriteableBitmap b = null;
var t = new ScaleTransform();
src.SetSource(file);
uiElement.Source = src;
//force render
uiElement.Effect = new DropShadowEffect() { ShadowDepth = 0, BlurRadius = 0 }; ;
//calc scale
double scaleX = 1;
double scaleY = 1;
if (src.PixelWidth > maxEdgeLength)
scaleX = ((double)maxEdgeLength) / src.PixelWidth;
if (src.PixelHeight > maxEdgeLength)
scaleY = ((double)maxEdgeLength) / src.PixelHeight;
double scale = Math.Min(scaleX, scaleY);
t.ScaleX = scale;
t.ScaleY = scale;
b = new WriteableBitmap(uiElement, t);
return b;
}
}
}
Thanks
I had the same problem and I succeeded to resolve it!
b=new new WriteableBitmap(0, 0);
b.SetSource(file);
b.Render( new Image() { Source = src, Effect = new DropShadowEffect() { ShadowDepth = 0, BlurRadius = 0 } }, new ScaleTransform() { ScaleX = scaleX , ScaleY = scaleY });
And you can remove :uiElement and file.Position!