c# - 我尝试在C#中进行编译时,此错误是什么意思?

标签 c# compiler-errors

这是代码

namespace FitnessApp
{
    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Database db = new Database();
        Exercise exercise = db.getAllMuscleGroups();
        lbMuscleGroup.Items.Add(exercise);
    }
}

}

和xaml代码:
<Window x:Class="FitnessApp.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:FitnessApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <ListBox x:Name="lbMuscleGroup" HorizontalAlignment="Left" Height="106" Margin="86,144,0,0" VerticalAlignment="Top" Width="145" SelectionChanged="lbMuscleGroup_SelectionChanged"/>
    <ListBox x:Name="lbSelected" HorizontalAlignment="Left" Height="100" Margin="334,149,0,0" VerticalAlignment="Top" Width="140"/>
</Grid>

这是错误:

Error CS1061 'MainWindow' does not contain a definition for 'lbMuscleGroup_SelectionChanged' and no accessible extension method 'lbMuscleGroup_SelectionChanged' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?) FitnessApp C:\Users\Remco\OneDrive - Office 365 Fontys\FUN12\Killer-app\FitnessApp\FitnessApp\MainWindow.xaml 10 Active



如何解决此错误?不断收到此错误。很烦人。

最佳答案

在您的XAML代码中,您将“lblMuscleGroup”定义为具有on change事件:

SelectionChanged="lbMuscleGroup_SelectionChanged"

在MainWindow代码中,您没有定义此名称,因此它说:“嘿!我正在寻找您说过的东西,但您没有,您忘了什么吗?”

因此,添加以下内容应该可以:
private void lbMuscleGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //something happens
}

或者,您可以从XAML中删除“SelectionChanged =“lbMuscleGroup_SelectionChanged”部分。

希望这可以帮助!

关于c# - 我尝试在C#中进行编译时,此错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53534423/

相关文章:

c - 如何将Golang的cgo与链接到math.h的C库一起使用?

c - 我试图将 return 放在代码末尾,但他们总是在 return 之前用 while 搞乱我

asp.net-mvc - MVC 3.0缺少编译器所需的错误

使用 Flex 和 Bison 指定前缀时编译错误

c# - Schema First WCF Web 服务模型的优势?

c# - <typeparamref> 到 C# XML 文档中另一个类的参数

c# - C++ 和 .NET 是否在银行、医疗保健和电信等领域一起使用

c# - 如何防止 MemberInfo.IsDefined 在不相关的属性上抛出 FileNotFoundException?

c# - 在电影院对 friend 进行分组的算法

c++ - 未解析的外部符号 b3Clock