简体   繁体   中英

How can I interact with a property in another frame? (C# / WPF)

I'm currently learning C# and WPF. Therefore I'm programming a little music player. But now I came to a problem. I was implementing different frames for menu items and other things I display.

Then my problem occoured. I have a MediaElement in my MainWindow.XAML(.CS) and I have to interact with that Element also in my Player.XAML(.CS) - Which obviously controls it (the Player can pause, play, stop, etc the MediaElement). The other class CurrentPlaylist.XAML(.CS) displays the current Playlist in a Datagrid. It shows information and can interact with the MediaElement (Setting a new Song / Start to play the song).

I really have no idea on how to achieve it when I have different Frames.

Here's a little bit of Code.

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);
    }
}

This was my latest idea. But well as I thought it won't work.

I would appreciate if you guys would give a little poke towards the answer. :)

I'm not entirely sure what you mean by frames, so that may be the problem. But, this worked for me.

' 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

Also, I decided to stop using the media element all together because WMP (Actually, all Windows Media tech. I just uninstall it.) is nothing but a problem when you use it with iTunes. When I attempt my next one I will use a package that I found, easily find one in Tools -> Nuget Package Manager -> Manage NuGet Packages for Solution. Search for something like audio/music player.

The one I found, and was great for button beeps, was called NAudio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM