Reputation: 2353
I am getting an error in Visual Studio that says the following
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
<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>
/// <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.
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?
Upvotes: 1
Views: 12134
Reputation: 1978
// TODO Handle an empty string, populate listview with disk drives
DirectoryInfo dirInfo = new DirectoryInfo(filepath);
Here, just handle the TODO ;)
Upvotes: 3