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

本文关键字:path 参数 不能 null | 更新日期: 2023-09-27 18:22:14

我在Visual Studio中收到一个错误,显示以下

值不能为空,参数名称:路径

关于编译和运行良好的代码。除了以下代码之外,我似乎无法追踪是什么导致了错误

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);
        }
    }
}

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

Visual Studio中的设计器不适用于此用户控件,但在运行时一切正常。这是Visual Studio中的错误吗?

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

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

在这里,只处理TODO;)