I have an Android app, created with XF, where I need to display a PDF on a second screen.
There is a default PDF forms that is used most of the times, however a user can supply a custom PDF or image that should then be displayed instead.
As the values are dynamic and change depending on the app's current dataset, I thought about using the empty PDF as a background image and settings Label
s with DataBinding above it instead of filling the form via code.
However, when I use an AbsoluteLayout
, it cannot get it to render properly. I can only see a white screen, probably because the renderer created by Platform.CreateRendererWithContext
is DefaultRenderer
for the AbsoluteLayout
.
Here is what my current code looks like:
XAML:
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Shared.Controls.SecondScreenSheet">
<AbsoluteLayout
BackgroundColor="White"
Padding="100, 64"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand">
<Image
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
Source="pdfbackground.png"
Aspect="AspectFit" />
<Label
Text="Test"
TextColor="Red" />
</AbsoluteLayout>
</ContentView>
Service to display UI on the second screen:
private static readonly DisplayManagerCompat _displayManager =
DisplayManagerCompat.GetInstance(Xamarin.Essentials.Platform.AppContext);
public void Show(
Xamarin.Forms.View view)
{
var displays = _displayManager?.GetDisplays();
// check if there is a second display
if (displays?.Skip(1)?.Any() != true)
{
return;
}
var renderer = Platform.CreateRendererWithContext(
view,
Xamarin.Essentials.Platform.AppContext);
renderer.SetElement(view);
var secondScreen = displays.Skip(1).First();
Device.BeginInvokeOnMainThread(() =>
{
var presentation = new Presentation(
Xamarin.Essentials.Platform.CurrentActivity.Window.Context,
secondScreen);
presentation.SetCancelable(false);
presentation.SetContentView(
renderer.View);
presentation.Show();
});
}
I pass the ContentView.Content
(which is the AbsoluteLayout
) to the service as view
parameter.
When I use something simple like a Label
, everything works fine as CreateRendererWithContext
is able to create a renderer for the XF View.
Is there anything I can do to get this to work with an AbsoluteLayout
?
Are there other options that I didn't see yet?
Thanks for your help!