ResourceReferenceKeyNotFoundException

本文关键字:ResourceReferenceKeyNotFoundException | 更新日期: 2023-09-27 18:17:38

我刚刚开始使用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();
            }
        }
    }
}

下面一行的第一个实例出现错误:

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

在Window_Loaded中使用'this'调用工作正常

ResourceReferenceKeyNotFoundException

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

注意资源名是区分大小写的!

相关文章:
  • 没有找到相关文章