2016-06-21 22 views
0

Ich möchte benutzerdefinierte animierte Fortschrittsbalken mit zehn Bildern machen. Jetzt benutze ich ein Timer`s Tick, die Ressource Bilder alle 5 Millisekunden ändert:XAML benutzerdefinierte Fortschrittsbalken mit Bildern

XAML:

<Grid> 
     <Image HorizontalAlignment="Stretch" 
       Margin="0" 
       VerticalAlignment="Top" 
       Stretch="Fill" 
      x:Name="imageProgressBar"/> 
    </Grid> 

C#

private void StartAnimation() 
{ 
     if (IsAnimating) return; 

     _timer = new DispatcherTimer(); 
     _timer.Interval = FrameDuration; 
     _timer.Tick += TimerTick; 
     _timer.Start(); 
    } 


    private void StopAnimation() 
    { 
     if (!IsAnimating) return; 

     _timer.Stop(); 
     _timer = null; 
    } 


    private void TimerTick(object sender, object e) 
    { 
     if (FramesCount == 0 || _frames == null) return; 

     _currentFrame++; 
     _currentFrame = _currentFrame % FramesCount; 

     if (_currentFrame < _frames.Count) 
      imageProgressBar.Source = _frames[_currentFrame]; 
    } 

Diese Variante funktioniert, aber es ist nicht gut genug, weil es benutzt den UI (wie ich denke) Thread für die Verarbeitung, und deshalb ist es manchmal langsam. Gibt es eine Möglichkeit, die Quelle des Bildes anders zu ändern?

+0

Es ist das sein kann Ändern und erneutes Rendern der Bilder, was die Dinge langsamer als die Quelle verlangsamt. Da Ihr Bedarf so besonders ist, lohnt es sich, einfach alle 10 Bilder sofort zu laden und dann etwas zu ändern, um die gewünschten Effekte zu erzielen. – Logan

+0

@ dmitriy überprüfen Sie dieses Gelenk von Jerrynixon für benutzerdefinierte Fortschrittsring [Link] (http://blog.jerrynixon.com/2015/06/lets-code-build-custom-progress-ring-in.html) hoffe, das hilft. .. – Krth

+0

Danke für die Antwort. @Logan Ich habe gerade darüber nachgedacht, aber da ich neu bei XAML bin, konnte ich mir nicht vorstellen, wie ich es implementieren könnte. – dmitriy

Antwort

0

Sogar ich erstellte ähnliche Animation mit Code hinter meinem Projekt. Das Problem liegt im ersten Zyklus, wenn das Bild zwischengespeichert und geladen werden muss. Dies erfordert Zeit und daher beginnt die Animation ab dem zweiten Zyklus korrekt zu arbeiten.
Die Abhilfe zu beheben, dass ich das Storyboard in XAML selbst wurde definiert, am Ende mit (wie es war einfacher) und das Laden von Bildern in XAML, während ihre Sichtbarkeit

XAML-Code Aktualisierung:

<Storyboard x:Name="Storyboard_Loading" RepeatBehavior="Forever"> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading1"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.1"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading2"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.2"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading3"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.3"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading4"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.4"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading5"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.5"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading6"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.6"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading7"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.7"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading8"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.8"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading9"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:0.9"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading10"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:1"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading11"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:1.1"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="pageLoading12"> 
       <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Collapsed</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
       <DiscreteObjectKeyFrame KeyTime="0:0:1.2"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
       </DiscreteObjectKeyFrame> 
      </ObjectAnimationUsingKeyFrames> 
     </Storyboard> 

    </UserControl.Resources> 
    <Grid Background="#3F000000"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="150"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="150"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Grid Grid.Row="1" Grid.Column="1" x:Name="GridLoading" > 
      <!--<ProgressRing IsActive="True" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" Foreground="#EF4D17"/>--> 
      <Image x:Name="pageLoading" Source="ms-appx:///Assets/ProgressBar/page_loading_1.png" /> 
      <Image x:Name="pageLoading1" Source="ms-appx:///Assets/ProgressBar/page_loading_1.png"/> 
      <Image x:Name="pageLoading2" Source="ms-appx:///Assets/ProgressBar/page_loading_2.png"/> 
      <Image x:Name="pageLoading3" Source="ms-appx:///Assets/ProgressBar/page_loading_3.png"/> 
      <Image x:Name="pageLoading4" Source="ms-appx:///Assets/ProgressBar/page_loading_4.png"/> 
      <Image x:Name="pageLoading5" Source="ms-appx:///Assets/ProgressBar/page_loading_5.png"/> 
      <Image x:Name="pageLoading6" Source="ms-appx:///Assets/ProgressBar/page_loading_6.png"/> 
      <Image x:Name="pageLoading7" Source="ms-appx:///Assets/ProgressBar/page_loading_7.png"/> 
      <Image x:Name="pageLoading8" Source="ms-appx:///Assets/ProgressBar/page_loading_8.png"/> 
      <Image x:Name="pageLoading9" Source="ms-appx:///Assets/ProgressBar/page_loading_9.png"/> 
      <Image x:Name="pageLoading10" Source="ms-appx:///Assets/ProgressBar/page_loading_10.png"/> 
      <Image x:Name="pageLoading11" Source="ms-appx:///Assets/ProgressBar/page_loading_11.png"/> 
      <Image x:Name="pageLoading12" Source="ms-appx:///Assets/ProgressBar/page_loading_12.png"/> 
     </Grid> 
     <!--<Grid Grid.Row="1" x:Name="GridLoading" Background="#3F000000"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Image Grid.Column="1" x:Name="Image_Progress_Bar" /> 
     </Grid>--> 
    </Grid> 
+0

Danke @Jerin das ist genau das, was ich machen wollte! – dmitriy

+0

Ich bin froh, Ihnen helfen zu können. – Jerin

Verwandte Themen