Ask Your Question
0

How do I reference media element events located on a ContentView from a ContentPage XAML CS class?

asked 2021-08-18 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-06-21 20:00:00 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-18 11:00:00 +0000

Seen: 11 times

Last updated: Jun 21 '22