【问题标题】:How can I interact with a property in another frame? (C# / WPF)如何与另一个框架中的属性进行交互? (C#/WPF)
【发布时间】:2016-03-27 19:35:36
【问题描述】:

我目前正在学习 C# 和 WPF。因此,我正在编写一个小音乐播放器。但是现在我遇到了一个问题。我正在为菜单项和我显示的其他内容实现不同的框架。

然后我的问题出现了。我的 MainWindow.XAML(.CS) 中有一个 MediaElement,我还必须在我的 Player.XAML(.CS) 中与该元素交互 - 这显然控制它(播放器可以暂停、播放、停止等 MediaElement) .另一个类 CurrentPlaylist.XAML(.CS) 在 Datagrid 中显示当前播放列表。它显示信息并可以与 MediaElement 交互(设置新歌曲/开始播放歌曲)。

当我有不同的框架时,我真的不知道如何实现它。

这里有一点代码。

MainWindow.XAML

 <Grid>
    <MediaElement Name="media" HorizontalAlignment="Left" Width="1" Height="1" LoadedBehavior="Manual" UnloadedBehavior="Stop"  
    MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>


    <Frame Width="1280"  Height="100" Grid.ColumnSpan="2" Grid.Row="1" VerticalAlignment="Bottom" Source="Player.xaml"></Frame>

    <StackPanel HorizontalAlignment="Left" Width="50" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" Name="fullMenu" Background="{StaticResource flavorColor}" >
        <Button Style="{StaticResource seeThrough}"  Width="50" Height="70" x:Name="hamburgerMenu" Grid.Column="0" Grid.Row="0" BorderBrush="{x:Null}" Click="hamburger_Click" Cursor="Hand" HorizontalAlignment="Right">
            <Rectangle Width="30" Height="30">
                <Rectangle.Fill>
                    <DrawingBrush>
                        <DrawingBrush.Drawing>
                            <DrawingGroup>
                                <DrawingGroup.Children>
                                    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                    <GeometryDrawing Brush="White" Geometry="F1M13,11L3,11 3,13 13,13z M13,7L3,7 3,9 13,9z M13,5L3,5 3,3 13,3z" />
                                </DrawingGroup.Children>
                            </DrawingGroup>
                        </DrawingBrush.Drawing>
                    </DrawingBrush>
                </Rectangle.Fill>
            </Rectangle>

        </Button>

        <Button Click="default_Playlist_Click" x:Name="item1" Style="{StaticResource seeThrough}"  Content="{Binding CurrentPlaylist.Name}" Visibility="Collapsed"></Button>

        <Button Click="playlist_List_Click" x:Name="item2" Style="{StaticResource seeThrough}"  Content="Playlists" Visibility="Collapsed"></Button>

        <Button Click="full_Player_Click" x:Name="item3" Style="{StaticResource seeThrough}"   Content="MaxPlayer" Visibility="Collapsed"></Button>
    </StackPanel>
</Grid>

Player.XAML

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition />
    </Grid.RowDefinitions>



    <Slider x:Name="timeLine" Grid.Column="0" VerticalAlignment="Center"/>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="1" Grid.Row="2">

        <Button x:Name="previousSong" Width="75" Height="30" Margin="10,0,10,20">
            &lt;&lt;
        </Button>
        <Button Click="PlayPause" x:Name="playSong" Width="75" Height="30" Margin="10,0,10,20">
            ||
        </Button>
        <Button x:Name="nextSong" Width="75" Height="30" Margin="10,0,10,20">
            &gt;&gt;
        </Button>
    </StackPanel>
</Grid>

Player.XAML.cs

public partial class Player : Page
{

    private TimeSpan TotalTime;
    DispatcherTimer timer;
    private bool playerRunning = false;
    private MainWindow mw;
    MediaElement Media;

    public Player(MainWindow mw)
    {
        this.mw = mw;
        Media = mw.Media;
        InitializeComponent();

        timeLine.AddHandler(MouseLeftButtonUpEvent,
                  new MouseButtonEventHandler(SeekMediaPosition),
                  true);

        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += new EventHandler(timer_Tick);
    }


    public void timer_Tick(object sender, EventArgs e)
    {
        timeLine.Value = timeLine.Value + 1000;
    }

    private void PlayPause(object sender, RoutedEventArgs e)
    {
        if (Media.Source != null && playerRunning == true)
        {

            Media.Pause();
            playSong.Content = ">";
            playerRunning = false;
        }
        else if (Media.Source != null && playerRunning == false)
        {
            Media.Play();
            playSong.Content = "||";
            playerRunning = true;
        }
    }

    private void Element_MediaOpened(object sender, EventArgs e)
    {
        timeLine.Maximum = Media.NaturalDuration.TimeSpan.TotalMilliseconds;
        TotalTime = Media.NaturalDuration.TimeSpan;
        timer.Start();

    }


    private void Element_MediaEnded(object sender, EventArgs e)
    {
        Media.Stop();
    }

    private void SeekMediaPosition(object sender, MouseButtonEventArgs args)
    {
        int val = (int)timeLine.Value;
        Media.Position = new TimeSpan(0, 0, 0, 0, val);
    }
}

这是我最新的想法。但是,我认为它不会起作用。

如果你们能稍微戳一下答案,我将不胜感激。 :)

【问题讨论】:

  • “不会工作”根本没有信息!
  • 因为它是一个新对象并且不会更新我的 MainWindow 中的 MediaElement 它不会改变任何东西。

标签: c# wpf xaml frames audio-player


【解决方案1】:

我不完全确定您所说的帧是什么意思,所以这可能是问题所在。但是,这对我有用。

' Configure open file dialog box 
Dim dlg As New Microsoft.Win32.OpenFileDialog()
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) & "iTunes\iTunes Media\Music"
dlg.FileName = "" ' Default file name
dlg.DefaultExt = ".txt" ' Default file extension
'dlg.Filter = "Music file (.mp3)|*.mp3;(.mp4)|*.mp4"    ' Filter files by extension
dlg.Filter = "Music file (.mp3,.m4a)|*.mp3;*.m4a"   ' Filter files by extension

' Show open file dialog box 
Dim result? As Boolean = dlg.ShowDialog()

' Process open file dialog box results 
If result = True Then
    ' Open document 
    Dim filename As String = dlg.FileName
    Dim baseUri As New Uri(filename)
    'MsgBox(filename)

    myMediaElement.Stop()
    myMediaElement.Source = baseUri
    myMediaElement.Play()

End If

另外,我决定停止同时使用媒体元素,因为 WMP(实际上,所有 Windows Media 技术。我只是将其卸载。)当您将它与 iTunes 一起使用时,它只不过是一个问题。当我尝试下一个包时,我将使用我找到的包,在工具 -> Nuget 包管理器 -> 管理 NuGet 包以解决方案中轻松找到一个。搜索音频/音乐播放器之类的东西。

我找到的一个非常适合按钮蜂鸣声的,叫做 NAudio。

【讨论】:

  • 我的意思是页面,我通过将它们链接为框架来在 WPF 中显示它们。示例:
  • 从未使用过这样的框架,事实上我什至从未使用过页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2021-08-10
  • 1970-01-01
相关资源
最近更新 更多