共用方式為


MediaElement.MediaFailed 事件

定義

發生于與媒體 來源相關聯的錯誤時。

public:
 virtual event ExceptionRoutedEventHandler ^ MediaFailed;
// Register
event_token MediaFailed(ExceptionRoutedEventHandler const& handler) const;

// Revoke with event_token
void MediaFailed(event_token const* cookie) const;

// Revoke with event_revoker
MediaElement::MediaFailed_revoker MediaFailed(auto_revoke_t, ExceptionRoutedEventHandler const& handler) const;
public event ExceptionRoutedEventHandler MediaFailed;
function onMediaFailed(eventArgs) { /* Your code */ }
mediaElement.addEventListener("mediafailed", onMediaFailed);
mediaElement.removeEventListener("mediafailed", onMediaFailed);
- or -
mediaElement.onmediafailed = onMediaFailed;
Public Custom Event MediaFailed As ExceptionRoutedEventHandler 
<MediaElement MediaFailed="eventhandler" .../>

事件類型

範例

下列程式碼會建立 MediaFailed 事件處理常式,以呼叫協助程式函式,以從事件引數擷取 HRESULT

private void videoMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
    // get HRESULT from event args 
    string hr = GetHresultFromErrorMessage(e);

    // Handle media failed event appropriately 
}

private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
{
    String hr = String.Empty;
    String token = "HRESULT - ";
    const int hrLength = 10;     // eg "0xFFFFFFFF"

    int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
    if (tokenPos != -1)
    {
        hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
    }

    return hr;
}

備註

最佳做法是一律處理 MediaFailed 事件並採取適當的動作。

適用於