Howto get more information about break state

Jakob Aagesen 1 Reputation point
2021-01-28T19:02:09.81+00:00

hi
I am new to Xamarin programming.
I am trying to make an app that displays a SurfaceView for video capture.
I have referenced the nuget package Xam.WebRtc.Android that uses a renderer called SurfaceViewRenderer that displays the SurfaceView.
I would like to create a custom renderer that inherits from the SurfaceViewRenderer hoping to be able to use in from the shared xamarin project.
This is how i implement my renderer:

[assembly: ExportRenderer(typeof(CameraView), typeof(CameraPreviewRenderer))]
namespace mobile.friends.Droid.CameraRenderer
{
    public class CameraPreviewRenderer : SurfaceViewRenderer
    {
        public CameraPreviewRenderer(Context context) : base(context)
        {
            this.SetBackgroundColor(Android.Graphics.Color.Red);
        }
    }
}

and this is how i have implemented my shared view:

public class CameraView : View
    {
        public CameraView()
        {

        }
    }

i then add it in the xaml like this:

<?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:mobile.friends.Camera"
                 x:Class="mobile.friends.MainPage">


        <local:CameraView HeightRequest="100" WidthRequest="100"/>
    </ContentPage>

during debug i can see that my custom renderer is called in it's constructor.
my problem is that the app throws an exception and the only information i get about it is:

Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).
System.InvalidCastException: 'Specified cast is not valid.'

I have tried to enable all exceptions in Visual Studio Exception settings.

How do i get more information about what goes wrong?
is there a way to get more information

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-01-29T02:00:53.987+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You create a custom renderer for Xamarin.Forms.View, but the Xamarin.Forms.View's renderer base class is IViewRenderer, not the SurfaceViewRenderer, when you create custom renderer, so you will get the System.InvalidCastException: 'Specified cast is not valid.'

    To create a cameraView with custom renderer view, You can refer to following thread:

    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view

    Here is source code.

    https://github.com/xamarin/xamarin-forms-samples/blob/master/CustomRenderers/View/Droid/CameraPreviewRenderer.cs

    If you want to create a custom renderer for xamarin forms controls, you should refer this thread to get the renderer base class and Native Controls, then create a custom renderer for your control.

    :https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/renderers

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.