UWP - 我无法从 UI 线程编辑应用程序 class 上的字段
UWP - I can't edit a field on the Application class from the UI thread
我的 Application
class.
中有 属性 Files
类型 ObservableCollection<Element>
public sealed partial class App : Application
{
public string CurrentPath;
public ObservableCollection<Element> Files;
public async void RefreshFilesAsync()
{
Files.Clear();
var folder = await StorageFolder.GetFolderFromPathAsync(CurrentPath);
var foldersRequest = folder.GetFoldersAsync();
var filesRequest = folder.GetFilesAsync();
var folders = await foldersRequest;
var files = await filesRequest;
foreach (StorageFolder directory in folders)
{
Files.Add(new FolderElement(directory));
}
foreach (StorageFile file in files)
{
Files.Add(new FileElement(file));
}
}
// ...
}
Element
是我的 class 对象的包装器 StorageFile
。这样做 class 以便为名称 属性 设置新值会调用 API 重命名实际文件。
public class Element
{
public string Name
{
get => StorageFile.Name;
set =>
{
StorageFolder.RenameAsync(value, NameCollisionOption.GenerateUniqueName).Completed = (IAsyncAction act, AsyncStatus status) =>
{
(App.Current as App).RefreshFilesAsync();
};
}
}
public StorageFile StorageFile { get; }
//...
}
我有一个 DataGrid
链接到应用程序 属性 Files
,因此您可以推断,每当我重命名文件名的列时,API 是调用以执行真正的重命名。
这是我的问题发生的时间。 API 调用成功并且文件被重命名,但是在 RefreshFilesAsync()
中,当调用 Files.Clear();
时,我得到一个异常 The application called an interface that was marshalled for a different thread
.
我哪里错了?
为了运行代码与UI线程,我使用了Window.Current.Dispatcher.RunAsync()
,并将其放在一个单独的异步方法中。异常不再出现。
public string Name
{
get => StorageFolder.Name;
set => RenameAsync(value);
}
private async void RenameAsync(string newName)
{
await StorageFolder.RenameAsync(newName, NameCollisionOption.GenerateUniqueName);
await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (App.Current as App).RefreshFilesAsync());
}
我的 Application
class.
Files
类型 ObservableCollection<Element>
public sealed partial class App : Application
{
public string CurrentPath;
public ObservableCollection<Element> Files;
public async void RefreshFilesAsync()
{
Files.Clear();
var folder = await StorageFolder.GetFolderFromPathAsync(CurrentPath);
var foldersRequest = folder.GetFoldersAsync();
var filesRequest = folder.GetFilesAsync();
var folders = await foldersRequest;
var files = await filesRequest;
foreach (StorageFolder directory in folders)
{
Files.Add(new FolderElement(directory));
}
foreach (StorageFile file in files)
{
Files.Add(new FileElement(file));
}
}
// ...
}
Element
是我的 class 对象的包装器 StorageFile
。这样做 class 以便为名称 属性 设置新值会调用 API 重命名实际文件。
public class Element
{
public string Name
{
get => StorageFile.Name;
set =>
{
StorageFolder.RenameAsync(value, NameCollisionOption.GenerateUniqueName).Completed = (IAsyncAction act, AsyncStatus status) =>
{
(App.Current as App).RefreshFilesAsync();
};
}
}
public StorageFile StorageFile { get; }
//...
}
我有一个 DataGrid
链接到应用程序 属性 Files
,因此您可以推断,每当我重命名文件名的列时,API 是调用以执行真正的重命名。
这是我的问题发生的时间。 API 调用成功并且文件被重命名,但是在 RefreshFilesAsync()
中,当调用 Files.Clear();
时,我得到一个异常 The application called an interface that was marshalled for a different thread
.
我哪里错了?
为了运行代码与UI线程,我使用了Window.Current.Dispatcher.RunAsync()
,并将其放在一个单独的异步方法中。异常不再出现。
public string Name
{
get => StorageFolder.Name;
set => RenameAsync(value);
}
private async void RenameAsync(string newName)
{
await StorageFolder.RenameAsync(newName, NameCollisionOption.GenerateUniqueName);
await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (App.Current as App).RefreshFilesAsync());
}