Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To reference media element events located on a ContentView from a ContentPage XAML CS class, you can declare the media element as a public property in the ContentView. Then, you can access the media element from the ContentPage by getting a reference to the ContentView and accessing its public property.

For example, in the ContentView XAML:

<ContentView>
   <MediaElement x:Name="myMediaElement" />
</ContentView>

In the ContentView CS class:

public partial class MyContentView : ContentView
{
   public MediaElement MyMediaElement => myMediaElement;
}

In the ContentPage XAML:

<ContentPage>
   <local:MyContentView x:Name="myContentView" />
</ContentPage>

In the ContentPage CS class:

public partial class MyContentPage : ContentPage
{
   private MyContentView _myContentView;

   public MyContentPage()
   {
      InitializeComponent();
      _myContentView = myContentView;
      _myContentView.MyMediaElement.MediaEnded += OnMediaEnded;
   }

   private void OnMediaEnded(object sender, EventArgs e)
   {
      // Media element ended event handler
   }
}

This way, you can access the media element events located on a ContentView from a ContentPage XAML CS class.