xaml - 水平旋转设备时界面出现问题
问题描述
我的应用程序的界面分为标题(它是一个 webview)和绝对内容,其中包含第二个 webview。
在垂直方向上它可以工作并且看起来很完美。
但是当我水平转动设备时,割台停止测量 70 并测量到 30 左右.... 割台降低了高度,尽管它具有固定的高度。
为什么会这样?有什么解决办法吗?
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PruebaHeight"
x:Class="PruebaHeight.MainPage">
<ContentPage.Content>
<StackLayout Spacing="0">
<WebView x:Name="webview_header" HorizontalOptions="FillAndExpand" HeightRequest="70" BackgroundColor="Red"/>
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Black">
<!-- other things -->
<ScrollView AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Green">
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="0" BackgroundColor="Purple">
<WebView x:Name="webview" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Blue" />
</AbsoluteLayout>
</ScrollView>
</AbsoluteLayout>
</StackLayout>
</ContentPage.Content>
解决方案
You could try to custom a view like following code.
XamWebView.cs
public class XamWebView: WebView
{
}
Then use it in your code.
var webview= new XamWebView();
webview.HorizontalOptions = LayoutOptions.Fill;
webview.VerticalOptions = LayoutOptions.StartAndExpand;
webview.WidthRequest = 70;
webview.HeightRequest = 70;
webview.Source = "https://www.google.com/";
Creating the Custom Renderer on Android(you could get the value of heightrequest when you running)
using WebView = Android.Webkit.WebView;
[assembly: ExportRenderer (typeof(XamWebView), typeof(XamWebViewRenderer))]
namespace Core.Droid
{
public class XamWebViewRenderer : WebViewRenderer
{
static XamWebView _xwebView = null;
WebView _webView;
public XamWebViewRenderer(Context context) : base(context)
{
}
class XamWebViewClient : Android.Webkit.WebViewClient
{
public override async void OnPageFinished(WebView view, string url)
{
if (_xwebView != null)
{
int i = 10;
while (view.ContentHeight == 0 && i-- > 0)
await System.Threading.Tasks.Task.Delay(100);
_xwebView.HeightRequest = view.ContentHeight;
}
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
_xwebView = e.NewElement as XamWebView;
_webView = Control;
if (e.OldElement == null)
{
_webView.SetWebViewClient(new XamWebViewClient());
}
}
}
}
推荐阅读
- swift - 在 WatchOS 上的 SwiftUI 中使用多个主机控制器
- javascript - 解构对象数组(es6)
- sql-server - SQL Server如何确定row_number中order by子句中未使用的其他列的顺序
- r - 在第一个和最后一个观察值等于某个值时对某些值求和
- azure-dev-spaces - azds up 似乎需要永远。有什么办法调试吗?
- python - AttributeError:'list'对象没有属性'lower' - Python
- gradle - Gradle 4.x API 而不是实现 - 我如何使用 gradle API 功能来公开内部依赖项
- arrays - Bash 从变量中引用数组
- amazon-elastic-beanstalk - 添加自动缩放通知后,ElasticBeanstalk 运行状况显示为降级
- java - log4j2:模式参数的自定义格式