Xamarin Forms 图像的固定尺寸

3
我试图设置图片的高度和宽度并将其固定大小,但是如果我缩小容器(框架、stackpanel、grid等),则图片也会随之缩小。然而,我本以为设置高度和宽度(WidthRequest、HeightRequest、MinimumHeightRequest、MinimumWidthRequest)会强制图片保持设置的大小…但实际上并没有。
我该怎么办...

你有尝试使用Image的Aspect属性吗?它有三种不同的方面:AspectFill、AspectFit和Fill。详细信息请参考此链接:https://developer.xamarin.com/api/type/Xamarin.Forms.Aspect/。 - Mario Galván
1个回答

2

试试这个;它将使用平台特定的尺寸设置图像的宽度和高度,即对于Android使用dp,对于iOS使用points。

<Image IsVisible="true">
          <Image.HeightRequest>
            <OnIdiom x:TypeArguments="x:Double">
              <OnIdiom.Phone>
                <OnPlatform x:TypeArguments="x:Double" iOS="72" Android="100" WinPhone="10" />
              </OnIdiom.Phone>
              <OnIdiom.Tablet>
                <OnPlatform x:TypeArguments="x:Double" iOS="212" Android="140" WinPhone="10" />
              </OnIdiom.Tablet>
            </OnIdiom>
          </Image.HeightRequest>
          <Image.WidthRequest>
            <OnIdiom x:TypeArguments="x:Double">
              <OnIdiom.Phone>
                <OnPlatform x:TypeArguments="x:Double" iOS="125" Android="150" WinPhone="10" />
              </OnIdiom.Phone>
              <OnIdiom.Tablet>
                <OnPlatform x:TypeArguments="x:Double" iOS="265" Android="190" WinPhone="10" />
              </OnIdiom.Tablet>
            </OnIdiom>
          </Image.WidthRequest>
          <Image.Source>
            <OnPlatform x:TypeArguments="ImageSource">
              <OnPlatform.iOS>
                <FileImageSource File="logo.png" />
              </OnPlatform.iOS>
              <OnPlatform.Android>
                <FileImageSource File="logo.png" />
              </OnPlatform.Android>
              <OnPlatform.WinPhone>
                <FileImageSource File="logo.png" />
              </OnPlatform.WinPhone>
            </OnPlatform>
          </Image.Source>
        </Image>

不幸的是,当您调整页面大小(在UWP上进行测试最容易),图像仍会重新调整大小。 - user1122052
请查看此帖子 https://forums.xamarin.com/discussion/65902/how-do-i-fix-the-size-of-an-image - Akash Amin
如果您将图像放在网格内,这也无法正常工作。 - Emil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接