none
metro的MediaElement怎么绑定MediaOpened RRS feed

  • 问题

  • Silverlight 是 <MediaElement x:Name="mediaElement" Margin="73,70,95,90" Source="{Binding SelectedVideoProperty}" AutoPlay="False" RenderTransformOrigin="0.498,0.492" UseLayoutRounding="False" d:LayoutRounding="Auto">
       <MediaElement.Projection>
        <PlaneProjection RotationX="-30" RotationY="10"/>
       </MediaElement.Projection>
       <i:Interaction.Triggers>
        <i:EventTrigger EventName="MediaOpened">
         <i:InvokeCommandAction Command="{Binding SetSeekControlCommand}"
                                               CommandParameter="{Binding ElementName=progressBar}"/>
        </i:EventTrigger>
        <i:EventTrigger SourceObject="{Binding ElementName=mediaElement}" EventName="MediaOpened">
         <i:InvokeCommandAction Command="{Binding MediaOpenedCommand}"
          CommandParameter="{Binding ElementName=mediaElement, Mode=OneWay}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseLeftButtonDown">
         <i:InvokeCommandAction Command="{Binding SetFullScreenCommand}" CommandParameter="{Binding ElementName=FullScreenGrid, Mode=OneWay}"/>
        </i:EventTrigger>
       </i:Interaction.Triggers>
      </MediaElement>

    metro应该是怎么样的 

    2012年8月30日 9:11

答案

  • 没有,简单的写法,还有只用就是用Attached Command 我也曾经实现过,可以参考我的一篇文章:http://www.codeproject.com/Articles/319855/Attached-Command-for-Windows-8-Metro-Style-in-C For RTM的话,要做一些修改, 修改后的例子可以看这里:

        public class AttachedCommand
        {
            public static string GetRoutedEvent(DependencyObject obj)
            {
                return (string)obj.GetValue(RoutedEventProperty);
            }
            public static void SetRoutedEvent(DependencyObject obj, string value)
            {
                obj.SetValue(RoutedEventProperty, value);
            }
            public static readonly DependencyProperty RoutedEventProperty =
                DependencyProperty.RegisterAttached("RoutedEvent", typeof(string), typeof(AttachedCommand), new PropertyMetadata(string.Empty, OnRoutedEventChanged));
    
    
            public static ICommand GetCommand(DependencyObject obj)
            {
                return (ICommand)obj.GetValue(CommandProperty);
            }
            public static void SetCommand(DependencyObject obj, ICommand value)
            {
                obj.SetValue(CommandProperty, value);
            }
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(AttachedCommand), new PropertyMetadata(null));
    
    
            public static object GetCommandParameter(DependencyObject obj)
            {
                return (object)obj.GetValue(CommandParameterProperty);
            }
            public static void SetCommandParameter(DependencyObject obj, object value)
            {
                obj.SetValue(CommandParameterProperty, value);
            }
            public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(AttachedCommand), new PropertyMetadata(null));
    
    
            private static void OnRoutedEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                String routedevent = (String)e.NewValue;
    
                if (!String.IsNullOrEmpty(routedevent))
                {
                    EventHooker eventHooker = new EventHooker();
                    eventHooker.AttachedCommandObject = d;
                    EventInfo eventInfo = GetEventrInfo(d.GetType().GetTypeInfo(), routedevent);
    
                    if (eventInfo != null)
                    {
                        WindowsRuntimeMarshal.RemoveAllEventHandlers(
                            a => eventInfo.RemoveMethod.Invoke(d, new object[] { a }));
                        WindowsRuntimeMarshal.AddEventHandler(
                            f => (EventRegistrationToken)eventInfo.AddMethod.Invoke(d, new object[] { f }),
                            a => eventInfo.RemoveMethod.Invoke(d, new object[] { a }),
                            eventHooker.GetEventHandler(eventInfo));
                    }
                }
            }
    
           private static EventInfo GetEventrInfo(TypeInfo typeInfo, string name)
            {
                EventInfo eventInfo = null;
                eventInfo = typeInfo.GetDeclaredEvent(name);
                if (eventInfo == null)
                {
                    Type baseType = typeInfo.BaseType;
                    if (baseType != null)
                        return GetEventrInfo(baseType.GetTypeInfo(),name);
                    else
                        return eventInfo;
                }
                return eventInfo;
            }
    
            internal sealed class EventHooker
            {
                public DependencyObject AttachedCommandObject { get; set; }
    
                public Delegate GetEventHandler(EventInfo eventInfo)
                {
                    Delegate del = null;
                    if (eventInfo == null)
                        throw new ArgumentNullException("eventInfo");
    
                    if (eventInfo.EventHandlerType == null)
                        throw new ArgumentNullException("eventInfo.EventHandlerType");
    
                    if (del == null)
                        del = this.GetType().GetTypeInfo().GetDeclaredMethod("OnEventRaised").CreateDelegate(eventInfo.EventHandlerType, this);
    
                    return del;
                }
    
                private void OnEventRaised(object sender, object e)  // the second parameter in Windows.UI.Xaml.EventHandler is Object
                {
                    ICommand command = (ICommand)(sender as DependencyObject).GetValue(AttachedCommand.CommandProperty);
                    object commandParameter = (sender as DependencyObject).GetValue(AttachedCommand.CommandParameterProperty);
                    if (commandParameter == null) commandParameter = new object[] { sender, e };
    
                    if (command != null && command.CanExecute(commandParameter))
                    {
                        command.Execute(commandParameter);
                    }
                }
            }
        }

    使用是一样的:

    <MediaElement local:AttachedCommand.RoutedEvent="MediaOpened" local:AttachedCommand.Command="{Binding MediaOpenedCommand}"/>


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 董圣博 2012年8月31日 8:33
    2012年8月31日 8:21
    版主

