I am trying to upgrade an app from Windows Phone Silverlight 8.0 to 8.1. I am hitting an exception when I try to access the 'Assets' folder on 8.1. I was able to reproduce this in a sample project which works fine when targeting 8.0, but produces an exception when upgraded to 8.1.

Here is the XAML for the sample app's MainPage:

<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>

And here is the MainPage code-behind:

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);
            }
        }
    }
}

When this project is targeting 8.0 you should see AlignmentGrid.png and ApplicationIcon.png listed in the Assets folder. On 8.1 I just get an exception even though the Assets folder still exists. Does anyone know why this happens and what I should do in order to access the Assets folder?

On a related note the same error seems to occur with a Windows Phone Univeral app.

Update: Some additional detail. The above code generates a "System.ArgumentException: Value does not fall within the expected range." Another developer suggested trying /Assets, so I tried that and \Assets but neither worked, I did get different exceptions though:

  • Assets - System.ArgumentException: Value does not fall within the expected range.
  • /Assets - System.Exception: The specified path is invalid.
  • \Assets - System.UnauthorizedAccessException: Access is denied.
有帮助吗?

解决方案

try this

var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
var assetsFolder = await package.GetFolderAsync("Assets");
foreach (var file in await assetsFolder.GetFilesAsync())
{
    Debug.WriteLine(file.Name);
}

Make sure you set "Build Action" for resources that you want to access as "Content"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top