Skip to content

Commit d65886a

Browse files
committed
[UWP]: Don't use AdaptiveImage to render BackgroundImage (#6838)
1 parent 7ce129a commit d65886a

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

source/uwp/Renderer/lib/AdaptiveColumnRenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ namespace AdaptiveCards::Rendering::Uwp
136136
ComPtr<IPanel> rootAsPanel;
137137
RETURN_IF_FAILED(rootElement.As(&rootAsPanel));
138138

139-
XamlHelpers::ApplyBackgroundToRoot(rootAsPanel.Get(), backgroundImage.Get(), renderContext, newRenderArgs.Get());
139+
XamlHelpers::ApplyBackgroundToRoot(rootAsPanel.Get(), backgroundImage.Get(), renderContext);
140140

141141
// get HeightType for column
142142
ABI::AdaptiveCards::ObjectModel::Uwp::HeightType columnHeightType{};

source/uwp/Renderer/lib/AdaptiveContainerRenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ namespace AdaptiveCards::Rendering::Uwp
128128
ComPtr<IPanel> rootAsPanel;
129129
RETURN_IF_FAILED(rootElement.As(&rootAsPanel));
130130

131-
XamlHelpers::ApplyBackgroundToRoot(rootAsPanel.Get(), backgroundImage.Get(), renderContext, newRenderArgs.Get());
131+
XamlHelpers::ApplyBackgroundToRoot(rootAsPanel.Get(), backgroundImage.Get(), renderContext);
132132

133133
// Add rootElement to containerBorder
134134
ComPtr<IUIElement> rootAsUIElement;

source/uwp/Renderer/lib/XamlBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ namespace AdaptiveCards::Rendering::Uwp
261261
RETURN_IF_FAILED(IsBackgroundImageValid(backgroundImage.Get(), &backgroundImageIsValid));
262262
if (backgroundImageIsValid)
263263
{
264-
XamlHelpers::ApplyBackgroundToRoot(rootAsPanel.Get(), backgroundImage.Get(), renderContext, renderArgs);
264+
XamlHelpers::ApplyBackgroundToRoot(rootAsPanel.Get(), backgroundImage.Get(), renderContext);
265265
}
266266

267267
ComPtr<IAdaptiveSpacingConfig> spacingConfig;

source/uwp/Renderer/lib/XamlHelpers.cpp

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -359,44 +359,48 @@ namespace AdaptiveCards::Rendering::Uwp::XamlHelpers
359359
return S_OK;
360360
}
361361