全部回复

  • Metro下没有Interaction 和 EventTrigger  也没有WPF的触发器。

    不过我自己实现过一个类似的,你参考着看看:https://skydrive.live.com/#cid=51B2FDD068799D15&id=51B2FDD068799D15%211035


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年8月31日 6:17
    版主
  • 那请问,现在如果我们要实现类似的绑定暂时是没有很直接的办法是吗?
    2012年8月31日 7:50
  • 没有,简单的写法,还有只用就是用Attached Command 我也曾经实现过,可以参考我的一篇文章:http://www.codeproject.com/Articles/319855/Attached-Command-for-Windows-8-Metro-Style-in-C For RTM的话,要做一些修改, 修改后的例子可以看这里:

        public class AttachedCommand
        {
            public static string GetRoutedEvent(DependencyObject obj)
            {
                return (string)obj.GetValue(RoutedEventProperty);
            }
            public static void SetRoutedEvent(DependencyObject obj, string value)
            {
                obj.SetValue(RoutedEventProperty, value);
            }
            public static readonly DependencyProperty RoutedEventProperty =
                DependencyProperty.RegisterAttached("RoutedEvent", typeof(string), typeof(AttachedCommand), new PropertyMetadata(string.Empty, OnRoutedEventChanged));
    
    
            public static ICommand GetCommand(DependencyObject obj)
            {
                return (ICommand)obj.GetValue(CommandProperty);
            }
            public static void SetCommand(DependencyObject obj, ICommand value)
            {
                obj.SetValue(CommandProperty, value);
            }
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(AttachedCommand), new PropertyMetadata(null));
    
    
            public static object GetCommandParameter(DependencyObject obj)
            {
                return (object)obj.GetValue(CommandParameterProperty);
            }
            public static void SetCommandParameter(DependencyObject obj, object value)
            {
                obj.SetValue(CommandParameterProperty, value);
            }
            public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(AttachedCommand), new PropertyMetadata(null));
    
    
            private static void OnRoutedEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                String routedevent = (String)e.NewValue;
    
                if (!String.IsNullOrEmpty(routedevent))
                {
                    EventHooker eventHooker = new EventHooker();
                    eventHooker.AttachedCommandObject = d;
                    EventInfo eventInfo = GetEventrInfo(d.GetType().GetTypeInfo(), routedevent);
    
                    if (eventInfo != null)
                    {
                        WindowsRuntimeMarshal.RemoveAllEventHandlers(
                            a => eventInfo.RemoveMethod.Invoke(d, new object[] { a }));
                        WindowsRuntimeMarshal.AddEventHandler(
                            f => (EventRegistrationToken)eventInfo.AddMethod.Invoke(d, new object[] { f }),
                            a => eventInfo.RemoveMethod.Invoke(d, new object[] { a }),
                            eventHooker.GetEventHandler(eventInfo));
                    }
                }
            }
    
           private static EventInfo GetEventrInfo(TypeInfo typeInfo, string name)
            {
                EventInfo eventInfo = null;
                eventInfo = typeInfo.GetDeclaredEvent(name);
                if (eventInfo == null)
                {
                    Type baseType = typeInfo.BaseType;
                    if (baseType != null)
                        return GetEventrInfo(baseType.GetTypeInfo(),name);
                    else
                        return eventInfo;
                }
                return eventInfo;
            }
    
            internal sealed class EventHooker
            {
                public DependencyObject AttachedCommandObject { get; set; }
    
                public Delegate GetEventHandler(EventInfo eventInfo)
                {
                    Delegate del = null;
                    if (eventInfo == null)
                        throw new ArgumentNullException("eventInfo");
    
                    if (eventInfo.EventHandlerType == null)
                        throw new ArgumentNullException("eventInfo.EventHandlerType");
    
                    if (del == null)
                        del = this.GetType().GetTypeInfo().GetDeclaredMethod("OnEventRaised").CreateDelegate(eventInfo.EventHandlerType, this);
    
                    return del;
                }
    
                private void OnEventRaised(object sender, object e)  // the second parameter in Windows.UI.Xaml.EventHandler is Object
                {
                    ICommand command = (ICommand)(sender as DependencyObject).GetValue(AttachedCommand.CommandProperty);
                    object commandParameter = (sender as DependencyObject).GetValue(AttachedCommand.CommandParameterProperty);
                    if (commandParameter == null) commandParameter = new object[] { sender, e };
    
                    if (command != null && command.CanExecute(commandParameter))
                    {
                        command.Execute(commandParameter);
                    }
                }
            }
        }

    使用是一样的:

    <MediaElement local:AttachedCommand.RoutedEvent="MediaOpened" local:AttachedCommand.Command="{Binding MediaOpenedCommand}"/>


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 董圣博 2012年8月31日 8:33
    2012年8月31日 8:21
    版主
  • 好的,我尝试一下,麻烦您了
    2012年8月31日 8:33