MediaTimeline 中的绑定源

发布于 2024-10-08 17:05:06 字数 1167 浏览 2 评论 0原文

我试图将媒体时间线绑定到 Uri,如下所示:

<UserControl.Resources>
    <Storyboard x:Key="myStoryboard">
        <MediaTimeline Storyboard.TargetName="myMediaPlayer"
                       Source="{Binding MediaSource}"
                       RepeatBehavior="Forever" />
    </Storyboard>
</UserControl.Resources>

<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />
    </EventTrigger>
</UserControl.Triggers>

<Grid>
    <MediaElement x:Name="mymediaPlayer" />
</Grid>

但是,当我这样做时,它说我需要“必须指定 URI”。调度员异常。在视图模型中,我有一个如下属性:

    public Uri MediaSource
    {
        get { return _mediaSource; }
        set
        {
            if (_oscilloscopeSource != value)
            {
                _mediaSource= value;
                OnPropertyChanged("MediaSource");
            }
        }
    }

似乎加载媒体播放器时,它不会从绑定中读取源。什么给?

在构造函数中,我有:

_mediaSource = new Uri(@"C:\someMovie.mov", UriKind.Absolute);

谢谢。

I am trying to have the mediatimeline bind to a Uri like so:

<UserControl.Resources>
    <Storyboard x:Key="myStoryboard">
        <MediaTimeline Storyboard.TargetName="myMediaPlayer"
                       Source="{Binding MediaSource}"
                       RepeatBehavior="Forever" />
    </Storyboard>
</UserControl.Resources>

<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />
    </EventTrigger>
</UserControl.Triggers>

<Grid>
    <MediaElement x:Name="mymediaPlayer" />
</Grid>

However, when I do this, it says that I need to "Must Specify URI." Dispatcher exception. In the viewmodel, I have a property like:

    public Uri MediaSource
    {
        get { return _mediaSource; }
        set
        {
            if (_oscilloscopeSource != value)
            {
                _mediaSource= value;
                OnPropertyChanged("MediaSource");
            }
        }
    }

It seems as though when the media player is loaded, it doesn't read the source from the binding. What gives?

In the constructor, I have:

_mediaSource = new Uri(@"C:\someMovie.mov", UriKind.Absolute);

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2024-10-15 17:05:06

更新
无法让它工作,所以现在在黑暗中拍摄。将触发器移至 MediaElement 会产生影响吗?

<MediaElement x:Name="myMediaPlayer">
    <MediaElement.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />
        </EventTrigger>
    </MediaElement.Triggers>
</MediaElement>

我尝试过这个,它对我有用。我能想到的可能原因。

  • 您是否为 UserControl 设置了 DataContext?
  • 直接设置 _mediaSource 不会调用 OnPropertyChanged,因为您没有设置 CLR 属性。设置 MediaSource 代替。
  • 您的 MediaElement 名为 mymediaPlayer,而不是 myMediaPlayer 作为 TargetName。 (打字错误?)

除了我更改的 MediaElement 名称之外,我的工作 xaml 与您的相同。这是我的完整代码隐藏文件

public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
    public UserControl1()
    {
        InitializeComponent();
        MediaSource = new Uri("C:\\C1.MOV");
        this.DataContext = this;
    }

    private Uri _mediaSource;
    public Uri MediaSource
    {
        get
        {
            return _mediaSource;
        }
        set
        {
            _mediaSource = value;
            OnPropertyChanged("MediaSource");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Update
Can't get this to work so shooting in the dark now. Does moving the trigger to MediaElement make a difference?

<MediaElement x:Name="myMediaPlayer">
    <MediaElement.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />
        </EventTrigger>
    </MediaElement.Triggers>
</MediaElement>

I tried this out and it works for me. Possible reasons I can think of.

  • Do you have the DataContext set for the UserControl?
  • Setting _mediaSource directly won't call OnPropertyChanged since you're not setting the CLR property. Set MediaSource instead.
  • Your MediaElement is named mymediaPlayer and not myMediaPlayer as the TargetName. (Typo?)

Except for the MediaElement Name which I changed, my working xaml is identical to yours. This is my full code behind file

public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
    public UserControl1()
    {
        InitializeComponent();
        MediaSource = new Uri("C:\\C1.MOV");
        this.DataContext = this;
    }

    private Uri _mediaSource;
    public Uri MediaSource
    {
        get
        {
            return _mediaSource;
        }
        set
        {
            _mediaSource = value;
            OnPropertyChanged("MediaSource");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文