gpt4 book ai didi

c# - XamlReader.Load() 编码

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:59 26 4
gpt4 key购买 nike

我正在尝试从 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);
}

调试器停在我试图加载标记的行上说:

A first chance exception of type 'System.Exception' occurred in BGTask.winmd

Additional information: The application called an interface that was marshalled for a different thread. (Exception from 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 之外运行时代码也能正常工作

最佳答案

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

关于c# - XamlReader.Load() 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com