.NET MAUI Windows平台网络图片无法加载问题求助
MAUI Windows应用网络图片无法加载问题解决
问题现象
- 应用基于Visual Studio 2022的MAUI项目模板生成
- 仅新增一个Image控件,其Source属性设置为
https://picsum.photos/seed/picsum/200 - 图片无法加载,仅显示设置的灰色背景
原因分析
Windows平台的MAUI应用默认未授予网络访问权限,导致无法加载外部网络资源。
解决步骤
在Package.appmanifest的<Capabilities>节点中添加网络访问权限声明:
<uap:Capability Name="internetClient" />
修改后的Package.appmanifest代码
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap rescap"> <Identity Name="maui-package-name-placeholder" Publisher="CN=User Name" Version="0.0.0.0" /> <mp:PhoneIdentity PhoneProductId="####hiddenbyme####" PhonePublisherId="####hiddenbyme####"/> <Properties> <DisplayName>$placeholder$</DisplayName> <PublisherDisplayName>User Name</PublisherDisplayName> <Logo>$placeholder$.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" /> <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" /> </Dependencies> <Resources> <Resource Language="x-generate" /> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$"> <uap:VisualElements DisplayName="$placeholder$" Description="$placeholder$" Square150x150Logo="$placeholder$.png" Square44x44Logo="$placeholder$.png" BackgroundColor="transparent"> <uap:DefaultTile Square71x71Logo="$placeholder$.png" Wide310x150Logo="$placeholder$.png" Square310x310Logo="$placeholder$.png" /> <uap:SplashScreen Image="$placeholder$.png" /> </uap:VisualElements> </Application> </Applications> <Capabilities> <rescap:Capability Name="runFullTrust" /> <!-- 添加网络访问权限 --> <uap:Capability Name="internetClient" /> </Capabilities> </Package>
其他相关代码参考
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TryingMauiNative.MainPage"> <ScrollView> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <Image Source="dotnet_bot.png" SemanticProperties.Description="Cute dot net bot waving hi to you!" HeightRequest="200" HorizontalOptions="Center" /> <Label Text="Hello, World!" SemanticProperties.HeadingLevel="Level1" FontSize="32" HorizontalOptions="Center" /> <Label Text="Welcome to .NET Multi-platform App UI" SemanticProperties.HeadingLevel="Level2" SemanticProperties.Description="Welcome to dot net Multi platform App U I" FontSize="18" HorizontalOptions="Center" /> <Button x:Name="CounterBtn" Text="Click me" SemanticProperties.Hint="Counts the number of times you click" Clicked="OnCounterClicked" HorizontalOptions="Center" /> <Image Source="https://picsum.photos/seed/picsum/200" BackgroundColor="#535353" HeightRequest="200" WidthRequest="200" /> </VerticalStackLayout> </ScrollView> </ContentPage>
app.manifest
<?xml version="1.0" encoding="utf-8"?> <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity version="1.0.0.0" name="TryingMauiNative.WinUI.app"/> <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <!-- 以下两个标签组合效果:1) Windows 10周年更新及以上版本支持每显示器DPI感知 2) Windows 10周年更新以下版本使用系统DPI感知 --> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware> <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness> </windowsSettings> </application> </assembly>
App.xaml
<maui:MauiWinUIApplication x:Class="TryingMauiNative.WinUI.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:maui="using:Microsoft.Maui" xmlns:local="using:TryingMauiNative.WinUI"> </maui:MauiWinUIApplication>
内容的提问来源于stack exchange,提问作者amitdigga
相关产品推荐
相关产品推荐

