2014-12-25 170 views
2

在C++中使用secondaryTitle时,必须输入指向徽标的URI。如果我尝试将它指向应用程序包外的任何文件,则URI将失败。我试图让用户使用filepicker选择文件将文件路径转换为URI

void App3::MainPage::FindLogo(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
{ 
    FileOpenPicker^ openPicker = ref new FileOpenPicker(); 
    openPicker->ViewMode = PickerViewMode::Thumbnail; 
    openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary; 
    openPicker->FileTypeFilter->Append(".jpg"); 
    openPicker->FileTypeFilter->Append(".jpeg"); 
    openPicker->FileTypeFilter->Append(".png"); 

    create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) 
    { 
     if (file) 
     { 
      StorageFolder^ folder; 
      auto ur = ref new Uri("ms-appx:///Assets//"); 

      String^ s = Windows::ApplicationModel::Package::Current->InstalledLocation->Path; 


      create_task(StorageFolder::GetFolderFromPathAsync(s)).then([=](StorageFolder^ folder){ 
       create_task(file->CopyAsync(folder, file->Name, NameCollisionOption::ReplaceExisting)).then([this, file](task<StorageFile^> task) 
       { 

        logoFile = ref new Uri("ms-appdata:///local//App3//Assets//StoreLogo.scale-100.png"); 
       }); 
      }); 
     } 
    }); 
} 

然后复制该文件并将其保存在应用程序目录中。使用uri指向新副本时仍然失败。

void App3::MainPage::kk(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
{ 
    text = url->Text->ToString(); 
    ids = id->Text->ToString(); 
    auto test = ref new Windows::UI::StartScreen::SecondaryTile(ids, "hi", text, logoFile, Windows::UI::StartScreen::TileSize::Square150x150); // Breaks right here 

//错误:logofile是0x05fcc1d0

num++; 

    test->RequestCreateAsync(); 



    //auto uri = ref new Windows::Foundation::Uri("http://www.google.com"); 
    //concurrency::task<bool> launchUriOperation(Windows::System::Launcher::LaunchUriAsync(uri)); 
} 

修订

create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) 
    { 
     if (file) 
     { 
      StorageFolder^ folder = ApplicationData::Current->LocalFolder; 
       create_task(file->CopyAsync(folder, file->Name, NameCollisionOption::ReplaceExisting)).then([this, file](task<StorageFile^> task) 
       { 
        String^ path = "ms-appdata:///local/" + file->Name; 
        logoFile = ref new Uri(path); 
       }); 

     } 
    }); 

回答

3

你试图拾取的文件复制到应用程序包的位置(InstalledLocation),而比进入应用程序数据文件夹。包位置是只读的,所以CopyAsync应该失败。使用StorageFolder^localFolder = ApplicationData :: Current-> LocalFolder;代替。

另外,您确实需要ms/appdata:/// local中的///,因为这是省略包id的简写形式,但您只需要URI中的其他位置。

最后,请注意,平铺图片必须是200KB或更小,并且必须是1024x1024或更小,否则它们将不会出现。如果您使用照片图像,请使用JPEG压缩;矢量图像压缩与PNG最好。有关处理此问题的更多信息,请参阅我的免费电子书第16章Programming Windows Store Apps with HTML, CSS, and JavaScript, 2nd Edition,特别是从第887页开始的“基本磁贴更新”和第899页的侧边栏。内容适用于以所有语言编写的应用程序,并且它是免费的书所以没有风险。

+0

我刚试过,它仍然找不到它。我用更新后的代码更新了我的帖子。谢谢。 – user41616

+0

这很可能是文件太大或图片尺寸太大......请参阅我编辑的回复。您可以通过查看本地appdata文件夹并查看在那里复制的内容来验证图像。如果它在任何维度上都超过200K或1024像素,该图块将不会显示它。 –