WPF 中自动生成的代码显示错误。 (C#、WPF、Visual Studio 社区 2013)
Auto-generated code in WPF shows error. (C#, WPF, Visual Studio Community 2013)
最近我开始在 Visual Studio 2013 社区中使用 C#。当我遇到以下错误时,我正在创建一个简单的数据绑定。我正在做的是使用拖放从我的数据库到主 window。 Visual studio 生成了所需的 XAML 和 .cs 文件。代码存在以下错误。我搜索了很多,但没有找到解决方案。它显示在
错误:"an object reference is required to access non-static field method or property"
以下是我的XAML。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:rrplproj" x:Class="rrplproj.MainWindow"
Title="MainWindow" Height="700" Width="1024" WindowState="Maximized" Loaded="Window_Loaded">
<Window.Resources>
<local:rDataSet x:Key="rDataSet"/>
<CollectionViewSource x:Key="dateViewSource" Source="{Binding Date, Source={StaticResource rDataSet}}"/>
<CollectionViewSource x:Key="deliveryViewSource" Source="{Binding Delivery,
Source={StaticResource rDataSet}}"/>
</Window.Resources>
<Grid Background="#FFDBFDF8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="165*"/>
<ColumnDefinition Width="172*"/>
<ColumnDefinition Width="171*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="56*"/>
<RowDefinition Height="149*"/>
<RowDefinition Height="18*"/>
</Grid.RowDefinitions>
<Grid x:Name="grid1" DataContext="{StaticResource dateViewSource}" HorizontalAlignment="Left"
Margin="19,23,0,0" Grid.Row="1" VerticalAlignment="Top" Height="32" Width="151">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Date:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/>
<DatePicker x:Name="dateDatePicker" Grid.Column="1" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
SelectedDate="{Binding Date, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center"/>
</Grid>
<Grid x:Name="grid2" Grid.Column="1" DataContext="{StaticResource deliveryViewSource}"
HorizontalAlignment="Left" Margin="64,18,0,0" Grid.Row="1" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Truck No:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/>
<TextBox x:Name="truckNoTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3"
Grid.Row="0" Text="{Binding TruckNo, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center" Width="120"/>
</Grid>
</Grid>
以下是 ide 在 C# 中自动生成的代码。
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;
namespace rrplproj
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Load data into the table Date. You can modify this code as needed.
rrplproj.rDataSetTableAdapters.DateTableAdapter rDataSetDateTableAdapter =
new rrplproj.rDataSetTableAdapters.DateTableAdapter();
rDataSetDateTableAdapter.Fill(rDataSet.Date);
System.Windows.Data.CollectionViewSource dateViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("dateViewSource")));
dateViewSource.View.MoveCurrentToFirst();
// Load data into the table Delivery. You can modify this code as needed.
rrplproj.rDataSetTableAdapters.DeliveryTableAdapter rDataSetDeliveryTableAdapter =
new rrplproj.rDataSetTableAdapters.DeliveryTableAdapter();
rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);
System.Windows.Data.CollectionViewSource deliveryViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("deliveryViewSource")));
deliveryViewSource.View.MoveCurrentToFirst();
}
}
}
此处显示错误
rDataSetDateTableAdapter.Fill(rDataSet.Date);
这里:
rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);
下方显示红线
(rDataSet.Date)
和
(rDataSet.Delivery)
感谢您的帮助。
感谢您阅读到最后一行。
从未尝试过使用数据集,但这很简单意味着它无法创建此命名空间中的 rDataSet 实例 - rrplproj 我们需要一些关于 rDataSet 的信息才能进一步理解它..另请阅读关于在 xaml..
中创建数据集实例
或者在你后面代码的构造函数中写这个...
myObj = new rDataSet();
this.Resources.Add("rDataSet", myObj);
并将其从您的 xaml
中删除
<local:rDataSet x:Key="rDataSet"/>
这会向 rDataSet 添加资源
最近我开始在 Visual Studio 2013 社区中使用 C#。当我遇到以下错误时,我正在创建一个简单的数据绑定。我正在做的是使用拖放从我的数据库到主 window。 Visual studio 生成了所需的 XAML 和 .cs 文件。代码存在以下错误。我搜索了很多,但没有找到解决方案。它显示在
错误:"an object reference is required to access non-static field method or property"
以下是我的XAML。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:rrplproj" x:Class="rrplproj.MainWindow"
Title="MainWindow" Height="700" Width="1024" WindowState="Maximized" Loaded="Window_Loaded">
<Window.Resources>
<local:rDataSet x:Key="rDataSet"/>
<CollectionViewSource x:Key="dateViewSource" Source="{Binding Date, Source={StaticResource rDataSet}}"/>
<CollectionViewSource x:Key="deliveryViewSource" Source="{Binding Delivery,
Source={StaticResource rDataSet}}"/>
</Window.Resources>
<Grid Background="#FFDBFDF8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="165*"/>
<ColumnDefinition Width="172*"/>
<ColumnDefinition Width="171*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="56*"/>
<RowDefinition Height="149*"/>
<RowDefinition Height="18*"/>
</Grid.RowDefinitions>
<Grid x:Name="grid1" DataContext="{StaticResource dateViewSource}" HorizontalAlignment="Left"
Margin="19,23,0,0" Grid.Row="1" VerticalAlignment="Top" Height="32" Width="151">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Date:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/>
<DatePicker x:Name="dateDatePicker" Grid.Column="1" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
SelectedDate="{Binding Date, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center"/>
</Grid>
<Grid x:Name="grid2" Grid.Column="1" DataContext="{StaticResource deliveryViewSource}"
HorizontalAlignment="Left" Margin="64,18,0,0" Grid.Row="1" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Truck No:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/>
<TextBox x:Name="truckNoTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3"
Grid.Row="0" Text="{Binding TruckNo, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center" Width="120"/>
</Grid>
</Grid>
以下是 ide 在 C# 中自动生成的代码。
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;
namespace rrplproj
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Load data into the table Date. You can modify this code as needed.
rrplproj.rDataSetTableAdapters.DateTableAdapter rDataSetDateTableAdapter =
new rrplproj.rDataSetTableAdapters.DateTableAdapter();
rDataSetDateTableAdapter.Fill(rDataSet.Date);
System.Windows.Data.CollectionViewSource dateViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("dateViewSource")));
dateViewSource.View.MoveCurrentToFirst();
// Load data into the table Delivery. You can modify this code as needed.
rrplproj.rDataSetTableAdapters.DeliveryTableAdapter rDataSetDeliveryTableAdapter =
new rrplproj.rDataSetTableAdapters.DeliveryTableAdapter();
rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);
System.Windows.Data.CollectionViewSource deliveryViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("deliveryViewSource")));
deliveryViewSource.View.MoveCurrentToFirst();
}
}
}
此处显示错误
rDataSetDateTableAdapter.Fill(rDataSet.Date);
这里:
rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);
下方显示红线
(rDataSet.Date)
和
(rDataSet.Delivery)
感谢您的帮助。
感谢您阅读到最后一行。
从未尝试过使用数据集,但这很简单意味着它无法创建此命名空间中的 rDataSet 实例 - rrplproj 我们需要一些关于 rDataSet 的信息才能进一步理解它..另请阅读关于在 xaml..
中创建数据集实例或者在你后面代码的构造函数中写这个...
myObj = new rDataSet();
this.Resources.Add("rDataSet", myObj);
并将其从您的 xaml
中删除<local:rDataSet x:Key="rDataSet"/>
这会向 rDataSet 添加资源