Click here to Skip to main content
15,886,362 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Silverlight to WPF Porting Pin
bdb3886528-Apr-09 6:42
bdb3886528-Apr-09 6:42 
GeneralRe: Silverlight to WPF Porting Pin
Mark Salsbery28-Apr-09 8:01
Mark Salsbery28-Apr-09 8:01 
GeneralRe: Silverlight to WPF Porting Pin
bdb3886528-Apr-09 11:10
bdb3886528-Apr-09 11:10 
GeneralRe: Silverlight to WPF Porting Pin
Mark Salsbery28-Apr-09 12:44
Mark Salsbery28-Apr-09 12:44 
GeneralRe: Silverlight to WPF Porting Pin
bdb3886529-Apr-09 6:52
bdb3886529-Apr-09 6:52 
QuestionRe: Silverlight to WPF Porting Pin
Mark Salsbery29-Apr-09 7:21
Mark Salsbery29-Apr-09 7:21 
AnswerRe: Silverlight to WPF Porting Pin
bdb3886529-Apr-09 7:44
bdb3886529-Apr-09 7:44 
GeneralRe: Silverlight to WPF Porting Pin
Mark Salsbery29-Apr-09 8:10
Mark Salsbery29-Apr-09 8:10 
The problem isn't in there.

The ImageSpace3D.addImages() method is where the random calculations are made.

The calculations are based on the Width and Height properties of the control, which
are always NaN unless set somewhere, which they are not.

ActualWidth and ActualHeight are the properties that should be used, and should have been
used in the Silverlight version as well.

ActualWidth and ActualHeight are not set until the control is loaded so you need to call the
initialization code from a "Loaded" event handler (or later) instead of from the constructor.

Here's what you should have to make it work...
<!-- ImageSpace3D.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"
	mc:Ignorable="d"
	x:Class="ImageSpace3Dwpf.ImageSpace3D"
	x:Name="UserControl"
	d:DesignWidth="640" d:DesignHeight="480" 
	<code>Loaded="UserControl_Loaded"</code> >

	<Grid x:Name="LayoutRoot"/>
</UserControl>

// ImageSpace3D.xaml.cs


        public ImageSpace3D()
        {
            InitializeComponent();

            <code>// note I removed all the initialization code from here
            //    and placed it in the UserControl_Loaded() method below</code>
        }

		
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // add the holder to the canvas
            _holder.SetValue(Canvas.LeftProperty, <code>ActualWidth</code> / 2);
            _holder.SetValue(Canvas.TopProperty, <code>ActualHeight</code> / 2);
            LayoutRoot.Children.Add(_holder);

            // add all the texts to the stage
            addImages();

            // not enough? create more
            addImages();

            // start the enter frame event
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / FPS);
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }

		
        <code>// Here note I changed Width and Height to ActualWidth and ActualHeight</code>
	private void addImages()
        {
            int seed = (int)DateTime.Now.Ticks;

            for (int i = 0; i < IMAGES.Length; i++)
            {
                // create a random object
                seed += (int)DateTime.Now.Ticks;
                Random r = new Random(seed);

                // load the image
                String url = IMAGE_PATH + IMAGES[i];
                Image image = new Image();
                image.Source = new BitmapImage(new Uri(url, UriKind.Relative));

                // randomly assign the position
                Point3D point3D = new Point3D();
                point3D.x = r.NextDouble() * <code>ActualWidth - ActualWidth</code> / 2;
                point3D.y = r.NextDouble() * <code>ActualHeight - ActualHeight</code> / 2;
                point3D.z = r.NextDouble() * SPACE_LENGTH * 2 - SPACE_LENGTH;


                // update the image property
                image.SetValue(Canvas.LeftProperty, point3D.x);
                image.SetValue(Canvas.TopProperty, point3D.y);
                image.MouseLeftButtonDown += new MouseButtonEventHandler(image_MouseLeftButtonDown);
                image.Cursor = Cursors.Hand;


                _imagePoint3Ds.Add(image, point3D);
                _holder.Children.Add(image);
                _images.Add(image);
            }
        }


Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: Silverlight to WPF Porting Pin
bdb3886529-Apr-09 9:10
bdb3886529-Apr-09 9:10 
GeneralRe: Silverlight to WPF Porting Pin
Mark Salsbery29-Apr-09 9:13
Mark Salsbery29-Apr-09 9:13 
GeneralRe: Silverlight to WPF Porting Pin
bdb3886529-Apr-09 9:46
bdb3886529-Apr-09 9:46 
GeneralRe: Silverlight to WPF Porting Pin
Mark Salsbery29-Apr-09 10:39
Mark Salsbery29-Apr-09 10:39 
QuestionAssign a Command to a Template Item Pin
#realJSOP27-Apr-09 9:38
mve#realJSOP27-Apr-09 9:38 
AnswerRe: Assign a Command to a Template Item Pin
Wes Aday27-Apr-09 10:47
professionalWes Aday27-Apr-09 10:47 
AnswerRe: Assign a Command to a Template Item Pin
Mark Salsbery27-Apr-09 11:08
Mark Salsbery27-Apr-09 11:08 
GeneralRe: Assign a Command to a Template Item Pin
#realJSOP29-Apr-09 23:58
mve#realJSOP29-Apr-09 23:58 
GeneralRe: Assign a Command to a Template Item Pin
Mark Salsbery30-Apr-09 7:07
Mark Salsbery30-Apr-09 7:07 
AnswerRe: Assign a Command to a Template Item Pin
Philipp Sumi29-Apr-09 22:20
Philipp Sumi29-Apr-09 22:20 
GeneralRe: Assign a Command to a Template Item Pin
User 2710091-May-09 5:13
User 2710091-May-09 5:13 
QuestionProblem with Silverlight-Tools installation Pin
Frank Köppel27-Apr-09 6:41
Frank Köppel27-Apr-09 6:41 
QuestionRe: Problem with Silverlight-Tools installation Pin
Mark Salsbery27-Apr-09 6:59
Mark Salsbery27-Apr-09 6:59 
AnswerRe: Problem with Silverlight-Tools installation Pin
Frank Köppel27-Apr-09 22:08
Frank Köppel27-Apr-09 22:08 
GeneralRe: Problem with Silverlight-Tools installation Pin
Mark Salsbery28-Apr-09 5:26
Mark Salsbery28-Apr-09 5:26 
GeneralRe: Problem with Silverlight-Tools installation Pin
Frank Köppel29-Apr-09 0:58
Frank Köppel29-Apr-09 0:58 
GeneralRe: Problem with Silverlight-Tools installation Pin
Abhinav S9-May-09 1:28
Abhinav S9-May-09 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.