Search code examples
c#xamlwindows-media-player

How to create a click handler for my XAML button?


I have been trying to have a button that displays a video (which also loops but i've worked that out) through windows media player.

I am very new to c# so this is all pretty basic code so far.

i'm not sure how to link a button in xaml to my c# code.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace Taillight_Project_3._1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

    
    }
    public class Button : System.Web.UI.WebControls.WebControl, System.Web.UI.IPostBackEventHandler,System.Web.UI.WebControls.IButtonControl { 

    }


}

xaml

<Window x:Class="Taillight_Project_3._1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Taillight_Project_3._1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"
    xmlns:gif ="https://github.com/XamlAnimatedGif/XamlAnimatedGif">
<Grid>
    <TextBox HorizontalAlignment="Center" Margin="0,10,0,0" Text="Sequential Taillight Simulator 2021" TextWrapping="Wrap" VerticalAlignment="Top" Width="780" Height="50" FontFamily="Bahnschrift" FontSize="50" FontWeight="Normal" FontStyle="Normal" TextDecorations="{x:Null}"/>
    <RadioButton x:Name="BrakeLights" HorizontalAlignment="Center" Width="100" Content="Brake Lights" Margin="0,396,0,22" AutomationProperties.Name="Brakelights"/>
    <StackPanel Margin="0,65,0,43">
        <MediaElement Name="Media1" >
            <MediaElement.Triggers>
                <EventTrigger RoutedEvent="MediaElement.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <MediaTimeline Source="C:\Users\n.a.smith\source\repos\Taillight Project 3.1\Taillight Project 3.1\files\Brake Lights Video.mp4" Storyboard.TargetName="myMediaElement"  RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </MediaElement.Triggers>
        </MediaElement>
    </StackPanel>
</Grid>
</Window>

Solution

  • If all you want to do is have a button execute a function from the UI, you add it like any other element in your XAML:

    ...
    <Button Content="My Button" ... />
    ...
    

    If you want to tell the button to execute something when it's clicked, then you can do:

    <Button Content="My Button" Click="Button_Click" />
    

    And in your code-behind for MainWindow, implement that method:

    ...
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // do something when clicked
    }
    ...
    

    I won't go into commands or MVVM or any of that, just know that this isn't necessarily the best design pattern for larger projects.


    However, this part has me a bit confused in your code:

    public class Button : System.Web.UI.WebControls.WebControl, System.Web.UI.IPostBackEventHandler,System.Web.UI.WebControls.IButtonControl
    

    You're creating a class Button in the Taillight_Project_3._1 namespace with no functionality or definition, other than what it inherits. I would imagine this doesn't compile because you aren't implementing the IPostBackEventHandler or IButtonControl interfaces.