我正在尝试将应用程序从Windows Phone Silverlight 8.0升级到8.1。当我尝试访问8.1上的“Assets”文件夹时,我遇到了异常。我能够在一个示例项目中重现这一点,该项目在定位8.0时工作正常,但在升级到8.1时会产生异常。
以下是示例应用程序MainPage的XAML:
<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Get Assets" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
这是MainPage代码隐藏:
using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Controls;
using Windows.Storage;
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
try
{
var assetsFolder = await StorageFolder.GetFolderFromPathAsync("Assets");
foreach (var file in await assetsFolder.GetFilesAsync())
{
Debug.WriteLine(file.Name);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}
当此项目定位到8.0时,您应该会在Assets文件夹中看到AlignmentGrid.png和ApplicationIcon.png。在8.1我即使Assets文件夹仍然存在,我只是得到一个例外。有谁知道为什么会发生这种情况以及我应该怎么做才能访问Assets文件夹?
在相关说明中,Windows Phone Univeral应用程序似乎发生了同样的错误。
更新: 一些额外的细节。上面的代码生成一个“System.ArgumentException:Value不在预期的范围内”。另一个开发人员建议尝试/资产,所以我尝试了和\ Assets但是没有工作,我确实得到了不同的例外:
答案 0 :(得分:7)
试试这个
var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assetsFolder = await package.GetFolderAsync("Assets");
foreach (var file in await assetsFolder.GetFilesAsync())
{
Debug.WriteLine(file.Name);
}
确保为要作为“内容”
访问的资源设置“构建操作”