我正在创建一个基于通用视图的控件,该视图与基于 iOS MPVolumeView 的自定义渲染器一起使用,这是一个简单的控件,允许您在应用程序中选择音频的备用输出路由(即蓝牙扬声器)。如果我包装在单个堆栈布局中,则代码工作得很好,但如果它在两个堆栈布局中则不行。我的 XAML 看起来像这样......非常基本:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DABApp.DabTestPage" xmlns:local="clr-namespace:DABApp;assembly=DABApp">
<ContentPage.Content>
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="Red">
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" VerticalOptions="FillAndExpand">
<local:AudioOutputView />
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
这是我的自定义渲染器的胆量:
[assembly: ExportRenderer(typeof(AudioOutputView), typeof(iosAudioOutputViewRenderer))]
namespace DABApp.iOS
{
public class iosAudioOutputViewRenderer: ViewRenderer<AudioOutputView, UIView>
{
MPVolumeView view;
protected override void OnElementChanged(ElementChangedEventArgs<AudioOutputView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
view = new MPVolumeView()
{
ShowsRouteButton = true,
ShowsVolumeSlider = false,
};
SetNativeControl(view);
}
}
}
}
使用此 XAML 和代码,当我将页面异步推送到导航堆栈时,页面甚至不会显示。如果我删除其中一个 StackLayouts,它可以正常工作。
我将 CustomRenderer 中的 IOS 控件更改为一个简单的 UILabel 并且效果很好......所以看起来它与将 MPVolumeView 放在 2 StackLayouts 中有关。由于我的应用程序的布局要求,我需要能够做到这一点,而且为什么 1 StackLayout 很好,但 2 不是,而且仅适用于这个本机控件,这没有任何意义。
有任何想法吗?