none
Get available window size and point the center

    Question

  • Hello World!

    I'm trying to point a center of my Silverlight app at the initial time.

    I have a problem with this, using native stuff from Silverlight runtime, it return me NaN ( not a number ), so I'm confused with pointing center.

    I had done this problem with JavaScript, but I don't want to use JavaScript interpreator for such aim, cause Silverlight stuff is binary ( ActiveX, NPAPI modules in Tridient, Presto, Gecko etc.. ) and not to use JavaScript, how could I do it? When I trying to point without JS I'm getting the hResult error.

    JS + Silverlight runtime ( which works, but I don't want to use ):

        public partial class MainPage : UserControl
        {
            int width;
            int height;
    
            public MainPage()
            {
                InitializeComponent();
                
                try
                {
                    width = Convert.ToInt32(HtmlPage.Window.Eval("document.body.offsetWidth"));
                    height = Convert.ToInt32(HtmlPage.Window.Eval("document.body.offsetHeight"));
    
                    this.Width = width;
                    this.Height = height;
    
                    SetIsometric();
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
    
            void SetIsometric()
            {
                Ellipse center = new Ellipse();
                center.Width = 1;
                center.Height = 1;
                center.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                center.StrokeThickness = 10;
                center.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                center.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                center.Dispatcher.BeginInvoke(() => { center.Margin = new Thickness(this.Width / 2, this.Height / 2, 0, 0); });
                this.LayoutRoot.Dispatcher.BeginInvoke(() => { this.LayoutRoot.Children.Add(center); });
            }
        }

    When , I'm trying not to use JS, you could comment lines it Main() method lines, where are setting values.

    And just run the code, you shall catch such exception:


    Monday, August 27, 2012 7:56 AM

Answers

  • Instead of using this:

    width = Convert.ToInt32(HtmlPage.Window.Eval("document.body.offsetWidth"));

    height = Convert.ToInt32(HtmlPage.Window.Eval("document.body.offsetHeight"));

    use this:

    width = App.Current.Host.Content.ActualWidth;

    height = App.Current.Host.Content.ActualHeight;

    But you probably want to wait until the loaded event has occured, good luck.

    Monday, August 27, 2012 4:26 PM

All replies

  • I didn´t understand the Problem.

    Is your goal to Center a Silverlight Control inside a Browser ?

    This function is build in.

     

    Monday, August 27, 2012 8:02 AM
  • The problem was described as clear as a crystal.
    I want to draw point ( ellipse object in SL ) at the center of my Silverlight application.

    For such aim I want to use pure Silverlight features from its Runtime, not using JS.

    When I try do it without using JS, I'm getting exceptions hResult ( in pervious message ) with center.Margin = new Thickness()...

    Monday, August 27, 2012 8:25 AM
  • Your trying top manipulate UI elemenets with the dispatcher before they have been rendered into the visual tree.  You will have better luck declaring your center ellipse in the xaml rather than the code behind.  Here is an example:

        <Grid x:Name="LayoutRoot" Background="White">
            <Ellipse x:Name="Center" Fill="Red" Width="1" Height="1" VerticalAlignment="Top" HorizontalAlignment="Left" />
        </Grid>
    
            public MainPage()
            {
                InitializeComponent();
                App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            }
    
            void Content_Resized(object sender, EventArgs e)
            {
                Thickness margin = new Thickness();
                margin.Left = App.Current.Host.Content.ActualWidth / 2;
                margin.Top = App.Current.Host.Content.ActualHeight / 2;
                Center.Margin = margin;
            }

    Monday, August 27, 2012 8:44 AM
  • This is the same in pure XAML.

     

    <UserControl
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="SilverlightApplication4.MainPage">
    
    	<Grid x:Name="LayoutRoot" Background="White">
    		<Ellipse Fill="#FF1919EF" Margin="0" Width="5" Height="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    	</Grid>
    </UserControl>

    Monday, August 27, 2012 10:48 AM
  • This is the same in pure XAML.

     

    <UserControl 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            x:Class="SilverlightApplication4.MainPage"> 
     
            <Grid x:Name="LayoutRoot" Background="White"> 
                    <Ellipse Fill="#FF1919EF" Margin="0" Width="5" Height="5" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
            </Grid> 
    </UserControl>

    XAML is a bad solution for dynamic additions of a lot of objects.
    Also dynamic addition let you know more perfectly structure of adding.

    XAML is stupid and for designers.

    Monday, August 27, 2012 11:02 AM
  • so do you job themself hero.

    Monday, August 27, 2012 11:06 AM
  • Your trying top manipulate UI elemenets with the dispatcher before they have been rendered into the visual tree.  You will have better luck declaring your center ellipse in the xaml rather than the code behind.  Here is an example:

        <Grid x:Name="LayoutRoot" Background="White"> 
            <Ellipse x:Name="Center" Fill="Red" Width="1" Height="1" VerticalAlignment="Top" HorizontalAlignment="Left" /> 
        </Grid> 
     
            public MainPage() 
            { 
                InitializeComponent(); 
                App.Current.Host.Content.Resized += new EventHandler(Content_Resized); 
            } 
     
            void Content_Resized(object sender, EventArgs e) 
            { 
                Thickness margin = new Thickness(); 
                margin.Left = App.Current.Host.Content.ActualWidth / 2; 
                margin.Top = App.Current.Host.Content.ActualHeight / 2; 
                Center.Margin = margin; 
            }

    No, I'm not, the main problem, that the Width && Height of this are NaN ( not a number ), they don't have the exact value.

    XAML - is only instruction for the C# compiler, later it will transalte XAML nodes into CLR-instructions for Silverlight Environment.

    Monday, August 27, 2012 11:06 AM
  • so do you job themself hero.

    Seems to be, it will be my path ) cause XAML not needed

    Monday, August 27, 2012 11:07 AM
  • Instead of using this:

    width = Convert.ToInt32(HtmlPage.Window.Eval("document.body.offsetWidth"));

    height = Convert.ToInt32(HtmlPage.Window.Eval("document.body.offsetHeight"));

    use this:

    width = App.Current.Host.Content.ActualWidth;

    height = App.Current.Host.Content.ActualHeight;

    But you probably want to wait until the loaded event has occured, good luck.

    Monday, August 27, 2012 4:26 PM