4

C#/WPF/Surface プログラミングは初めてです。

私は aLibraryStackで aScatterViewItemを使用してい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>

のにaObservableCollectionを設定してライブラリ スタックを埋めます。は、画像へのファイル パスである文字列で構成されます。ItemsSourceLibraryStackObservableCollection

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と、同じ例外がスローされます。

私は何をすべきか?

4

2 に答える 2

0

考えられる説明は 2 つあります。

  1. 別のプロセスがファイルをロックしています (OneDrive はコードを同期するためにロックすることがよくあります)。Process Explorerを使用して、ファイルがロックされているものを確認してください。
  2. コードのどこかでファイルを誤ってロックし、自分自身をブロックしています
于 2014-06-23T18:54:02.550 に答える