我刚刚开始使用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中调用可以正常工作。
答案 0 :(得分:3)
(this.FindResource("energyDataSet")));
(Application.Current.FindResource("EnergyDataSet")));
请注意,资源名称区分大小写! energyDataSet vs EnergyDataSet。