【问题标题】:Value cannot be null, Parameter name: path值不能为空,参数名称:路径
【发布时间】:2015-11-16 11:58:16
【问题描述】:

我在 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 中的错误吗?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:
       // TODO Handle an empty string, populate listview with disk drives
        DirectoryInfo dirInfo = new DirectoryInfo(filepath);
    

    在这里,只需处理 TODO ;)

    【讨论】:

    • 那行得通。为什么 TODO 注释会产生这样的错误?
    • @JME TODO 只是一个提醒——它没有任何作用。问题是 filepath 为空。
    • 文件路径在当前迭代中不能为空。关于这个解决方案的奇怪之处在于,我只将 TODO 从评论中删除,问题就解决了。因此我的问题。
    • @JME 你确定你没有做其他事情吗?或者在您上次遇到错误和错误成功之间发生了变化?
    • 非常确定。我可以重新添加 TODO 注释,错误再次出现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多