首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XamlReader.Load()编组

XamlReader.Load()编组
EN

Stack Overflow用户
提问于 2015-01-04 15:35:22
回答 1查看 316关注 0票数 2

我正在尝试从xaml生成我自己的自定义瓷砖。为了将它集成到我的应用程序中,我已经将一些代码从C++转换成了C#。

基本上我有这样的方法:

代码语言:javascript
复制
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))

我的瓷砖很朴素,因为我只是在测试:

代码语言:javascript
复制
<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++中如下所示

代码语言:javascript
复制
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);
    });
}

我是不是遗漏了什么?

编辑正在从运行方法运行的方法

代码语言:javascript
复制
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之外运行它时,代码也可以正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-05 01:22:16

如果使用Silverlight,则需要从默认的dispatcherDeployment.Current.Dispatcher.BeginInvoke(...)运行代码。如果您使用的是通用应用程序,则需要从XamlRenderingBackgroundTask派生,但强烈建议您使用C++来避免内存不足。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27767115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档