集合由于其保护级别错误 WPF C# 而无法访问
本文关键字:访问 WPF 错误 于其 保护 集合 | 更新日期: 2023-09-27 18:36:10
嗨,我对 wpf 和 c# 很陌生。我写了一个简单的WPF应用程序,但由于错误collection is inaccessible due to its protection level error
而无法正常工作。我想知道我做错了什么?
这是我的代码:
它只是一个虚拟程序,还有很多事情要做,例如创建另一个窗口来添加和编辑收集项,但我无法克服保护级别错误的障碍。谁能帮我修复它?
主窗口 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;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.IO;
namespace cars
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Cars> cars;
Random ran = new Random(Environment.TickCount);
public MainWindow()
{
InitializeComponent();
}
//start of window loaded
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//clock############################################
DateTime.Now.ToLongTimeString(); //#
DispatcherTimer clock = new DispatcherTimer(); //#
clock.Interval = new TimeSpan(0, 0, 1); //#
clock.Tick += clock_Tick; //#
clock.Start(); //#
//#################################################
//creating new observable collection for my cars
cars = new ObservableCollection<Cars>();
// adding 3 cars in to collection ########################################
cars.Add(new Cars(001, Type.Coupe, "Porsche", Fuel.Petrol, "Red")); //#
cars.Add(new Cars(002, Type.Coupe, "Ferrari", Fuel.Petrol, "Blue")); //#
cars.Add(new Cars(003, Type.Coupe, "McLaren", Fuel.Petrol, "Yellow")); //#
//########################################################################
//displaing collection in lable
lbxCars.ItemsSource = cars;
//start randomly picking car from collection
PickRandomCar();
}//end of window loaded
//pick random car method ##############################
private void PickRandomCar() //#
{ //#
Cars randCars = cars[ran.Next(cars.Count)]; //#
lblRandom.Content = randCars; //#
} //#
//#####################################################
//method to display time in text box ##################
void clock_Tick(object sender, EventArgs e) //#
{ //#
tbxClock.Text = DateTime.Now.ToLongTimeString();//#
} //#
//#####################################################
//methodfor adding cars in to cars collection ###################
private void btnAddCar_Click(object sender, RoutedEventArgs e)//#
{ //#
AddCars AddCars = new AddCars();
AddCars.Owner = this;
AddCars.ShowDialog();
} //#
//###############################################################
//method to edit cars in cars collection##########################
private void btnEdidCar_Click(object sender, RoutedEventArgs e)//#
{ //#
} //#
//################################################################
private void btnDeleteCar_Click(object sender, RoutedEventArgs e)
{
}
private void btnRentCar_Click(object sender, RoutedEventArgs e)
{
}
private void btnReturnCar_Click(object sender, RoutedEventArgs e)
{
}
private void tbxFilterName_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void tbxFilterNumber_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void tbxFilterName_GotFocus(object sender, RoutedEventArgs e)
{
}
private void tbxFilterNumber_GotFocus(object sender, RoutedEventArgs e)
{
}
private void lbxMembers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
主页 xaml:
<Window x:Class="cars.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CARS" Height="420" Width="650"
Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<Style x:Key="txtblkStyle" TargetType="TextBlock">
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Background" Value="White"/>
</Style>
<Style x:Key="txtbxStyle" TargetType="TextBox">
<Setter Property="Width" Value="200"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="lblMember" TargetType="Label">
<Setter Property="Width" Value="100"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
</Window.Resources>
<Grid Background="Goldenrod">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.6*" />
<RowDefinition Height="1.5*"/>
<RowDefinition Height="0.7*"/>
<RowDefinition Height="0.7*"/>
<RowDefinition Height="0.7*"/>
<RowDefinition Height="0.7*"/>
<RowDefinition Height="0.7*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="0.7*"/>
</Grid.RowDefinitions>
<TextBox Name="tbxFilterName" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left" Style="{StaticResource txtbxStyle}" TextChanged="tbxFilterName_TextChanged" GotFocus="tbxFilterName_GotFocus" />
<TextBox Name="tbxFilterNumber" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="3" HorizontalAlignment="Right" Style="{StaticResource txtbxStyle}" TextChanged="tbxFilterNumber_TextChanged" GotFocus="tbxFilterNumber_GotFocus" />
<!--buttons-->
<Button x:Name="btnAddCar" CommandParameter="add" Content="Add Car" Grid.Row="8" Grid.Column="0" Style="{StaticResource btnStyle}" Click="btnAddCar_Click"/>
<Button x:Name="btnEdidCar" Content="Edit Car" Grid.Row="8" Grid.Column="1" Style="{StaticResource btnStyle}" Click="btnEdidCar_Click"/>
<Button x:Name="btnDeleteCar" Content="Delete Car" Grid.Row="8" Grid.Column="2" Style="{StaticResource btnStyle}" Click="btnDeleteCar_Click"/>
<Button x:Name="btnRentCar" Content="Rent Car" Grid.Row="8" Grid.Column="3" Style="{StaticResource btnStyle}" Click="btnRentCar_Click"/>
<Button x:Name="btnReturnCar" Content="Return Car" Grid.Row="8" Grid.Column="4" Style="{StaticResource btnStyle}" Click="btnReturnCar_Click"/>
<!--labels-->
<Label Name="lblNumber" Content="Number: " Grid.Row="2" Grid.Column="3" Style="{StaticResource lblMember}" />
<Label Name="lblName" Content="Type: " Grid.Row="3" Grid.Column="3" Style="{StaticResource lblMember}" Margin="5,0,0,5" VerticalAlignment="Bottom" />
<Label Name="lblPhone" Content="Name: " Grid.Row="4" Grid.Column="3" Style="{StaticResource lblMember}" />
<Label Name="lblAddress" Content="Fuel Type: " Grid.Row="5" Grid.Column="3" Style="{StaticResource lblMember}" />
<Label Name="lblRentals" Content="Colour: " VerticalAlignment="Top" Grid.Row="6" Grid.Column="3" Style="{StaticResource lblMember}" Margin="5,10,0,0" Grid.RowSpan="2" />
<!--textblocks-->
<TextBlock Name="txtblkNumber" Grid.Column="4" Grid.Row="2" Style="{StaticResource txtblkStyle}" ></TextBlock>
<TextBlock Name="txtblkName" Grid.Column="4" Grid.Row="3" Style="{StaticResource txtblkStyle}" ></TextBlock>
<TextBlock Name="txtblkPhone" Grid.Column="4" Grid.Row="4" Style="{StaticResource txtblkStyle}" ></TextBlock>
<TextBlock Name="txtblkAddress" Grid.Column="4" Grid.Row="5" Height="33" Style="{StaticResource txtblkStyle}" Margin="5,5,5,0" />
<TextBlock Name="txtblkRental" TextWrapping="Wrap" Height="30" Grid.Column="4" Grid.Row="6" Style="{StaticResource txtblkStyle}" Margin="0,10,15,0" Grid.RowSpan="2" />
<!--Style="{StaticResource txtblkStyle}"-->
<ListBox Name="lbxCars" Grid.Column="0" Grid.Row="2" Grid.RowSpan="5" Grid.ColumnSpan="3" Background="Aqua" SelectionChanged="lbxMembers_SelectionChanged" ></ListBox>
<Image Name="imgUser" Grid.Column="3" Grid.Row="1" Stretch="Uniform" ></Image>
<TextBox x:Name="tbxClock" Grid.ColumnSpan="2" Grid.Column="3" HorizontalAlignment="Left" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label x:Name="lblRandom" Content="" Grid.Column="2" HorizontalAlignment="Left" Margin="5,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="119" Height="60"/>
</Grid>
</Window>
I类使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace cars
{
//enum for fuel types
public enum Fuel
{
Petrol, Diesel, Electric, Other
}
//enum for car types
public enum Type
{
Cabriolet, Coupe, Saloon, Hatchback, Estate, MPV, SUV, Other
}
//start class
class Cars
{
//foelds and properties
public int Number { get; set; }
public Type Type { get; set; }
public string Name { get; set; }
public Fuel FuelType { get; set; }
public string Colour { get; set; }
// working constructor
public Cars(int number, Type type, string name, Fuel fuel, string colour)
{
Number = number;
Type = type;
Name = name;
FuelType = fuel;
Colour = colour;
}
// to string method
public override string ToString()
{
return string.Format(" Number: {0}.'n Type: {1}.'n Name: {2}.'n Fuel type: {3}.'n Colour: {4}. ", Number, Type, Name, FuelType, Colour);
}
//method to find text file with cars
public static string GetCarsFile()
{
string debug = Directory.GetCurrentDirectory();
string bin = Directory.GetParent(debug).ToString();
return Directory.GetParent(bin) + "''cars.txt";
}
}
}
你的类Cars
没有修饰符,所以这意味着它是private
。添加public
修饰符。
由于其
保护而无法访问 级别意味着您尝试从外部访问非公共内容 查找行并将修饰符更改为"公共"
顺便说一句,这是一个经典的RTFM问题||问谷歌