1

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>
Mooffy
  • 13
  • 3
  • Welcome to SO. Hope you don't mind but I have re-worded your title and formatted your post a bit. Good luck! –  Apr 24 '21 at 23:31

1 Answers1

0

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.

Aelarion
  • 397
  • 4
  • 11
  • I'll have a go at that but I was struggling to find a way to implement the button functionality so thank you very much! – Mooffy Apr 26 '21 at 09:41
  • Ah ok, I was confused by your code because I thought your intention was to create a custom "button" and use that instead of the built in control :) your best bet is to add the button tag into your XAML where you want it, and then let auto complete insert your "Click=" method (it will put the proper code behind and link it up) – Aelarion Apr 26 '21 at 18:23
  • My only follow up question is what is the best way to call the video player from inside of the c# function. VS 2019 doesn't like the private void part either but i'll try my best to get it to work. – Mooffy Apr 27 '21 at 08:31
  • You can actually check out [this answer](https://stackoverflow.com/a/1477108/15744261) for finding an element by name -- in your case, you've called it `Media1`. However you may want to open a new question, since the answer may change depending on what you want to do. Regarding the `private void` of the Click handler, you can change the access modifier if really necessary but a private method should be fine for calling that from the `MainWindow`. You may be having a different issue (e.g. trying to call the event handler from another namespace or class or something). – Aelarion Apr 27 '21 at 14:38
  • The private void bit is now working so who knows why it didn't like it last night. I understand it enough now to eat least google the right questions. Thanks for all the help! – Mooffy Apr 28 '21 at 04:34
  • Anytime, glad to help. If you wouldn't mind accepting the answer that would help others who come across your question :) – Aelarion Apr 28 '21 at 04:35