【发布时间】:2015-01-05 09:22:16
【问题描述】:
我正在尝试从 xaml 生成我自己的自定义磁贴。我已经将一些代码从 C++ 转换为 C#,以便将其集成到我的应用程序中。
基本上我有这个方法:
async Task GenerateHighResTileImage(string inputMarkupFilename, string outputImageFilename, Size size)
{
StorageFolder assetsFolder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFile markupStorageFile = await assetsFolder.GetFileAsync(inputMarkupFilename);
string markupContent = await FileIO.ReadTextAsync(markupStorageFile);
Border grid = (Border)XamlReader.Load(markupContent);
await RenderAndSaveToFileAsync(grid, outputImageFilename, (int)size.Width, (int)size.Height);
}
调试器在我尝试加载标记的那一行停止:
“System.Exception”类型的第一次机会异常发生在 BGTask.winmd
附加信息:应用程序调用的接口是 编组为不同的线程。 (来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD))
我的图块很简单,因为我只是在测试:
<Border Height="360" Width="360" Padding="12" BorderBrush="White" BorderThickness="4" CornerRadius="2" Background="#00b2f0" 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">
</Border>
并且它能够成功的将其加载到markupContent字符串中。
C++ 中的代码如下所示
task<void> AppTileUpdater::GenerateHighResTileImageUpdateAsync(String^ inputMarkupFilename, String^ outputImageFilename, Size size)
{
return create_task(Package::Current->InstalledLocation->GetFolderAsync("Assets"))
.then([inputMarkupFilename](StorageFolder^ assetsFolder) ->IAsyncOperation<StorageFile^>^ {
return assetsFolder->GetFileAsync(inputMarkupFilename);
}).then([](StorageFile^ markupStorageFile) ->IAsyncOperation<Platform::String^>^ {
return FileIO::ReadTextAsync(markupStorageFile);
}).then([this, outputImageFilename, size](Platform::String^ markupContent){
Border^ border = (Border^) XamlReader::Load(markupContent);
return RenderAndSaveToFileAsync(border, outputImageFilename, (unsigned int) size.Width, (unsigned int) size.Height);
});
}
我错过了什么吗?
编辑 该方法正在从 Run 方法运行
async public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
taskInstance.Canceled += (s, e) => { };
taskInstance.Progress = 0;
await GenerateHighResTileImage("150x150Tile.xml", "updatedTile.png", new Size(150, 150));
UpdateTile("updatedTile.png");
deferral.Complete();
}
当我在 BackgroundTask 之外运行代码时,它也可以正常工作
【问题讨论】:
-
从哪个方法调用
GenerateHighResTileImage方法?方法GenerateHighResTileImage开头的SyncronizationContext.Current是否不为空?。 -
我不确定,我会添加代码
-
我不熟悉 Windows Phone 线程模型,但无论如何,如果您没有同步上下文,那么您在等待之后的继续将被编组到线程池。在方法开始时检查
SyncronizationContext.Current。
标签: c# c++ xaml windows-phone-8.1