Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Getting Size of ContentView for Custom Renderer

$
0
0

Let's say I've got a simple XAML page like so, where CameraView is just a ContentView within a Frame:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:SelfieMirroring" x:Class="SelfieMirroring.MainPage">
    <!-- Place new controls here -->
    <Grid x:Name="mainGrid" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label x:Name="mainLabel" Grid.Row="0" Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        <Frame Grid.Row="1" x:Name="cameraFrame">
            <local:CameraView x:Name="mainCamera" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"></local:CameraView>
        </Frame>
    </Grid>
</ContentPage>

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SelfieMirroring.CameraView">
    <ContentView.Content>
    </ContentView.Content>
</ContentView>

Then I've got a Custom Renderer like so, except that Control is null, but e.NewElement isn't.

using System;
using Xamarin.Forms;
using SelfieMirroring;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using AVFoundation;

[assembly: ExportRenderer(typeof(CameraView), typeof(SelfieMirroring.iOS.CameraViewRenderer))]
namespace SelfieMirroring.iOS
{
    public class CameraViewRenderer : ViewRenderer<CameraView, UIView>
    {
        AVCaptureDevice device;
        AVCaptureDeviceInput input;
        AVCaptureSession session;
        AVCapturePhotoOutput photo;

        UIView layoutPanel;

        CameraView view;

        public CameraViewRenderer()
        {
        }

        public void SetupCamera()
        {
            // to do
        }

        protected override void OnElementChanged(ElementChangedEventArgs<CameraView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
            }

            if (e.OldElement != null)
            {
            }

            if (e.NewElement != null)
            {
            }
        }
    }
}

How can I get the ContentView's Width and Height within the Custom Renderer's allocated width and height for the Frame that Xamarin set, so that I can base a UIView off of it to display the preview layer for the camera?

It returns in e.NewElement as -1 and -1 for the width/height. I don't want to know or explicitly set its width/height at design time though because it may be different for different devices. Surely the actual on-screen rendered dimensions of the Xamarin Forms components that were rendered are kept somewhere that I can reuse.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>