我是 C#/WPF/Surface 编程的新手。
我正在使用 a LibraryStack
in ScatterViewItem
a ScatterView
:
<Grid Name="DataGrid" Background="LightBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.Resources>
<DataTemplate x:Key="LibraryItemTemplate">
<Viewbox Stretch="Uniform">
<Image Source="{Binding}" />
</Viewbox>
</DataTemplate>
<!-- Styles to ensure each library control uses the above defined templates -->
<Style TargetType="{x:Type s:LibraryStack}">
<Setter Property="ItemTemplate" Value="{StaticResource LibraryItemTemplate}"/>
</Style>
<Style TargetType="{x:Type s:LibraryBar}">
<Setter Property="ItemTemplate" Value="{StaticResource LibraryItemTemplate}"/>
</Style>
<DataTemplate x:Key="itemTemplate">
<Image Source="{Binding XPath=@FullPath}"/>
</DataTemplate>
</Grid.Resources>
<s:ScatterView HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<s:ScatterViewItem Name="ScatterViewItem1" Background="DarkGray" MinWidth="800" MinHeight="800"
Orientation="0.0" CanRotate="False">
<s:LibraryStack Name="LibraryStack1" Background="Transparent" MinWidth="800" MinHeight="800" AllowDrop="True" >
</s:LibraryStack>
</s:ScatterViewItem>
</s:ScatterView>
</Grid>
我通过将 a设置ObservableCollection
为. 由字符串组成,这些字符串是图像的文件路径。ItemsSource
LibraryStack
ObservableCollection
ObservableCollection<string> oc = new ObservableCollection<string>(System.IO.Directory.GetFiles(folder));
LibraryStack1.ItemsSource = ocs;
现在我有了一个ScatterViewItem
包含所有图像的拖放功能。
然后我想清除LibraryStack
/中的所有图像ScatterViewItem
并删除文件夹中的所有文件/图像:
oc=null;
LibraryStack1.ItemsSource = null;
string[] files = Directory.GetFiles(folder);
foreach (String file in files)
{
try
{
File.Delete(file);
}
catch (Exception f)
{
Console.WriteLine(f);
}
}
屏幕上的 ScatterViewItem 是空的,但通过删除文件(File.Delete(file)
)总是抛出异常:
System.IO.IOException:该进程无法访问文件“xyz”,因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.File.Delete(String path) ...
删除FileInfo
会引发相同的异常。
我应该怎么办?