How to Access a Button present inside a Custom Control, from the implementing page?

前端 未结 4 2024
感情败类
感情败类 2021-01-23 00:53

I have my generic.xaml containing the following code:



   

          

        
4条回答
  •  孤城傲影
    2021-01-23 00:59

    You just need to add a couple of events to your Control.

     public event EventHandler MovedPrevious
     public event EventHandler MovedNext
    

    Now this are typically implemented like this:-

     protected virtual void OnMovedPrevious(EventArgs e)
     {
       var handler = MovedPrevious;
       if (handler != null)
         handler(this, e);   
     }
    
     protected virtual void OnMovedNext(EventArgs e)
     {
       var handler = MovedNext;
       if (handler != null)
         handler(this, e);   
     }
    

    Now in your existing click events:-

    nextBtn.Click += (obj, Args) =>
    {
      customMediaPlayer.Source=new Uri(CustomMediaSource.ToString(),UriKind.RelativeOrAbsolute);  //No idea what this doing
      OnMovedNext(EventArgs.Empty);
    };
    
    prevBtn.Click += (obj, Args) =>
    {
      customMediaPlayer.Source = new Uri(CustomMediaSource.ToString(), UriKind.RelativeOrAbsolute); //No idea what this is doing either
      OnMovedPrevious(EventArgs.Empty);
    };
    

    Now in your consuming code you can do this sort of thing:-

    CustomVideoControl myVControl=new CustomVideoControl();
    myVControl.MovedNext += (s, args) => { /* deal with next */ };
    myVControl.MovedPrevious += (s, args) => { /* deal with previous */ };
    

提交回复
热议问题