我正在尝试从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);
});
}我是不是遗漏了什么?
编辑正在从运行方法运行的方法
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之外运行它时,代码也可以正常工作。
发布于 2015-01-05 01:22:16
如果使用Silverlight,则需要从默认的dispatcherDeployment.Current.Dispatcher.BeginInvoke(...)运行代码。如果您使用的是通用应用程序,则需要从XamlRenderingBackgroundTask派生,但强烈建议您使用C++来避免内存不足。
https://stackoverflow.com/questions/27767115
复制相似问题