XamlReader.Load() 编组
XamlReader.Load() Marshalled
我正在尝试从 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++ 以避免 运行ning 内存不足。
我正在尝试从 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++ 以避免 运行ning 内存不足。