值不能为null,参数名称:path

时间:2015-11-16 11:58:17

标签: c# wpf

我在Visual Studio中遇到以下

的错误
  

值不能为null,参数名称:path

关于编译和运行良好的代码。我似乎无法追踪除了以下代码之外产生错误的内容

XAML:

<UserControl x:Class="Project.UI.ViewSessions.FileViewPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <DataGrid Name="Files" ItemsSource="{Binding DirectoryContents}" MouseDoubleClick="Files_MouseDoubleClick"
                  Style="{StaticResource DataGridStyle}" RowStyle="{StaticResource DataGridRowStyle}" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="File" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="20" Height="20" Source="{Binding View}"/>
                                <TextBlock Padding="5,0,0,0" Text="{Binding File}" TextTrimming="CharacterEllipsis" FontSize="12" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Modified" Width="Auto" Binding="{Binding Timestamp}" ElementStyle="{StaticResource trimStyle}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

的.cs:

/// <summary>
/// Interaction logic for FileViewPanel.xaml
/// </summary>
public partial class FileViewPanel : UserControl
{
    string filepath;

    public FileViewPanel()
    {
        filepath = DataPath.SessionPath;

        InitializeComponent();

        populateDirectoryContents(filepath);
    }

    /// <summary>
    /// Get the DirectoryContents collection
    /// </summary>
    public ObservableCollection<FileData> DirectoryContents
    { get { return DataAccess.DirectoryContents; } }

    /// <summary>
    /// Get the FilePath
    /// Set the FilePath and repopulate DirectoryContents
    /// </summary>
    public string FilePath
    {
        get { return filepath; }
        set
        {
            filepath = value;
            populateDirectoryContents(value);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public void Refresh()
    {
        populateDirectoryContents(filepath);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="filepath">
    /// The file path to populate the DirectoryContents listview with
    /// clears the DirectoryContents container and repopulates it
    /// </param>
    private void populateDirectoryContents(string filepath)
    {
        FileData data = new FileData();
        DirectoryContents.Clear();
        // TODO Handle an empty string, populate listview with disk drives
        DirectoryInfo dirInfo = new DirectoryInfo(filepath);

        if(dirInfo == null)
        { return;  }

        DirectoryInfo[] subDirectories = dirInfo.GetDirectories();

        if (subDirectories == null)
        { return; }

        foreach (DirectoryInfo d in subDirectories)
        {
            // Check if the directory is hidden, if it isn't add it to the list
            if ((d.Attributes & FileAttributes.Hidden) == 0)
            {
                data = new FileData();
                data.File = d.Name;
                data.FullPath = d.FullName;
                DateTime time = d.LastWriteTime;
                data.Timestamp = time.ToString();
                data.View = "pack://application:,,,/Project;component/Images/Folder.ico";
                data.Type = "Dir";
                DirectoryContents.Add(data);
            }
        }

        FileInfo[] info = dirInfo.GetFiles("*.acsx");

        if (info == null)
        { return; }

        foreach (FileInfo f in info)
        {
            data = new FileData();
            data.File = System.IO.Path.GetFileNameWithoutExtension(f.Name);
            data.FullPath = f.FullName;
            DateTime time = f.LastWriteTime;
            data.Timestamp = time.ToString();
            data.View = "pack://application:,,,/Project;component/Images/FnetSession16.png";
            data.Type = "File";
            DirectoryContents.Add(data);
        }
    }

    /// <summary>
    /// On DoubleClick of a Directory in the LocalPanel, navigate to that Directory
    /// </summary>
    private void Files_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var data = ((FrameworkElement)e.OriginalSource).DataContext as FileData;
        if (data != null && data.Type == "Dir")
        {
            FilePath = data.FullPath;
        }
        else if (data != null)
        {
            SessionDataImporter.ImportFile(data.FullPath);
        }
    }
}

我在代码中的任何地方都没有任何参数或数据名称路径,并且每个使用此用户控件的类都会生成错误实例。

Visual Studio中的设计器不适用于此用户控件,但一切都在运行时按预期工作。这是Visual Studio中的错误吗?

1 个答案:

答案 0 :(得分:3)

   // TODO Handle an empty string, populate listview with disk drives
    DirectoryInfo dirInfo = new DirectoryInfo(filepath);

在这里,只需处理TODO;)