0

I'm trying to make my interface fit all phone screen resolutions so I thought of using ViewBox since it supposed to scale the content nicely the problem is whenever I wrap the Canvas that has all my elements with the ViewBox like this:

       <Viewbox Stretch="Uniform" >
        <Canvas x:Name="canvas">
        <Canvas.Background>
            <ImageBrush Stretch="Uniform" ImageSource="Assets/start-01.jpg"/>
        </Canvas.Background>
                <Image x:Name="play" HorizontalAlignment="Left" Height="36" Margin="70,271,0,0" VerticalAlignment="Top" Width="194" Source="Assets/start bottuns-01.png" Stretch="Fill" Tapped="play_Tapped"/>
                <TextBlock x:Name="playTB" Height="36" Margin="117,265,221,0" TextWrapping="Wrap" Text="RACE" FontSize="30" VerticalAlignment="Top" CharacterSpacing="364" Tapped="play_Tapped" FontFamily="Lucida Sans Unicode"/>
                <Image x:Name="highscoresBT" HorizontalAlignment="Left" Height="36" Margin="70,337,0,0" VerticalAlignment="Top" Width="194" Source="Assets/start bottuns-01.png" Stretch="Fill" Tapped="highscoresBT_Tapped"/>
                <TextBlock x:Name="highscoreTB" Height="36" Margin="72,337,236,0" TextWrapping="Wrap" Text="HIGHSCORES" FontSize="24" VerticalAlignment="Top" CharacterSpacing="194" Tapped="highscoresBT_Tapped" FontFamily="Lucida Sans Unicode"/>
                <Image x:Name="storeBT" HorizontalAlignment="Left" Height="36" Margin="70,403,0,0" VerticalAlignment="Top" Width="194" Source="Assets/start bottuns-01.png" Stretch="Fill" Tapped="storeBT_Tapped"/>
                <TextBlock x:Name="storeTB" HorizontalAlignment="Left" Height="36" Margin="111,397,0,0" TextWrapping="Wrap" Text="STORE" Width="151" FontSize="30" VerticalAlignment="Top" CharacterSpacing="364" Tapped="storeBT_Tapped"/>
                <Image x:Name="helpBut" HorizontalAlignment="Left" Height="100" Margin="131,522,0,0" VerticalAlignment="Top" Width="72" Source="Assets/helpBut-01.png" Tapped="helpBut_Tapped"/>
                <Image x:Name="coinsBG" HorizontalAlignment="Left" Height="19" Margin="282,0,0,0" VerticalAlignment="Top" Width="56" Source="Assets/comp bar-01.png" Stretch="Fill"/>
                <Image x:Name="coinsIC" HorizontalAlignment="Left" Height="19" VerticalAlignment="Top" Width="14" Source="Assets/coins-01.png" Margin="316,1,0,0"/>
                <TextBlock x:Name="coinsOwnedTB" HorizontalAlignment="Left" Height="14" Margin="290,2,0,0" TextWrapping="Wrap" Text="120" VerticalAlignment="Top" Width="21" FontSize="10" FontFamily="Lucida Sans Unicode" Foreground="#FF535257"/>
                <Image x:Name="scoreBG" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="136" Source="Assets/comp bar-01.png" Stretch="Fill"/>
                <TextBlock x:Name="scoreNameTB" HorizontalAlignment="Left" Height="21" Margin="1,2,0,0" TextWrapping="Wrap" Text="Score: " VerticalAlignment="Top" Width="48" FontSize="14" FontFamily="Lucida Sans Unicode" Foreground="#FF535257"/>
                <TextBlock x:Name="scoreTB" HorizontalAlignment="Left" Height="21" Margin="49,2,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="87" FontSize="14" FontFamily="Lucida Sans Unicode" Foreground="#FF535257"/>
                <Image x:Name="feedbackBT" HorizontalAlignment="Left" Height="36" Margin="70,469,0,0" VerticalAlignment="Top" Width="194" Source="Assets/start bottuns-01.png" Stretch="Fill" Tapped="feedback_Tapped"/>
                <TextBlock x:Name="feedbackTB" Height="36" Margin="83,463,244,0" TextWrapping="Wrap" Text="SUPPORT" FontSize="30" VerticalAlignment="Top" Tapped="feedback_Tapped" FontFamily="Lucida Sans Unicode" TextAlignment="Center" CharacterSpacing="200"/>
    </Canvas>
    </Viewbox>

All the elements disappears and when running on Phone emulator the App crashes Anyone has any thoughts on this?

Note: Using a Grid instead of Canvas solves the problem but there Are some places in my app where canvas should be used and is causing the same problem with ViewBox that is why I posted this question as an example I know this page doesn't really need a Canvas. Thanks

Fadi Banna
  • 554
  • 1
  • 10
  • 26

1 Answers1

0

Well it turns out that the canvas should have height and width to view properly in the view box while Grid doesn't need that , it actually logical since Grid takes the size of the elements in it while the canvas doesn't.

what still a mystery is why would the App crash on phone emulators when adding the ViewBox without setting the height and width for the canvas, but works normally if it was set.

Fadi Banna
  • 554
  • 1
  • 10
  • 26