Skip to main content
Home Forums Silverlight Programming Programming with .NET - General 100% width & 100% height, dont like re-load
21 replies. Latest Post by niklasnson on January 17, 2008.
(0)
niklasnson
Member
62 points
26 Posts
01-15-2008 1:48 PM |
Im using ..
InitializeComponent()
parentCanvas.Width = _width
parentCanvas.Height = _height
txtLoading.Text =
txtLoading.FontSize = 90
txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((_height - txtLoading.ActualHeight) / 2))
txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((_width - txtLoading.ActualWidth) / 2))
To center a text on screen, this works great. But if i hit F5, it freaks out. I guess the
dosent like refresh... any one got a tip for me ?
Bill Reiss
Contributor
4818 points
913 Posts
01-15-2008 2:01 PM |
Yes you will want to handle the BroswerHost.Resize event and put your positioning logic inside the event handler.
mario_mh
498 points
116 Posts
01-15-2008 2:06 PM |
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
Dim parentCanvas As Canvas = FindName("parentCanvas")
txtLoading.Text = "Meantime Media"
txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((Width - txtLoading.ActualHeight) / 2))
txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((Height - txtLoading.ActualWidth) / 2))
End Sub
01-15-2008 2:18 PM |
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...
01-15-2008 2:32 PM |
what code do you use?
mine works fine ...
01-15-2008 2:47 PM |
mario_mh: what code do you use? mine works fine ...
parentCanvas.Width = Width
parentCanvas.Height = Height
01-15-2008 2:53 PM |
Set the text in the BrowserHost_Resize as well, the BroswerHost_Resize hasn't executed by the time Page_Loaded runs.
01-15-2008 3:11 PM |
i get 480x640
01-15-2008 3:28 PM |
mario_mh: i get 480x640
Is that youre current resolution on screen ?
Btw, thanks for helping out!
01-15-2008 3:32 PM |
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...
01-15-2008 3:34 PM |
try setting breakpoints and check the value.
no, i don't use 640x480 ;). mine is 1440x1280
01-15-2008 3:37 PM |
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 ...
01-15-2008 5:47 PM |
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
01-15-2008 5:55 PM |
just don't set width/height and i guess this will solve the problem ;)
01-16-2008 1:30 AM |
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...
01-16-2008 1:31 AM |
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
Participant
902 points
205 Posts
01-16-2008 12:29 PM |
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)
txtLoading.SetValue(Canvas.TopProperty, Convert.ToDouble((Interop.BrowserHost.ActualHeight - txtLoading.ActualHeight) / 2))
txtLoading.SetValue(Canvas.LeftProperty, Convert.ToDouble((Interop.BrowserHost.ActualWidth - txtLoading.ActualWidth) / 2))
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.
01-17-2008 12:53 AM |
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.
Partial
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
parentCanvas.Width = Interop.BrowserHost.ActualWidth
parentCanvas.Height = Interop.BrowserHost.ActualHeight
End
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.
01-17-2008 3:20 AM |
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 L...
All-Star
25052 points
2,747 Posts
01-17-2008 5:47 AM |
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…
01-17-2008 6:31 AM |
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!
01-17-2008 2:30 PM |
Private Function BrowserHeight() As Integer