0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MAUI Tips - WinUIのImage付きボタンの挙動

Posted at

概要

これまでXamarin.Formsで作っていたアプリを、一念発起してMAUIで作り直す中で
調べたことの備忘録。

環境

Visual Studio 2022
MAUI
.NET 7

問題となった現象

Visual Studio for MACでiOS / Android向けに開発してきたスマホアプリを
Windows上でも動作させられないかと相談があり、
WindowsのVisual Studio 2022でコンパイルして実行してみたんですが、
ボタンに設定した画像の表示がおかしい・・・。

こちらがiPhone SE で表示したときの画面
image.png

こちらがWindows上で動作させて時の画面
image.png

解析結果

画像の表示がうまくいっているボタンもあり、
コード上の違いを追ってみたところ、
どうやらWindowsで動かした場合のButtonの画像は、
そのボタン内で表示できる最大サイズに拡大(または縮小)されている模様・・・。
※ここではPaddingが0に設定しているため、ボタンの枠ぎりぎりまで画像が表示されている

わざと横幅を広げてみるとこんな感じ。若干左寄り?
ContentLayoutを指定しないと中心にも表示されないですね。
image.png

⇒ContentLayoutでRight,0を指定すると、こうなる。
image.png

⇒ContentLayoutでTop,0を指定すると、変になる。
image.png

対処方法

・ContentLayoutでValueにRight,0を指定
・WinUIの時のみ、Paddingで画像サイズを調整

    <Style x:Key="IconButtonStyle" TargetType="Button">
        <Setter Property="WidthRequest" Value="60" />
        <Setter Property="HeightRequest" Value="60" />
        <Setter Property="CornerRadius" Value="10" />
        <Setter Property="Padding" Value="{OnPlatform 0, WinUI=15}" />
        <Setter Property="ContentLayout" Value="Right,0" />

        <Setter Property="BorderColor" Value="Transparent" />
        <Setter Property="BackgroundColor"
                Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
        <Setter Property="TextColor"
                Value="{AppThemeBinding Light={StaticResource LightOnPrimaryColor}, Dark={StaticResource DarkOnPrimaryColor}}" />
    </Style>

image.png

これでAndroidもiOSもWindowsも1コードでいける・・・はず・・・。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?