2015-01-05 34 views
-3

嗨我對wpf和c#很新。我寫了一個簡單的WPF應用程序,但由於錯誤collection is inaccessible due to its protection level error而無法工作。我想知道我做錯了什麼?集合由於其保護級別錯誤而無法訪問wpf c#

這裏是我的代碼:

這只是一個虛擬的計劃,剩有不少在裏面做這樣創建另一個窗口來添加和編輯收集的項目,但我不能讓過去的這一關保護級別錯誤。任何人都可以幫我修復它嗎?

主窗口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> 

類我用:

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"; 
    } 
} 
} 
+2

當您報告/詢問編譯器錯誤或異常時,請一般性地指出發生錯誤的確切位置,並引用確切的錯誤消息。 –

回答

1

無法進入,由於其保護級別意味着您嘗試訪問從東西非公衆的外部尋找線路並將修改器更改爲「公開」

btw。這是一個經典的rtfm-question ||問谷歌

2

您的類Cars是沒有修飾符,所以這意味着它是private。添加public修飾符。

+0

heh太快了! 我覺得有點尷尬,但非常感謝您的幫助。 – usernike80

相關問題