Programming with .NET - Generalhttp://forums.silverlight.net//17.aspx/1?Programming+with+NET+GeneralGeneral discussions around authoring Silverlight .NET applications.Mon, 01 Jan 0001 00:00:00 -05001723612http://forums.silverlight.net//p/7625/23612.aspx/1?How+to+specify+the+height+and+width+of+Canvas+by+percentage+How to specify the height and width of Canvas by percentage.? <p>The code below doesn't work.&nbsp; Can anyone tell me how to specify the height or width of Canvas by percentage? Thanks.<br> </p> <p>&nbsp;&lt;Canvas xmlns=&quot;http://schemas.microsoft.com/client/2007&quot; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Width=&quot;100%&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Height=&quot;280&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Background=&quot;Yellow&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;<br> <br> &lt;/Canvas&gt;</p> 2007-12-14T06:03:17-05:0023635http://forums.silverlight.net//p/7625/23635.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p>I guess the 100% should be related to some value.</p> <p>you can specify the percentage of silverlight host in de createsilverlight function:</p> <font color="#0000ff" size="2"> <p>function</font><font size="2"> createSilverlight(xaml){ <br> Silverlight.createObject(xaml, document.getElementById(</font><font color="#a31515" size="2">&quot;SilverlightControlHost&quot;</font><font size="2">),<br> </font><font color="#a31515" size="2">&quot;SilverlightControl&quot;</font><font size="2">,<br> {width:</font><font color="#a31515" size="2">'100%'</font><font size="2">, height:</font><font color="#a31515" size="2">'100%'</font><font size="2">, version:</font><font color="#a31515" size="2">'1.1'</font><font size="2">},<br> {onError:</font><font color="#0000ff" size="2">null</font><font size="2">, onLoad:</font><font color="#0000ff" size="2">null</font><font size="2">},</font><font color="#0000ff" size="2">null</font><font size="2">); }</font></p> <p><font size="2">Or you can calculate the size of the canvas in relation to the browser size or html element<br> (haven't tested this, but i think it should work):</font></p> <font size="2"><font color="#0000ff" size="2"> <p>public</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> Page_Loaded(</font><font color="#0000ff" size="2">object</font><font size="2"> sender, </font><font color="#2b91af" size="2">EventArgs</font><font size="2"> e){</font><font size="2"><br> <font size="2"><br> <font size="2">HtmlDocument </font>_document = HtmlPage.Document;</font></font></p> <font size="2"><font size="2"><font size="2"> <p>Canvas&nbsp;root = FindName(</font><font color="#a31515" size="2">&quot;root&quot;</font><font size="2">) </font><font color="#0000ff" size="2">as</font><font size="2"> Canvas;<br> <font size="2"><font size="2">ScaleTransform scale = </font><font color="#0000ff" size="2">new</font><font size="2"> ScaleTransform();<br> </font><font size="2">scale.ScaleX = _document.GetElementByID(<font color="#a31515" size="2">&quot;someElement&quot;</font><font size="2">).getAttribute('width');<br> </font>scale.ScaleY = _document.GetElementByID(<font color="#a31515" size="2">&quot;someElement&quot;</font><font size="2">).getAttribute('height');<br> </font>root.RenderTransform = scale;</font></font></font></p> <font size="2">}<font size="2"></font></font><font size="2"><font size="2"> <p>&nbsp;</p> </font></font></font></font><font size="2"></font></font> 2007-12-14T08:27:22-05:0023636http://forums.silverlight.net//p/7625/23636.aspx/1?Re+Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: Re: How to specify the height and width of Canvas by percentage.? <p>Thanks. I will try and let you know. &nbsp;</p> 2007-12-14T08:39:18-05:0023762http://forums.silverlight.net//p/7625/23762.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Aleho666</h4> Canvas&nbsp;root = FindName(<font color="#a31515" size="2">&quot;root&quot;</font><font size="2">) </font><font color="#0000ff" size="2">as</font><font size="2"> Canvas;<br> <font size="2"><font size="2">ScaleTransform scale = </font><font color="#0000ff" size="2">new</font><font size="2"> ScaleTransform();<br> </font><font size="2">scale.ScaleX = _document.GetElementByID(<font color="#a31515" size="2">&quot;someElement&quot;</font><font size="2">).getAttribute('width');<br> </font>scale.ScaleY = _document.GetElementByID(<font color="#a31515" size="2">&quot;someElement&quot;</font><font size="2">).getAttribute('height');<br> </font>root.RenderTransform = scale;</font></font></font></blockquote> <p></p> <p>ScaleTransform works the the percentage of the actual canvas scale (1 = 100%, 0.5 = 50%) and if you set the scale&nbsp; with getAttibute('Width') then it is going to multiply the canvas by that number (the actual canvas will be width times wider) I haven't tried it&nbsp;(I dont use JavaScript) but I think it works that way. </p> <p>I think, this should work</p> <p>Double width = _document.GetElementByID(<font color="#a31515" size="2">&quot;someElement&quot;</font><font size="2">).getAttribute('width');<br> </font>root.SetValue(WidthProperty, width); </p> <p>This will get better in Silverlight 2.0&nbsp;with &quot;{Binding }&quot; property. </p> <p>Correct me if I am wrong. </p> 2007-12-15T19:16:11-05:0023779http://forums.silverlight.net//p/7625/23779.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Cass</h4> ScaleTransform works the the percentage of the actual canvas scale (1 = 100%, 0.5 = 50%) </blockquote> <p></p> <p>Yes, you are correct Cass (seems I forgat that). I usually use&nbsp;ScaleTransform to create a relative size canvas to an other canvas (not to HTML elements).</p> 2007-12-16T00:02:55-05:0023792http://forums.silverlight.net//p/7625/23792.aspx/1?Re+Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: Re: How to specify the height and width of Canvas by percentage.? <p>Thanks a lot. man.. For the time being, I'm facing the VS 2008 installation problem on Windows Vista. so, I can't continue learning... I spent the whole day&nbsp; to figure out what the problem is ... </p> <p>Do you have any idea how to check whether my Vista is pre-released version or not? Actually, I got this OS (Vista Home Premium)&nbsp; when I bought a laptop (Sony VAIO). I tried to install the .NET 3.0 sp1 on that machine but it failed. I got the message that this product is not supported on Vista Operation System. <br> </p> 2007-12-16T03:24:27-05:0023794http://forums.silverlight.net//p/7625/23794.aspx/1?Re+Re+Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: Re: Re: How to specify the height and width of Canvas by percentage.? <p>I just come to know that .NET 3.0 sp1 doesn't support for Vista.. I just manged to install .NET 3.5 right now... (not sure why it works right now.) .. i hav to try to install VS 2008 again... <br> </p> 2007-12-16T03:36:18-05:0025649http://forums.silverlight.net//p/7625/25649.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p>I just tested and it doesn't work. </p> <p><b>1. TestPage.html</b></p> <p>.silverlightHost { width: 855px; height : 100%; }</p> <p><b>2. TestPage.html.js</b></p> <p>// JScript source code<br> <br> //contains calls to silverlight.js, example below loads Page.xaml<br> function createSilverlight()<br> {<br> &nbsp;&nbsp;&nbsp; Silverlight.createObjectEx({<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; source: &quot;Page.xaml&quot;,<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parentElement: document.getElementById(&quot;SilverlightControlHost&quot;),<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; id: &quot;SilverlightControl&quot;,<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; properties: {<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; width: &quot;100%&quot;,<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; height: &quot;100%&quot;,<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; version: &quot;1.1&quot;,<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; enableHtmlAccess: &quot;true&quot;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; },<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; events: {}<br> &nbsp;&nbsp;&nbsp; });<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; // Give the keyboard focus to the Silverlight control by default<br> &nbsp;&nbsp;&nbsp; document.body.onload = function() {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var silverlightControl = document.getElementById('SilverlightControl');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (silverlightControl)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; silverlightControl.focus();<br> &nbsp;&nbsp;&nbsp; }<br> <br> }<br> </p> <p><b>3. Page.xaml.cs</b></p> <p>&nbsp;HtmlDocument _document = HtmlPage.Document;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScaleTransform scale = new ScaleTransform();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scale.ScaleX = Convert.ToDouble (_document.GetElementByID (&quot;SilverlightControlHost&quot;).GetAttribute (&quot;width&quot;));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scale.ScaleY = Convert.ToDouble (_document.GetElementByID (&quot;SilverlightControlHost&quot;).GetAttribute (&quot;height&quot;));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.RenderTransform = scale; <br> </p> <p>&nbsp;</p> <p>It shows nothing on the browser. Did I miss something? Thanks in advance. <br> </p> 2008-01-08T02:26:50-05:0025650http://forums.silverlight.net//p/7625/25650.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p>Thanks. It works with Cass's code. <br> </p> <p>Double width = _document.GetElementByID(<font color="#a31515" size="2">&quot;someElement&quot;</font><font size="2">).getAttribute('width');<br> </font>root.SetValue(WidthProperty, width); <br> </p> <p><br> </p> <p>&nbsp;</p> 2008-01-08T02:32:11-05:0025722http://forums.silverlight.net//p/7625/25722.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p>When I try to do this I get an null string for the width. From what element did you manage to get the width? Did you modify the default generated test page to test this?<br> </p> <p>&nbsp;(From a default application auto-generated code)<br> </p> <p>public void Page_Loaded(object o, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InitializeComponent(); <br> </p> <p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HtmlDocument _document = HtmlPage.Document;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _document.GetElementByID(&quot;SilverlightControlHost&quot;).GetAttribute(&quot;id&quot;);&nbsp; --&gt; equal to SilverlightControlHost as expected</p> <p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _document.GetElementByID(&quot;SilverlightControlHost&quot;).GetAttribute(&quot;width&quot;); -&gt; Is null</p> <p>...&nbsp;</p> <p>Did a few more,&nbsp;</p> <p>.GetAttribute(&quot;style&quot;) is set to [object], </p> <p>_document.GetElementByID(&quot;SilverlightControlHost&quot;)..ccsClass is silverLightHost<br> </p> <p>But I didn't figure out how to extract the width from the style neither...<br> </p> <p>Any hints?</p> <p>What I want to do at the end, is to make the page full screen on the browser (using BrowserHost.IsFullScreen = true;) then adjust the size of all the various components according to the available space on the screen...<br> &nbsp;</p> 2008-01-08T18:04:25-05:0025769http://forums.silverlight.net//p/7625/25769.aspx/1?Re+How+to+specify+the+height+and+width+of+Canvas+by+percentage+Re: How to specify the height and width of Canvas by percentage.? <p>Try the following steps ~ </p> <p>1. Set [Scriptable] in Page.xaml</p> <p>2. Add the constructor in Page.xaml</p> <p>public Page()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebApplication.Current.RegisterScriptableObject (&quot;EntryPoint&quot;, this);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch ( Exception ex )<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>3. Update this function in TestPage.html.js. (Take a look at bold)<br> </p> <p>document.body.onload = function() {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var silverlightControl = document.getElementById('SilverlightControl');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (silverlightControl)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; silverlightControl.focus();<br> &nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>var control = document.getElementById(&quot;SilverlightControl&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; control.Content.EntryPoint.InitResolution(screen.width, screen.height);&nbsp;&nbsp;</b>&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; }&nbsp;</p> <p>&nbsp;4. Add this function in Page.xaml<br> </p> <p>double _width;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double _height;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Scriptable]<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void InitResolution(string width,string height)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _width =&nbsp; Convert.ToDouble(width);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _height =&nbsp; Convert.ToDouble(height);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.SetValue (WidthProperty, _width);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br> </p> <p>&nbsp;</p> <p>then, it will work. &nbsp;</p> 2008-01-09T02:38:37-05:00