在 UWP 中显示来自可选包的图像

Show image from Optional Package in UWP

我尝试了

中的解决方案

但是当我通过以下代码将图像包含在 InitializeComponent 中时:

Uri resourceLocator = new Uri("ms-appx://ad4d8e16-9f9c-458f-ac0f-e74cb99fa10c/HalloPage.xaml");
Application.LoadComponent(this, resourceLocator, ComponentResourceLocation.Nested);

Uri resourceLocatorImg = new Uri("ms-appx://ad4d8e16-9f9c-458f-ac0f-e74cb99fa10c/Assets/image.jpg");
Application.LoadComponent(this, resourceLocatorImg, ComponentResourceLocation.Nested);

当我显示可选包页面时。它崩溃并抛出错误:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
XamlParseException: The text associated with this error code could not be found.
unrecognized input signature

我的代码在 https://github.com/imlinhanchao/ShowPageOfOptionalPackageErrorSample

源自official document关于可选包,当你部署主应用程序项目时,你需要确保两个包是同步的。请参考以下步骤解决。 在主应用程序项目中,导航到项目构建属性和 select 使用 .NET Native 工具链编译 。转到项目调试属性和 select 部署可选包

另外,Application.LoadComponent Method适合加载xaml个文件,不适合加载图片。所以你可以删除加载图片的代码。

更新:

建议在HalloPage.xaml中写图片控件,这样图片就可以随页面一起加载了。如下:

HalloPage.xaml

<Page
   ……>
   <Grid>
        <StackPanel>
            <TextBlock FontSize="72" HorizontalAlignment="Center" VerticalAlignment="Center">Hallo Welt!</TextBlock>
            <Image Source="Assets/image.jpg" Height="600"/>
        </StackPanel>
    </Grid>
</Page>