362-
void ApplyBackgroundToRoot(_In_ IPanel* rootPanel,
363-
_In_ IAdaptiveBackgroundImage* backgroundImage,
364-
_In_ IAdaptiveRenderContext* renderContext,
365-
_In_ IAdaptiveRenderArgs* renderArgs)
362+
void CreateBackgroundImage(_In_ IAdaptiveRenderContext* renderContext, _In_ HSTRING url, _Outptr_ IImage** backgroundImage)
366363
{
367-
// In order to reuse the image creation code paths, we simply create an adaptive card
368-
// image element and then build that into xaml and apply to the root.
369-
ComPtr<IAdaptiveImage> adaptiveImage = XamlHelpers::CreateABIClass<IAdaptiveImage>(
370-
HStringReference(RuntimeClass_AdaptiveCards_ObjectModel_Uwp_AdaptiveImage));
364+
ComPtr<IImage> image = XamlHelpers::CreateABIClass<IImage>(HStringReference(RuntimeClass_Windows_UI_Xaml_Controls_Image));
371365

372-
HString url;
373-
THROW_IF_FAILED(backgroundImage->get_Url(url.GetAddressOf()));
374-
THROW_IF_FAILED(adaptiveImage->put_Url(url.Get()));
366+
ComPtr<IDependencyObject> imageAsDependencyObject;
367+
THROW_IF_FAILED(image.As(&imageAsDependencyObject));
375368

376-
ComPtr<IAdaptiveCardElement> adaptiveCardElement;
377-
THROW_IF_FAILED(adaptiveImage.As(&adaptiveCardElement));
369+
ComPtr<IAdaptiveHostConfig> hostConfig;
370+
THROW_IF_FAILED(renderContext->get_HostConfig(&hostConfig));
378371

379-
ComPtr<IAdaptiveElementRendererRegistration> elementRenderers;
380-
THROW_IF_FAILED(renderContext->get_ElementRenderers(&elementRenderers));
372+
ComPtr<IUriRuntimeClass> imageUrl;
373+
GetUrlFromString(hostConfig.Get(), url, imageUrl.GetAddressOf());
381374

382-
ComPtr<IAdaptiveElementRenderer> elementRenderer;
383-
THROW_IF_FAILED(elementRenderers->Get(HStringReference(L"Image").Get(), &elementRenderer));
375+
ComPtr<IBitmapImage> bitmapImage =
376+
XamlHelpers::CreateABIClass<IBitmapImage>(HStringReference(RuntimeClass_Windows_UI_Xaml_Media_Imaging_BitmapImage));
377+
THROW_IF_FAILED(bitmapImage->put_UriSource(imageUrl.Get()));
384378

385-
ComPtr<IUIElement> background;
386-
if (elementRenderer != nullptr)
387-
{
388-
elementRenderer->Render(adaptiveCardElement.Get(), renderContext, renderArgs, &background);
389-
if (background == nullptr)
390-
{
391-
return;
392-
}
393-
}
379+
ComPtr<IImageSource> bitmapImageSource;
380+
THROW_IF_FAILED(bitmapImage.As(&bitmapImageSource));
381+
382+
ComPtr<IUIElement> imageAsUIElement;
383+
THROW_IF_FAILED(image.As(&imageAsUIElement));
384+
THROW_IF_FAILED(image->put_Source(bitmapImageSource.Get()));
385+
THROW_IF_FAILED(image.CopyTo(backgroundImage));
386+
}
387+
388+
void ApplyBackgroundToRoot(_In_ IPanel* rootPanel,
389+
_In_ IAdaptiveBackgroundImage* backgroundImage,
390+
_In_ IAdaptiveRenderContext* renderContext)
391+
{
392+
HString url;
393+
THROW_IF_FAILED(backgroundImage->get_Url(url.GetAddressOf()));
394394

395395
ComPtr<IImage> xamlImage;
396-
THROW_IF_FAILED(background.As(&xamlImage));
396+
CreateBackgroundImage(renderContext, url.Get(), &xamlImage);
397+
if (!xamlImage)
398+
{
399+
return;
400+
}
397401

398-
ABI::AdaptiveCards::ObjectModel::Uwp::BackgroundImageFillMode fillMode;
399-
THROW_IF_FAILED(backgroundImage->get_FillMode(&fillMode));
402+
ComPtr<IUIElement> xamlImageAsUIElement;
403+
THROW_IF_FAILED(xamlImage.As(&xamlImageAsUIElement));
400404

401405
// Creates the background image for all fill modes
402406
ComPtr<TileControl> tileControl;
@@ -413,7 +417,7 @@ namespace AdaptiveCards::Rendering::Uwp::XamlHelpers
413417
THROW_IF_FAILED(rootPanel->QueryInterface(rootElement.GetAddressOf()));
414418
THROW_IF_FAILED(tileControl->put_RootElement(rootElement.Get()));
415419

416-
THROW_IF_FAILED(tileControl->LoadImageBrush(background.Get()));
420+
THROW_IF_FAILED(tileControl->LoadImageBrush(xamlImageAsUIElement.Get()));
417421

418422
ComPtr<IFrameworkElement> backgroundAsFrameworkElement;
419423
THROW_IF_FAILED(tileControl.As(&backgroundAsFrameworkElement));

source/uwp/Renderer/lib/XamlHelpers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ namespace AdaptiveCards::Rendering::Uwp::XamlHelpers
299299

300300
void ApplyBackgroundToRoot(_In_ ABI::Windows::UI::Xaml::Controls::IPanel* rootPanel,
301301
_In_ ABI::AdaptiveCards::ObjectModel::Uwp::IAdaptiveBackgroundImage* backgroundImage,
302-
_In_ ABI::AdaptiveCards::Rendering::Uwp::IAdaptiveRenderContext* renderContext,
303-
_In_ ABI::AdaptiveCards::Rendering::Uwp::IAdaptiveRenderArgs* renderArgs);
302+
_In_ ABI::AdaptiveCards::Rendering::Uwp::IAdaptiveRenderContext* renderContext);
304303

305304
HRESULT AddRenderedControl(_In_ ABI::Windows::UI::Xaml::IUIElement* newControl,
306305
_In_ ABI::AdaptiveCards::ObjectModel::Uwp::IAdaptiveCardElement* element,

0 commit comments

Comments
 (0)