Skip to main content

Microsoft Silverlight

Answered Question 100% width & 100% height, dont like re-loadRSS Feed

(0)

niklasnson
niklasnson

Member

Member

62 points

26 Posts

100% width & 100% height, dont like re-load

Im using ..

Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)

' Required to initialize variables

InitializeComponent()

Dim _width As Double = Interop.BrowserHost.ActualWidth

Dim _height As Double = Interop.BrowserHost.ActualHeight

Dim parentCanvas As Canvas = FindName("parentCanvas")

parentCanvas.Width = _width

parentCanvas.Height = _height

txtLoading.Text = "Meantime Media"

txtLoading.FontSize = 90

txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((_height - txtLoading.ActualHeight) / 2))

txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((_width - txtLoading.ActualWidth) / 2))

End Sub

To center a text on screen, this works great. But if i hit F5, it freaks out. I guess the

Dim _width As Double = Interop.BrowserHost.ActualWidth

Dim _height As Double = Interop.BrowserHost.ActualHeight

dosent like refresh... any one got a tip for me ?

Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: 100% width & 100% height, dont like re-load

Yes you will want to handle the BroswerHost.Resize event and put your positioning logic inside the event handler.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: 100% width & 100% height, dont like re-load

is there a special reasion you use the "Interop.BrowserHost...."?

You can achieve the same with:

Width and Height, there it works perfectly fine:

Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)

' Required to initialize variables

InitializeComponent()

 

 

Dim parentCanvas As Canvas = FindName("parentCanvas")

 

txtLoading.Text = "Meantime Media"

txtLoading.FontSize = 90

txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((Width - txtLoading.ActualHeight) / 2))

txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((Height - txtLoading.ActualWidth) / 2))

End Sub

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: 100% width & 100% height, dont like re-load

mario_mh:
is there a special reasion you use the "Interop.BrowserHost...."?

 

When i try without it the text ends up in higher left corner of screen... when using my original code it works fine...

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: 100% width & 100% height, dont like re-load

what code do you use?

mine works fine ...

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: 100% width & 100% height, dont like re-load

mario_mh:

what code do you use?

mine works fine ...

 

Partial Public Class Page

Inherits Canvas

Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)

' Required to initialize variables

InitializeComponent()

AddHandler Interop.BrowserHost.Resize, AddressOf Me.BrowserHost_Resize

txtLoading.Text = "Height: " + Height.ToString + "Width: " + Width.ToString

txtLoading.FontSize = 90

'txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((Height - txtLoading.ActualHeight) / 2))

'txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((Width - txtLoading.ActualWidth) / 2))

'imgHolder.Source = New Uri("a.jpg", UriKind.Relative)

'imgHolder.Height = 400

'imgHolder.SetValue(Canvas.TopProperty, Convert.ToDouble((_height - imgHolder.Height) / 2))

'imgHolder.SetValue(Canvas.LeftProperty, Convert.ToDouble((_width - imgHolder.Width) / 2))

End Sub

Private Sub BrowserHost_Resize(ByVal sender As Object, ByVal e As System.EventArgs)Dim parentCanvas As Canvas = FindName("parentCanvas")

parentCanvas.Width = Width

parentCanvas.Height = Height

End Sub

From the tip's i got so far, the BrowserHost_Resize looks fine (the silverlight app looks like its 100% width & height), but when i do the code above, i get

txtLoading.Text = "Height: " + Height.ToString + "Width: " + Width.ToString saying Height: 0 Width: 0 ...

Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: 100% width & 100% height, dont like re-load

Set the text in the BrowserHost_Resize as well, the BroswerHost_Resize hasn't executed by the time Page_Loaded runs.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: 100% width & 100% height, dont like re-load

i get 480x640

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: 100% width & 100% height, dont like re-load

mario_mh:

i get 480x640

 Is that youre current resolution on screen ?

 Btw, thanks for helping out!

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: 100% width & 100% height, dont like re-load

Bill Reiss:

Set the text in the BrowserHost_Resize as well, the BroswerHost_Resize hasn't executed by the time Page_Loaded runs.

 Same thing happens, it still gets 0 value. But my application are 100% width & height. Hmmm... Dont know what to try out...
 Is it just me, or does anyone elese has this trubble. I mean BrowserHost_Resize is firering, coz in my window anywhere i right click i get a
silverlight popup. But the text wont get it...

Btw, thanks for helping out!

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: 100% width & 100% height, dont like re-load

try setting breakpoints and check the value.

no, i don't use 640x480 ;). mine is 1440x1280

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: 100% width & 100% height, dont like re-load

btw, do you overwrite your new and height-property?

prepend a "MyBase" in front of height/width ... maybe you do smth with height and width ...

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: 100% width & 100% height, dont like re-load

Yes that's what's happening, you're setting the width and height of the canvas, but then setting the text to the width and height of the page object, which haven't been set, so they will be 0


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: 100% width & 100% height, dont like re-load

just don't set width/height and i guess this will solve the problem ;)

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: 100% width & 100% height, dont like re-load

mario_mh:

just don't set width/height and i guess this will solve the problem ;)

 Yes im almost there, just need to pull some more hair from my head. The problem is that a relly want a full screen to work with. It would be so nice. But now i really dont know what to do anymore.... argh...

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: 100% width & 100% height, dont like re-load

Bill Reiss:

