<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Xamarin on AYU MAX</title>
    <link>https://ayumax.net/tags/xamarin/</link>
    <description>Recent content in Xamarin on AYU MAX</description>
    <image>
      <title>AYU MAX</title>
      <url>https://ayumax.net/profile.jpg</url>
      <link>https://ayumax.net/profile.jpg</link>
    </image>
    <generator>Hugo -- 0.147.1</generator>
    <language>en</language>
    <lastBuildDate>Tue, 16 Oct 2018 22:12:37 +0000</lastBuildDate>
    <atom:link href="https://ayumax.net/tags/xamarin/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>View Scaling in Xamarin Forms</title>
      <link>https://ayumax.net/entry/2018/10/16/221237/</link>
      <pubDate>Tue, 16 Oct 2018 22:12:37 +0000</pubDate>
      <guid>https://ayumax.net/entry/2018/10/16/221237/</guid>
      <description>&lt;h1 id=&#34;view-scaling&#34;&gt;View Scaling&lt;/h1&gt;
&lt;p&gt;In the app I&amp;rsquo;m currently building, I wanted to create a virtual-sized canvas within the Window and change the overall scale according to the window size without breaking the layout inside. I explored various options.&lt;/p&gt;
&lt;p&gt;In WPF, it was convenient because you could adjust the scaling of the content display instantly by writing something like the below&amp;hellip; I wondered if Xamarin had something similar, but&amp;hellip; it doesn&amp;rsquo;t.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-xml&#34; data-lang=&#34;xml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;DockPanel&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;DockPanel.RenderTransform&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;ScaleTransform&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ScaleX=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{Binding Scale}&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ScaleY=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{Binding Scale}&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;/DockPanel.RenderTransform&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;/DockPanel&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id=&#34;about-visualelements-scale-property&#34;&gt;About VisualElement&amp;rsquo;s Scale Property&lt;/h1&gt;
&lt;p&gt;So, I wondered if controls have a property to set their own scale, and I found a property called &amp;ldquo;Scale&amp;rdquo;.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h1 id="view-scaling">View Scaling</h1>
<p>In the app I&rsquo;m currently building, I wanted to create a virtual-sized canvas within the Window and change the overall scale according to the window size without breaking the layout inside. I explored various options.</p>
<p>In WPF, it was convenient because you could adjust the scaling of the content display instantly by writing something like the below&hellip; I wondered if Xamarin had something similar, but&hellip; it doesn&rsquo;t.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;DockPanel&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;DockPanel.RenderTransform&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;ScaleTransform</span> <span style="color:#a6e22e">ScaleX=</span><span style="color:#e6db74">&#34;{Binding Scale}&#34;</span> <span style="color:#a6e22e">ScaleY=</span><span style="color:#e6db74">&#34;{Binding Scale}&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/DockPanel.RenderTransform&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/DockPanel&gt;</span>
</span></span></code></pre></div><h1 id="about-visualelements-scale-property">About VisualElement&rsquo;s Scale Property</h1>
<p>So, I wondered if controls have a property to set their own scale, and I found a property called &ldquo;Scale&rdquo;.</p>
<p>I tried setting the value to 0.5, expecting the size to become half.</p>
<p>But, the appearance didn&rsquo;t change.
It also feels strange that the Scale property isn&rsquo;t split into x and y. Is its purpose different?</p>
<p>I decided not to pursue this further and look for another approach.</p>
<h1 id="alternative-adjust-size-manually-the-gritty-way">Alternative: Adjust Size Manually (The Gritty Way)</h1>
<p>After considering various options, I decided to simply capture the ContentView&rsquo;s SizeChanged event, calculate the change rate from the original size (1x scale), and adjust accordingly.</p>
<p>I receive SizeChanged within a Behavior attached to the View and pass it to the ViewModel via a Command.</p>
<p>At that time, I tried connecting it to the ViewModel using Prism&rsquo;s EventToCommandBehavior, but it didn&rsquo;t work (EventArgs had no value, and I couldn&rsquo;t pass the sender to EventArgsConverter, so I gave up). I created a new Behavior specifically for SizeChanged to handle it.</p>
<p>So far, this method achieves the desired behavior.</p>
<h1 id="yet-another-method">Yet Another Method</h1>
<p>I haven&rsquo;t actually tried this, but instead of drawing directly on the screen as Xamarin Forms controls, I think scaling could be easily achieved by first drawing everything onto a Bitmap.</p>
<p>(I don&rsquo;t plan to try this for a while because the scope of change is large, and it would prevent UI creation using Xaml.)</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
