BitmapImage.Stop 方法

定義

結束動畫影像的動畫。

public:
 virtual void Stop() = Stop;
void Stop();
public void Stop();
function stop()
Public Sub Stop ()

Windows 需求

裝置系列
Windows 10 Anniversary Edition (已於 10.0.14393.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v3.0 引進)

範例

以下是如何使用 ApiInformation.IsMethodPresent 檢查 Stop 方法是否存在,然後再呼叫它。

if (ApiInformation.IsMethodPresent("Windows.UI.Xaml.Media.Imaging.BitmapImage", "Stop"))
{
    imageSource.Stop();
}

此範例示範如何使用動畫 GIF。 按鈕可讓使用者啟動或停止動畫。 會檢查 IsPlaying 屬性,以判斷是否呼叫 Play 或 Stop 方法來切換播放。

此範例會使用版本調適型程式碼,以便在所有版本的 Windows 10 上執行。 在此情況下, IsPlaying 屬性的存在表示 Play 和 Stop 方法也存在,因此不需要額外的 API 檢查。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Image>
        <Image.Source>
            <BitmapImage x:Name="imageSource"
                         UriSource="Assets/example.gif"/>
        </Image.Source>
    </Image>

    <AppBarButton x:Name="playButton"
              Icon="Play"
              Click="playButton_Click"/>
</Grid>
// Play or stop the animated bitmap.
void playButton_Click(object sender, RoutedEventArgs e)
{
    if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Media.Imaging.BitmapImage", "IsPlaying") 
        && imageSource.IsPlaying == true)
    {
        playButton.Icon = new SymbolIcon(Symbol.Play);
        imageSource.Stop();
    }
    else
    {
        playButton.Icon = new SymbolIcon(Symbol.Stop);
        imageSource.Play();
    }   
}

備註

從 Windows 10 1607 版開始,XAML Image元素支援動畫 GIF 影像。 當您使用 BitmapImage 做為影像 來源時,您可以存取 BitmapImage API 來控制動畫 GIF 影像的播放。 如需詳細資訊,請參閱 BitmapImage 類別和 動畫 GIF 播放範例的一節。

使用 IsPlaying 屬性以及 Play 和 Stop 方法來控制動畫點陣圖的播放。

相容性注意事項

如果您的應用程式在 1607 版之前的 Windows 10版本上執行,您必須使用ApiInformation類別來檢查此方法是否存在,才能使用它。 如需詳細資訊,請參閱 版本調適型程式碼:使用新的 API,同時維持與舊版的相容性

適用於