Yes that's what's happening, you're setting the width and height of the canvas, but then setting the text to the width and height of the page object, which haven't been set, so they will be 0

 Any tip on work around for this ?

Sopheap Ly
Sopheap Ly

Participant

Participant

902 points

205 Posts

Re: Re: 100% width & 100% height, dont like re-load

Like Bill Reise mentioned before, why don't you just replace with this:

 

 

    Private Sub BrowserHost_Resize(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim parentCanvas As Canvas = FindName("parentCanvas")

        txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((Interop.BrowserHost.ActualHeight - txtLoading.ActualHeight) / 2))

        txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((Interop.BrowserHost.ActualWidth - txtLoading.ActualWidth) / 2))

    End Sub

 

Don't use the _width all the time, because variable stores by value not reference. That means when Interop.BrowserHost.ActualWidth changes, the _width does not. So use Interop.BrowserHost.ActualWidth directly.

 

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: Re: 100% width & 100% height, dont like re-load

Sopheap Ly:

Like Bill Reise mentioned before, why don't you just replace with this:

Don't use the _width all the time, because variable stores by value not reference. That means when Interop.BrowserHost.ActualWidth changes, the _width does not. So use Interop.BrowserHost.ActualWidth directly.

Ok so i removed my _width and _height and so now my code looks like this:

Partial Public Class Page

Inherits Canvas

Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)

' Required to initialize variables

InitializeComponent()

' Add handlers

AddHandler Interop.BrowserHost.Resize, AddressOf Me.BrowserHost_Resize

txtLoading.Text = "Meantime Media"

txtLoading.SetValue(Canvas.TopProperty, (Convert.ToDouble(Interop.BrowserHost.ActualHeight - txtLoading.ActualHeight) / 2))

txtLoading.SetValue(Canvas.LeftProperty, (Convert.ToDouble(Interop.BrowserHost.ActualWidth - txtLoading.ActualWidth) / 2))

txtDebuging.FontSize = 9

txtDebuging.Text =
"Width: " + Interop.BrowserHost.ActualWidth.ToString + ", Height: " + Interop.BrowserHost.ActualHeight.ToString

 

End Sub

Private Sub BrowserHost_Resize(ByVal sender As Object, ByVal e As System.EventArgs)Dim parentCanvas As Canvas = FindName("parentCanvas")

parentCanvas.Width = Interop.BrowserHost.ActualWidth

parentCanvas.Height = Interop.BrowserHost.ActualHeight

End Sub

End Class

But the trubble with Refresh (F5) is still there. Most of the time when i hit Refresh, the value of Interop.BrowserHost.ActualWidth is 0 and the same for
Interop.BrowserHost.ActualHeight. I dont want to place all the code in BrowserHost_Resize, becourse i want to be abel to work with txtLoading etc from other parts in the code aswell.

Sopheap Ly
Sopheap Ly

Participant

Participant

902 points

205 Posts

Re: Re: Re: 100% width & 100% height, dont like re-load

Have you tried putting those code in the Resize() event yet? You need to do this because the host element width/height is fluid.

txtLoading is still accessible from everywhere. I don't know why you said if you call txtLoading in the Resize(), other parts would not be able to access it.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Re: 100% width & 100% height, dont like re-load

Hello, this is a known issue in the current alpha bits, and has been fixed in Silverlight 2.0 Beta (but may be will introduce some break changes). In the current bits, BrowserHost.ActualWidth/Height will return 0 in the Loaded event. If you want to work around this issue now, you can try this:

 

                   Dim screenWidth As Integer = (HtmlPage.Document.DocumentElement.GetProperty(Of Integer)("clientWidth"))

                   Dim screenHeight As Integer = (HtmlPage.Document.DocumentElement.GetProperty(Of Integer)("clientHeight"))

                   txtLoading.SetValue(Canvas.LeftProperty, ((screenWidth - test.ActualWidth) / 2))

                   txtLoading.SetValue(Canvas.TopProperty, ((screenHeight - test.ActualHeight) / 2))

 

You’ll probably want to wrap these code into a method, because you need to call it both in Loaded and Resize.

 

But I suggest you to wait for Silverlight 2.0 Beta, because there’s likely to be some break changes. Sorry I can’t provide you with more details at this time…

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: Re: Re: 100% width & 100% height, dont like re-load

Sopheap Ly:

Have you tried putting those code in the Resize() event yet? You need to do this because the host element width/height is fluid.

txtLoading is still accessible from everywhere. I don't know why you said if you call txtLoading in the Resize(), other parts would not be able to access it.

 

Hi Sopheap Ly,

Thanks for the reply. What i ment was not that i would not be abel to access txtLoading. Just that i dont want to put all of my code in the Resize() sub.
That i want to be be abel to add text and elements in other sub's / functions and still be abel to get them in correct placement of the screen. Are at work currently but will test some more when i get home. Thanks!

niklasnson
niklasnson

Member

Member

62 points

26 Posts

Re: Re: 100% width & 100% height, dont like re-load

Private Function BrowserHeight() As Integer

Return HtmlPage.Document.DocumentElement.GetProperty(Of Integer)("clientHeight")

End Function

Private Function BrowserWidth() As Integer

Return HtmlPage.Document.DocumentElement.GetProperty(Of Integer)("clientWidth")

End Function

 

And it works like a charm... Thank you all for helping me out!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities