gpt4 book ai didi

c# - ResourceReferenceKeyNotFoundException

转载 作者:行者123 更新时间:2023-11-30 16:26:47 24 4
gpt4 key购买 nike

我刚刚开始使用 Visual Studio C# Express。我在构建一个小型数据库应用程序时遇到了以下问题。我得到一个 System.Windows.ResourceReferenceKeyNotFoundException

这是 MainWindow.cs 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
WpfApplication1.EnergyDataSet energyDataSet = ((WpfApplication1.EnergyDataSet)(this.FindResource("energyDataSet")));

// Load data into the table energy. You can modify this code as needed.
WpfApplication1.EnergyDataSetTableAdapters.energyTableAdapter energyDataSetenergyTableAdapter = new WpfApplication1.EnergyDataSetTableAdapters.energyTableAdapter();
energyDataSetenergyTableAdapter.Fill(energyDataSet.energy);
// add a ColumnChanged event handler for the table.
energyDataSet.energy.ColumnChanged += new
DataColumnChangeEventHandler(Column_Changed);

System.Windows.Data.CollectionViewSource energyViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("energyViewSource")));
energyViewSource.View.MoveCurrentToFirst();

}

private static void Column_Changed(object sender, DataColumnChangeEventArgs e)
{
WpfApplication1.EnergyDataSet energyDataSet = ((WpfApplication1.EnergyDataSet)(Application.Current.FindResource("EnergyDataSet")));

if (e.Column.ColumnName == "Gas Reading")
{

DataRowCollection rowCollection = energyDataSet.Tables["energy"].Rows;
DataRow foundRow = energyDataSet.Tables["energy"].Rows.Find(e.Row["DateTime"]);
int frIndex = rowCollection.IndexOf(foundRow);
energyDataSet.Tables["energy"].Rows[frIndex]["Gas_Diff"] =
(float)energyDataSet.Tables["energy"].Rows[frIndex]["Gas_Reading"] -
(float)energyDataSet.Tables["energy"].Rows[frIndex - 1]["Gas_Reading"];
energyDataSet.AcceptChanges();

}
if (e.Column.ColumnName == "Elec Reading")
{
DataRow foundRow = energyDataSet.Tables["energy"].Rows.Find(e.Row["DateTime"]);
int frIndex = energyDataSet.Tables["energy"].Rows.IndexOf(foundRow);
energyDataSet.Tables["energy"].Rows[frIndex]["Elec_Diff"] =
(float)energyDataSet.Tables["energy"].Rows[frIndex]["Elec_Reading"] -
(float)energyDataSet.Tables["energy"].Rows[frIndex - 1]["Elec_Reading"];
energyDataSet.AcceptChanges();
}
}
}
}

错误发生在以下行的 Coloumn_Changed 中的第一个实例:

WpfApplication1.EnergyDataSet energyDataSet = ((WpfApplication1.EnergyDataSet)(Application.Current.FindResource("EnergyDataSet")));

使用“this”在 Window_Loaded 中调用正常。

最佳答案

(this.FindResource("energyDataSet")));
(Application.Current.FindResource("EnergyDataSet")));

请注意,资源名称区分大小写! energyDataSet 与 EnergyDataSet。

关于c# - ResourceReferenceKeyNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8492827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com