2017-04-19 107 views
1

我正在創建自定義傳輸控件。其中,我添加了一個帶有MenuFlyout的新按鈕來改變視頻的質量。那麼如何創建一個事件,當一個特定的MenuFlyoutItem被點擊時發生。如何爲自定義傳輸控件創建事件

例如:
如果我在XAML中使用

<MediaPlayerElement Name="YoutubePlayer" MaxWidth="640" MaxHeight="360" AreTransportControlsEnabled="True"> 
    <MediaPlayerElement.TransportControls> 
     <QualityTransportControls x:Name="CustomMediaControl" QualityChanged="CustomMediaControl_QualityChanged"/> 
    </MediaPlayerElement.TransportControls> 
</MediaPlayerElement> 

,它應該叫CustomMediaControl_QualityChanged每當我改變質量。它可以通過

public event EventHandler QualityChanged 
{ 
    add{} 
    remove{} 
} 

做,但我不知道在「添加」和「刪除」

這裏是我的自定義按鈕的代碼添加什麼。

<AppBarButton x:Name='QualityButton' 
    Style='{StaticResource AppBarButtonStyle}' 
    MediaTransportControlsHelper.DropoutOrder='3'> 
    <AppBarButton.Flyout> 
     <MenuFlyout> 
      <MenuFlyoutItem x:Name="Quality144p" Text="144p"/> 
      <MenuFlyoutItem x:Name="Quality240p" Text="240p"/> 
      <MenuFlyoutItem x:Name="Quality360p" Text="360p"/> 
      <MenuFlyoutItem x:Name="Quality480p" Text="480p"/> 
      <MenuFlyoutItem x:Name="Quality720p" Text="720p"/> 
      <MenuFlyoutItem x:Name="Quality1080p" Text="1080p"/> 
     </MenuFlyout> 
    </AppBarButton.Flyout> 
    <AppBarButton.Icon> 
     <SymbolIcon x:Name='QualitySymbol' Symbol='Setting' /> 
    </AppBarButton.Icon> 
</AppBarButton> 

回答

2

該解決方案爲我工作:

在你ResourceDictionary中添加一個MediaTransportControls的默認樣式。
(這可以在這裏找到:C:\ Program Files文件(x86)的\的Windows套件\ 10 \設計時\ CommonConfiguration \中性\ UAP * your_version_Number * \通用,
不是簡單地複製和粘貼整個
<Style TargetType="MediaTransportControls"> ... </Style>標籤,該標籤可在約線16350處找到)

將第一行從<Style TargetType="MediaTransportControls">更改爲例如<Style TargetType="QualityTransportControls">以定位您的控件。

您需要將您的控件的xaml代碼添加到ResourceDictionary中,例如在AudioTracksSelectionButton之後(取決於您想要控件顯示的位置)。

<AppBarButton x:Name='AudioTracksSelectionButton' 
       Style='{StaticResource AppBarButtonStyle}' 
       MediaTransportControlsHelper.DropoutOrder='13' 
       Visibility='Collapsed'> 
    <AppBarButton.Icon> 
     <FontIcon Glyph="&#xED1F;" /> 
    </AppBarButton.Icon> 
</AppBarButton> 
<AppBarButton x:Name='QualityButton' 
       Style='{StaticResource AppBarButtonStyle}' 
       MediaTransportControlsHelper.DropoutOrder='3'> 
    <AppBarButton.Flyout> 
     <MenuFlyout> 
      <MenuFlyoutItem x:Name="Quality144p" 
          Text="144p" /> 
      <MenuFlyoutItem x:Name="Quality240p" 
          Text="240p" /> 
     </MenuFlyout> 
    </AppBarButton.Flyout> 
    <AppBarButton.Icon> 
     <SymbolIcon x:Name='QualitySymbol' 
        Symbol='Setting' /> 
    </AppBarButton.Icon> 
</AppBarButton> 

