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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33734838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:32:25  来源:igfitidea点击:

Value cannot be null, Parameter name: path

c#wpf

提问by JME

I am getting an error in Visual Studio that says the following

我在 Visual Studio 中收到一条错误消息,内容如下

Value cannot be null, Parameter name: path

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

about code that compiles and runs fine. I can't seem to track down what is generating the error other than the following code

关于编译和运行良好的代码。除了以下代码之外,我似乎无法找到导致错误的原因

xaml:

xml:

<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:

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

I don't have any parameters, or datamembers named path anywhere in the code and an instance of the error is generated by every class that uses this user control.

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

The designer in Visual Studio doesn't work for this user control, but everything works as intended at runtime. Is this a bug in Visual Studio?

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

回答by owairc

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

Here, just handle the TODO ;)

在这里,只需处理 TODO ;)