收集访问是由于错误wpf c#的保护水平collection is inaccessible due to its protection level error wpf c#

- 此内容更新于:2015-11-28
主题:

原文:

Hi I am quite new to wpf and c#. I have written a simple WPF app but it doesn't work because of the error collection is inaccessible due to its protection level error. I was wondering what am I doing wrong?

Here is my code:

It is only a dummy program and there is quite a lot left to do in it like creating another window to add and edit collection items, but I cant get past the hurdle of the protection level error. Can anyone help me fix it?

Main window 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)
{

}
}

Main Page 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>

Class I used:

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}.
 Type: {1}.
 Name: {2}.
 Fuel type: {3}.
 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";
    }
}
}
O. R. Mapper的回复:请通常显示的准确行出现错误,引用具体的错误信息,当你报告/询问一个编译器错误或异常。

(原文:Please generally indicate the exact line where the error occurs, and cite the exact error message, when you report/ask about a compiler error or an exception.)

解决方案:
你们班汽车没有修饰符,这意味着它是私人的。增加公众的修饰符。
原文:

Your class Cars is without modifier, so it means that it is private. Add public modifier.

usernike80的回复:嘿,快!我觉得有点尴尬,但是非常感谢你的帮助。

(原文:Heh so quick! I feel little bit embarrassed, but thanks very much for your help.)

解决方案:
无法访问由于其保护水平意味着您试图访问的东西从外面看,不是公共线和修饰符更改为“公开” 顺便说一句。这是一个古典rtfm-question | |问谷歌
原文:

inaccessible due to its protection Level means you try to access something from the outside that isnt public look for the line and Change the modifier to "public"

btw. this is a classical rtfm-question || ask google