You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在.NET MAUI中自定义Entry控件时如何获取宽高?

问题:在MAUI Entry控件映射器中获取宽高失败

我在自定义Entry控件的映射器时,尝试获取控件的宽高,已经学习了官方关于使用处理程序自定义控件的内容。

尝试通过PlatformView访问原生视图属性,以及通过VirtualView获取跨平台控件属性,但结果不符合预期:

  • Android/iOS平台下,PlatformView返回的宽高均为0
  • VirtualView返回的宽高均为NaN

相关代码如下:

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyEntry", (handler, view) =>
{
#if __ANDROID__
    // 这些值为0
    var pv_width = handler.PlatformView.Width;
    var pv_height = handler.PlatformView.Height;

    // 这些值为NaN
    var vv_width = handler.VirtualView.Width;
    var vv_height = handler.VirtualView.Height;
#elif __IOS__
    // 这些值为0
    var pv_width = handler.PlatformView.Bounds.Width;
    var pv_height = handler.PlatformView.Bounds.Height;

    // 这些值为NaN
    var vv_width = handler.VirtualView.Width;
    var vv_height = handler.VirtualView.Height;
#endif
}

请问在自定义Entry控件的映射器时,有没有办法正确获取其宽高?

内容的提问来源于stack exchange,提问作者Tyloo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:35:03