简体   繁体   中英

Image in custom control isn't loaded in UWP (WinUI3 preview 3 C++/WinRT)

I was following the Microsoft tutorial on custom controls(templated) in WinUI3, and decided to fiddle with them (I'm new to WinUI). I've been trying to build an Image Card control, but can't seem to get my image to load from the app folder.

Markup from MainPage.xaml (Just the control):

<local:BasicCard ImgSrc="Assets/woah.jpg" Title="Title here" Content="Content Here"/>

The style for the control by default is in Themes/Generic.xaml , here's the relevant markup:

<ImageBrush.ImageSource>
     <BitmapImage UriSource="{Binding Path=ImgSrc, RelativeSource={RelativeSource TemplatedParent}}" />
</ImageBrush.ImageSource>

Now, it seems to work fine if I use a hyperlink for the ImgSrc property , but I am unable to pull it off with a local image file (in the Assets folder). Also, the same thing works fine in a desktop application, but only if I use the absolute path, whereas in the UWP app, nothing works at all(other than the https link). I've placed the woah.jpg file in all the folders and tested patterns like Themes/woah.jpg and just woah.jpg and even absolute paths, nothing works. What am I doing wrong? any help would be appreciated, and please do ask for the full markup snippets if needed.

Finally, I don't know if this matters, but I am currently registering the property as a winrt::hstring like so:

Microsoft::UI::Xaml::DependencyProperty BasicCard::m_ImageSourceProperty =
    Microsoft::UI::Xaml::DependencyProperty::Register(
        L"ImgSrc",
        winrt::xaml_typename<winrt::hstring>(),
        winrt::xaml_typename<TitleBarPlease::BasicCard>(),
        NULL
    );

IInspectable was right in pointing out that the image needs to be a part of the package, weirldy not all the resources in the Assets folder are included by default, right clicking on the image, and selecting "Include in project" solved the issue(VS 2019).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM