【问题标题】:.net maui change graphics view width and height to size of window or screen.net maui 将图形视图的宽度和高度更改为窗口或屏幕的大小
【发布时间】:2022-11-19 21:55:52
【问题描述】:

我有简单的图形视图

<VerticalStackLayout>
    <GraphicsView x:Name="modelerArea"
        Drawable="{StaticResource drawable}"
                  HeightRequest="1000"
                  WidthRequest="1000" />

我想将宽度和高度设置为窗口或屏幕的大小。我怎样才能做到这一点?
在 xaml.cs 文件中,我尝试了类似的操作,但出现错误,因为字段是只读的

    double height = DeviceDisplay.MainDisplayInfo.Height;
    double width = DeviceDisplay.MainDisplayInfo.Width;
    modelerArea.Width = width;
    modelerArea.Height = height;

感谢您的任何提示。

【问题讨论】:

  • 您只能在 MAUI(和 Xamarin.Forms)中设置 HeightRequestWidthRequest 属性。其他属性是只读的,并提供实际计算的尺寸,而您只能请求特定的宽度或高度,但不能保证这些值。如果您想填充整个可用空间,您还可以在 XAML 中使用 HorizontalOptions = "Fill"VerticalOptions = "Fill",但是您不应该同时设置 HeightRequestWidthRequest,因为它们会覆盖伸展行为。
  • 感谢您提供宝贵的信息。现在可以使用了:) 如果可能的话,请重写您的评论作为答案,以便我可以接受。

标签: c# xamarin maui


【解决方案1】:

填充整个可用空间的一种方法是简单地使用 GraphicsView 作为唯一的内容项,并使用 HorizontalOptionsVerticalOptions 来拉伸它:

<ContentPage ...>
    <GraphicsView x:Name="modelerArea"
                  Drawable="{StaticResource drawable}"
                  HorizontalOptions="Fill"
                  VerticalOptions="Fill"/>
</ContentPage>

更多相关信息:https://learn.microsoft.com/en-us/dotnet/maui/user-interface/align-position?view=net-maui-7.0

或者,在您的代码隐藏中,您还可以使用 GraphicsViewWidthRequestHeightRequest 属性:

modelerArea.WidthRequest = width;
modelerArea.HeightRequest = height;

这是必要的,因为 WidthHeight 是只读属性,仅提供实际计算的屏幕尺寸。不保证WidthRequestHeightRequest 的值。

笔记:您不能同时使用这两种方法,因为 HorizontalOptions="Fill"VerticalOptions="Fill" 将覆盖请求的宽度和高度值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2011-09-09
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多