Windows 上的預設映像目錄

Download Sample 下載範例

此 通用 Windows 平台 平臺特定定義將載入映像資產之專案中的目錄。 它會在 XAML 中取用,方法是將 設定 Application.ImageDirectorystring ,代表包含映像資產的項目目錄:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core"
             ...
             windows:Application.ImageDirectory="Assets">
	...
</Application>

或者,您可以使用 Fluent API 從 C# 取用它:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
...
Application.Current.On<Windows>().SetImageDirectory("Assets");

方法Application.On<Windows>會指定這個平臺專用只會在 通用 Windows 平台 上執行。 命名空間 Application.SetImageDirectory 中的 Xamarin.Forms.PlatformConfiguration.WindowsSpecific 方法可用來指定要從中載入映像的項目目錄。 此外, GetImageDirectory 方法可以用來傳回 string ,代表包含應用程式映像資產的項目目錄。

結果是應用程式中使用的所有映像都會從指定的項目目錄載入。