12

選択したアイテムに基づいて画像を表示するWinRTMetroプロジェクトがあります。ただし、選択した画像の一部は存在しません。私がやりたいのは、それらが存在しない場合をトラップして、代替案を表示することです。

これまでの私のコードは次のとおりです。

internal string GetMyImage(string imageDescription)
{
    string myImage = string.Format("Assets/MyImages/{0}.jpg", imageDescription.Replace(" ", ""));

    // Need to check here if the above asset actually exists

    return myImage;
}

呼び出し例:

GetMyImage("First Picture");
GetMyImage("Second Picture");

存在Assets/MyImages/SecondPicture.jpgしますが、存在Assets/MyImages/FirstPicture.jpgしません。

最初は、WinRTに相当するを使用することを考えましFile.Exists()たが、存在しないようです。ファイルを開こうとしてエラーをキャッチする必要はありませんが、ファイルが存在するか、プロジェクトにファイルが存在するかを簡単に確認できますか?

4

4 に答える 4

15

ここGetFilesAsyncから、既存のファイルを列挙するために使用できます。存在しない可能性のある複数のファイルがあることを考えると、これは理にかなっているようです。

現在のフォルダーとそのサブフォルダー内のすべてのファイルのリストを取得します。ファイルは、指定されたCommonFileQueryに基づいてフィルタリングおよびソートされます。

var folder = await StorageFolder.GetFolderFromPathAsync("Assets/MyImages/");
var files = await folder.GetFilesAsync(CommonFileQuery.OrderByName);
var file = files.FirstOrDefault(x => x.Name == "fileName");
if (file != null)
{
    //do stuff
}

編集:

@Filip Skakunが指摘したように、リソースマネージャーには、呼び出し可能なリソースマッピングがありContainsKey、適格なリソース(つまり、ローカライズ、スケーリングなど)もチェックできるという利点があります。

編集2:

Windows 8.1では、ファイルとフォルダーを取得するための新しい方法が導入されました。

var result = await ApplicationData.Current.LocalFolder.TryGetItemAsync("fileName") as IStorageFile;
if (result != null)
    //file exists
else
    //file doesn't exist
于 2012-08-25T13:03:58.093 に答える
6

あなたがそれを扱うことができる2つの方法があります。

1)ファイルを取得しようとしたときにFileNotFoundExceptionをキャッチします。

 Windows.Storage.StorageFolder installedLocation = 
     Windows.ApplicationModel.Package.Current.InstalledLocation;
 try
 {
     // Don't forget to decorate your method or event with async when using await
     var file = await installedLocation.GetFileAsync(fileName);
     // Exception wasn't raised, therefore the file exists
     System.Diagnostics.Debug.WriteLine("We have the file!");
 }
 catch (System.IO.FileNotFoundException fileNotFoundEx)
 {
     System.Diagnostics.Debug.WriteLine("File doesn't exist. Use default.");
 }
 catch (Exception ex)
 {
     // Handle unknown error
 }

2)mydogisboxが推奨するように、LINQを使用します。私がテストした方法は少し異なりますが:

 Windows.Storage.StorageFolder installedLocation =
     Windows.ApplicationModel.Package.Current.InstalledLocation;
 var files = await installedLocation.GetFilesAsync(CommonFileQuery.OrderByName);
 var file = files.FirstOrDefault(x => x.Name == fileName);
 if (file != null)
 {
    System.Diagnostics.Debug.WriteLine("We have the file!");
 }
 else
 {
    System.Diagnostics.Debug.WriteLine("No File. Use default.");
 }
于 2012-08-27T01:31:24.040 に答える
2

BitmapImageImageFailed画像を読み込めない場合に発生するイベントがあります。これにより、元の画像を読み込んで、そこにない場合は反応することができます。

もちろん、これには、をBitmapImage構築するだけでなく、自分自身をインスタンス化する必要がありますUri

于 2012-08-25T14:03:00.193 に答える
0

c ++ / cxのリソース可用性のサンプルチェック(Windows Phone 8.1でテスト済み):

std::wstring resPath = L"Img/my.bmp";
std::wstring resKey = L"Files/" + resPath;
bool exists = Windows::ApplicationModel::Resources::Core::ResourceManager::Current->MainResourceMap->HasKey(ref new Platform::String(resKey.c_str()));
于 2015-09-08T16:40:19.293 に答える