的Windows Phone 8滑盖绑定工作,只有在点击后 [英] Windows phone 8 slider binding works only after a click

查看:116
本文介绍了的Windows Phone 8滑盖绑定工作,只有在点击后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Windows Phone 8,我创建了一个的MediaElement和寻求杆(滑块)的音频应用程序:

I am writing an audio app on Windows Phone 8. I've created a MediaElement and a seek-bar(slider):

<MediaElement x:Name="player" CurrentStateChanged="GetTrackDuration" />
<Slider x:Name="playerSeekBar" Value="{Binding ElementName=player, Path=Position, 
 Mode=TwoWay, Converter={StaticResource PositionConverter}}" SmallChange="1" LargeChange="1"/>



这是我的转换代码:

And this is my converter code:

public class PositionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double position = 0;
        TimeSpan timespan = TimeSpan.Parse(value.ToString());
        position = timespan.TotalSeconds;

        return position;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
            return TimeSpan.FromSeconds((double)value);
    }
}



这里是CurrentStateChanged事件代码:

And here is the CurrentStateChanged event code:

private void GetTrackDuration(object sender, RoutedEventArgs e)
{
    var player = (MediaElement)sender;
    if (player.CurrentState == System.Windows.Media.MediaElementState.Playing)
        playerSeekBar.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
}



这一切似乎工作确定,但是有一个问题与绑定滑块 - 它不更新,直到我点击某处里面的应用程序 - 我的意思是我可以点击不与滑块或媒体元素或一个空连接的按钮。我后,点击滑块正在更新,一切工作不错。当滑块不更新,甚至在开始 - 顺便说一句,音乐发挥正常。我试图寻找在互联网上,但我不知道要问什么,这就是为什么我要求你的帮助。如果有人只知道在那里我可以搜索解决方案,我将非常感激:!)

It all seems to work OK, however there is one problem with the binding to slider - it doesn't update until I click somewhere inside the app - I mean i may click on a button that isn't connected with slider or media element or on a empty space. After I click the slider is being updated and everything works nice. BTW, the music plays normally - even at the beginning when the slider is not being updated. I tried to look on the Internet, however I am not sure what to ask, that's why I am asking You for help. If someone just knows where I could search for the solution, I would be very grateful!:)

感谢您对提前帮助

推荐答案

它看起来像当滑块获得焦点的一个问题,我试图找到重定向对焦的解决方案,但到目前为止 - 我还没有找到它。相反,我有一个不同势提议,几句话对你的代码:

It looks like a problem when Slider gets Focus, I've tried to find a solution with redirecting Focus, but so far - I haven't found it. Instead I've a diffrent proposal and few remarks to your code:


  1. 获取持续跟踪媒体时,打开是不是在PlayState变化

  1. Get track duration when Media is Opened not when PlayState changes:

private void player_MediaOpened(object sender, RoutedEventArgs e)
{
    playerSeekBar.Maximum = (sender as MediaElement).NaturalDuration.TimeSpan.TotalSeconds;
}


  • 您滑块可能会被MediaElement的立场的每一个小变化更新。我想是不是需要它 - 例如,它可以每秒进行更新。所以我的建议是 - 你的滑块绑定属性,并通知的PropertyChanged每一秒(使用 DispatcherTimer ):

    // In this case we need INotifyPropertyChanged - 
    public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
    {
    
    // implementing interface
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaiseProperty(string property = null)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
    
    // Property for Binding
    public double SlideValue
    {
        get { return player.Position.TotalSeconds; }
        set { player.Position = TimeSpan.FromSeconds(value); }
    }
    
    DispatcherTimer timer = new DispatcherTimer(); // timer
    
    // Get the duration when Media File is opened
    private void player_MediaOpened(object sender, RoutedEventArgs e)
    {
        playerSeekBar.Maximum = (sender as MediaElement).NaturalDuration.TimeSpan.TotalSeconds;
    }
    
    public MainPage()
    {
        InitializeComponent();
        this.DataContext = this;  // Set the DataContext
        Play.Click += Play_Click;  // My play method
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (s, e) => { RaiseProperty("SlideValue"); };
    }
    
    private void Play_Click(object sender, RoutedEventArgs e)
    {
        player.AutoPlay = true;
        player.Source = new Uri("music.mp3", UriKind.RelativeOrAbsolute);
        timer.Start();  // DON'T forget to start the timer.
    }
    


  • 在这种情况下,不再需要转换器,你的XAML代码可以是这样的:

    In this case you no longer need Converters, and your XAML code can look like this:

    <MediaElement x:Name="player" MediaOpened="player_MediaOpened"/>
    <Slider x:Name="playerSeekBar" Value="{Binding SlideValue, Mode=TwoWay}" SmallChange="1" LargeChange="1"/>
    



    以上代码可能仍然需要一些改进,但工作得很好。

    Above code probably still needs some improvements, but works quite fine.

    修改 - 方法,无需数据绑定和INotifyPropertyChanged的

    EDIT - method without DataBinding and INotifyPropertyChanged

    您也可以完成你的任务更简单的方法没有约束力,仅仅使用定时 LostMouseCapture

    You can also accomplish your task simpler way without Binding, just using Timer and LostMouseCapture:

    public partial class MainPage : PhoneApplicationPage
    {
        private double totalSeconds = 1;
    
        DispatcherTimer timer = new DispatcherTimer();
    
        private void player_MediaOpened(object sender, RoutedEventArgs e)
        {
            totalSeconds = (sender as MediaElement).NaturalDuration.TimeSpan.TotalSeconds;
        }
    
        public MainPage()
        {
            InitializeComponent();
            Play.Click += Play_Click;
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += (s, e) => { playerSeekBar.Value += (double)(1 / totalSeconds); };
            playerSeekBar.LostMouseCapture += (s, e) =>
            { player.Position = TimeSpan.FromSeconds(playerSeekBar.Value * totalSeconds); };
        }
    
        private void Play_Click(object sender, RoutedEventArgs e)
        {
            player.AutoPlay = true;
            player.Source = new Uri("music.mp3", UriKind.RelativeOrAbsolute);
            timer.Start();  // DON'T forget to start the timer.
        }
    }
    

    在XAML:

    <MediaElement x:Name="player" MediaOpened="player_MediaOpened"/>
    <Slider x:Name="playerSeekBar" Value="0" SmallChange="0.01" Maximum="1.0"/>
    



    希望这有助于。

    Hope this helps.

    这篇关于的Windows Phone 8滑盖绑定工作,只有在点击后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