如何为我的 XAML 按钮创建单击处理程序?

问题描述

我一直在尝试使用一个按钮来通过 windows 媒体播放器显示视频(也可以循环播放,但我已经解决了)。

我对 c# 很陌生,所以到目前为止这些都是非常基本的代码

我不确定如何将 xaml 中的按钮链接到我的 c# 代码

这是我的代码

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 { 

    }


}

xml

<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" 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,22" AutomationProperties.Name="Brakelights"/>
    <StackPanel Margin="0,65,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>

解决方法

如果您只想让按钮从 UI 执行功能,您可以像添加 XAML 中的任何其他元素一样添加它:

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

如果你想告诉按钮在点击时执行某事,那么你可以这样做:

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

MainWindow 的代码隐藏中,实现该方法:

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

我不会讨论命令或 MVVM 或任何其他内容,只要知道这不一定是大型项目的最佳设计模式。


然而,这部分让我在你的代码中有点困惑:

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

您正在 Button 命名空间中创建一个类 Taillight_Project_3._1,除了它继承的内容外,没有任何功能或定义。我想这不会编译,因为您没有实现 IPostBackEventHandlerIButtonControl 接口。