然後創建一個新的類從MediaTransportControls派生並重寫OnApplyTemplate(),讓您的控制,可檢索,您可以手動添加竊聽事件。

public sealed class QualityTransportControls: MediaTransportControls 
{ 
    //store the old quality for the custom event 
    private int oldQuality = 144; 
    private int quality = 144; 
    public int Quality 
    { 
     get { return quality; } 
     set { 
      //update oldQuality 
      oldQuality = quality; 
      quality = value; 
      //this method is responsible for raising the event 
      OnQualityChanged(); 
     } 
    } 

    public QualityTransportControls() 
    { 
     this.DefaultStyleKey = typeof(QualityTransportControls); 
    } 

    protected override void OnApplyTemplate() 
    { 
     MenuFlyoutItem flyoutItem = GetTemplateChild("Quality144p") as MenuFlyoutItem; 
     flyoutItem.Tapped += SetQuality144; 

     MenuFlyoutItem flyoutItem2 = GetTemplateChild("Quality240p") as MenuFlyoutItem; 
     flyoutItem.Tapped += SetQuality240; 

     base.OnApplyTemplate(); 
    } 

    private void SetQuality144(object sender, TappedRoutedEventArgs e) 
    { 
     Quality = 144; 
    } 

    private void SetQuality240(object sender, TappedRoutedEventArgs e) 
    { 
     Quality = 244; 
    } 

    private EventRegistrationTokenTable<EventHandler<QualityChangedEventArgs>> m_NumberChangedTokenTable = null; 

    //this is your custom event which you can use within xaml 
    public event EventHandler<QualityChangedEventArgs> QualityChanged 
    { 
     add 
     { 
      EventRegistrationTokenTable<EventHandler<QualityChangedEventArgs>> 
       .GetOrCreateEventRegistrationTokenTable(ref m_NumberChangedTokenTable) 
       .AddEventHandler(value); 
     } 
     remove 
     { 
      EventRegistrationTokenTable<EventHandler<QualityChangedEventArgs>> 
       .GetOrCreateEventRegistrationTokenTable(ref m_NumberChangedTokenTable) 
       .RemoveEventHandler(value); 
     } 
    } 


    internal void OnQualityChanged() 
    { 
     //here you raise the event for every subscriber 
     EventRegistrationTokenTable<EventHandler<QualityChangedEventArgs>> 
     .GetOrCreateEventRegistrationTokenTable(ref m_NumberChangedTokenTable) 
     .InvocationList?.Invoke(this, new QualityChangedEventArgs(oldQuality, Quality)); 
    } 
} 

然後你需要爲QualityChangedEventArgs一類,所以你可以到事件(例如,舊的和新的質量)添加信息。

public class QualityChangedEventArgs:EventArgs 
{ 
    public int OldQuality { get; set; } 
    public int NewQuality { get; set; } 

    public QualityChangedEventArgs(int oldValue, int newValue) 
    { 
     OldQuality = oldValue; 
     NewQuality = newValue; 
    } 
} 

現在(構建後),您可以使用QualityChanged事件就像像螺紋

<local:CustomMediaTransportControls 
    QualityChanged="CustomMediaTransportControls_QualityChanged"/> 

任何其它事件,然後你實現代碼隱藏的方法(例如,您MainePage)像這個:

private void CustomMediaTransportControls_QualityChanged(object sender, QualityChangedEventArgs e) 
{ 
    //here you can change the quality according to the new quality which is stored in e.NewQuality 
} 
+0

我知道這樣做,我已經做到了。我的問題是如何爲控件創建自定義事件。例如,如果我在XAML中使用'',那麼每當更改質量時應調用CustomMediaControl_QualityChanged。它可以使用'公共事件EventHandler QualityChanged {add;刪除;}'但我不知道要添加和刪除中添加什麼。我希望你現在能理解我的問題。 –

+0

我不能使用'e.NewQuality','e'沒有'NewQuality'屬性。 **編輯:**通過在QualityChangedEventArgs類中將NewQuality設置爲公共來修正。 –