簡體   English   中英

嘗試實現 MAUI 滑動以更改圖像

[英]Trying to implement MAUI Swipe to change Image

我正在創建一個簡單的頁面來通過滑動來更改圖像。 向左滑動給出下一張圖片,向右滑動上一張。 不幸的是,滑動事件不會觸發。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Alm.MainPage">
    
    <SwipeView SwipeEnded="SwipeView_SwipeEnded" Threshold="200">
        <SwipeItemView>             
        <Image >
            <Image.Source>
                <UriImageSource x:Name="Sorsa" Uri="https://someurl.com/someimage.jpg"></UriImageSource>
            </Image.Source>
        </Image>
        </SwipeItemView>
    </SwipeView>
 
</ContentPage>

事件是:

private void SwipeView_SwipeChanging(object sender, SwipeChangingEventArgs e) {
    if (e.SwipeDirection == SwipeDirection.Left)
        Sorsa.Uri = HaeEdellinen();
    if (e.SwipeDirection == SwipeDirection.Right)
        Sorsa.Uri = HaeSeuraava();
}

但它永遠不會開火。

您可以直接將Swipe GestureRecognizer添加到圖像並檢測滑動方向: LeftRight以更改圖像的來源。 以下是供您參考的代碼片段:

Xaml:


<Image x:Name="img" HeightRequest="150" WidthRequest="150" Source="logo.png" > 
         <Image.GestureRecognizers>
                 <SwipeGestureRecognizer Direction="Left" 
                                         Swiped="SwipeGestureRecognizer_Left"/>


                 <SwipeGestureRecognizer Direction="Right" 
                                         Swiped="SwipeGestureRecognizer_Right"/>

         </Image.GestureRecognizers>
</Image>

代碼隱藏:

private void SwipeGestureRecognizer_Left(object sender, SwipedEventArgs e) 
{
    img.Source = "logo.png";
}

private void SwipeGestureRecognizer_Right(object sender, SwipedEventArgs e)
{
    img.Source = "uk.jpg";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM