zoukankan      html  css  js  c++  java
  • wp7、8丿____IValueConverter_(含参,值转换器)

    示例:

     1 using System;
     2 using System.Globalization;
     3 using System.Windows.Data;
     4 using System.Windows.Media;
     5 
     6 namespace Sounds
     7 {
     8     /// <summary>
     9     /// 此转换器:实现MediaElementState类型的转换为Boolean类型
    10     /// </summary>
    11     public class AudioStateConverter : IValueConverter
    12     {
    13         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    14         {
    15             MediaElementState testState;
    16             if (parameter is MediaElementState)
    17             {
    18                 testState = (MediaElementState)parameter;
    19             }
    20             else
    21             {
    22                 testState = (MediaElementState)Enum.ToObject(typeof(MediaElementState), parameter);
    23             }
    24             var state = (MediaElementState)value;
    25             var matched = state == testState;
    26             return matched;
    27         }
    28 
    29         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    30         {
    31             throw new NotSupportedException();
    32         }
    33     }
    34 }
    <phone:PhoneApplicationPage
        .................
        xmlns:local="clr-namespace:Sounds">
    1     <phone:PhoneApplicationPage.Resources>
    2         <local:TimeSpanConverter x:Key="TimeSpanConverter"/>
    3         <MediaElementState x:Key="playing_state">Playing</MediaElementState>
    4         <MediaElementState x:Key="stopped_state">Stopped</MediaElementState>
    5         <local:AudioStateConverter x:Key="AudioStateConverter"/>
    6     </phone:PhoneApplicationPage.Resources>
     1 <MediaElement x:Name="VideoPlayer" AutoPlay="True" 
     2     Source="Wildlife.wmv" >
     3     <MediaElement.Clip>
     4         <RectangleGeometry RadiusY="0" RadiusX="0" Rect="30,30,100,85">
     5             <RectangleGeometry.Transform>
     6                 <CompositeTransform Rotation="-5" CenterX="130" CenterY="75" />
     7             </RectangleGeometry.Transform>
     8         </RectangleGeometry>
     9     </MediaElement.Clip>
    10 
    11 </MediaElement>
    12 <local:MediaElementBinder x:Name="MediaBinder" MediaElement="{Binding ElementName=VideoPlayer}"/>
    13 <Button x:Name="VideoPlayButton" Content="Play" Height="70" Margin="0,140,0,0" Width="168" VerticalAlignment="Top" HorizontalAlignment="Left" Click="VideoPlayButton_Click" IsEnabled="{Binding CurrentState, ConverterParameter={StaticResource stopped_state}, Converter={StaticResource AudioStateConverter}, ElementName=MediaBinder}" />
    14 <Button x:Name="VideoStopButton" Content="Stop" Height="70" Margin="0,212,0,0" Width="168" VerticalAlignment="Top" HorizontalAlignment="Left" Click="VideoStopButton_Click" IsEnabled="{Binding CurrentState, ConverterParameter={StaticResource playing_state}, Converter={StaticResource AudioStateConverter}, ElementName=MediaBinder}" />

     由于MediaElement很多属性直接绑定是不会变的,所以增加一些辅助类

      1 using System.Windows;
      2 using System.Windows.Controls;
      3 using System.Windows.Interactivity;
      4 using System.Windows.Media;
      5 using System.ComponentModel;
      6 using System.Windows.Threading;
      7 using System;
      8 
      9 namespace Sounds
     10 {
     11     public class MediaElementBinder : FrameworkElement
     12     {
     13         private DispatcherTimer timer = new DispatcherTimer();
     14 
     15         public MediaElementBinder()
     16         {
     17             // Timer interval 200 milliseconds
     18             timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
     19             timer.Tick += timer_Tick;
     20         }
     21 
     22         void timer_Tick(object sender, EventArgs e)
     23         {
     24             this.Position = this.MediaElement.Position;
     25         }
     26 
     27         public MediaElement MediaElement
     28         {
     29             get { return (MediaElement)GetValue(MediaElementProperty); }
     30             set { SetValue(MediaElementProperty, value); }
     31         }
     32         public static readonly DependencyProperty MediaElementProperty =
     33             DependencyProperty.Register(
     34                 "MediaElement",
     35                 typeof(MediaElement),
     36                 typeof(MediaElementBinder),
     37                 new PropertyMetadata(OnMediaElementChanged));
     38         private static void OnMediaElementChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
     39         {
     40             var mediator = (MediaElementBinder)o;
     41             var mediaElement = (MediaElement)(e.NewValue);
     42             if (null != mediaElement)
     43             {
     44                 mediaElement.CurrentStateChanged += mediator.MediaPlayer_CurrentStateChanged;
     45                 mediaElement.MediaOpened += mediator.MediaPlayer_MediaOpened;
     46             }
     47         }
     48 
     49         
     50        
     51         public MediaElementState CurrentState
     52         {
     53             get { return (MediaElementState)GetValue(CurrentStateProperty); }
     54             set { SetValue(CurrentStateProperty, value); }
     55         }
     56 
     57         public static readonly DependencyProperty CurrentStateProperty =
     58             DependencyProperty.Register("CurrentState", typeof(MediaElementState), typeof(MediaElementBinder), new PropertyMetadata(null));
     59 
     60 
     61 
     62 
     63         public TimeSpan Position
     64         {
     65             get { return (TimeSpan)GetValue(PositionProperty); }
     66             set { SetValue(PositionProperty, value); }
     67         }
     68 
     69         // Using a DependencyProperty as the backing store for Position.  This enables animation, styling, binding, etc...
     70         public static readonly DependencyProperty PositionProperty =
     71             DependencyProperty.Register("Position", typeof(TimeSpan), typeof(MediaElementBinder), new PropertyMetadata(null));
     72 
     73 
     74 
     75         public Duration Duration
     76         {
     77             get { return (Duration)GetValue(DurationProperty); }
     78             set { SetValue(DurationProperty, value); }
     79         }
     80 
     81         // Using a DependencyProperty as the backing store for Duration.  This enables animation, styling, binding, etc...
     82         public static readonly DependencyProperty DurationProperty =
     83             DependencyProperty.Register("Duration", typeof(Duration), typeof(MediaElementBinder), new PropertyMetadata(null));
     84 
     85 
     86         private void MediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
     87         {
     88             var element =sender as MediaElement;
     89             this.Duration = element.NaturalDuration;
     90         }        
     91 
     92 
     93         private void MediaPlayer_CurrentStateChanged(object sender, System.Windows.RoutedEventArgs e)
     94         {
     95             this.CurrentState = (sender as MediaElement).CurrentState;
     96 
     97             if (this.CurrentState == MediaElementState.Playing)
     98             {
     99                 timer.Start();
    100             }
    101             else
    102             {
    103                 timer.Stop();
    104             }
    105         }
    106         
    107     }
    108 }
  • 相关阅读:
    错误总结Mapper扫描不到
    Spring项目出现--Error:java: Compilation failed: internal java compiler error
    mybatis-plus自动填充时间
    自定义异常类
    Swagger配置类
    JwtUtils工具类
    MD5加密工具类
    SpringBoot通用返回JSON数据封装类
    使用Swagger测试使传入json数据时出现JSON parse error: Unexpected character (‘}‘ (code 125)): was expecting double
    org.springframework.context.ApplicationContextException: Unable to start web server; nested exceptio
  • 原文地址:https://www.cnblogs.com/qq278360339/p/2849500.html
Copyright © 2011-2022 走看看