将文件路径转换为URI

时间:2014-12-25 03:41:11

标签: windows-runtime c++-cx

在c ++中使用secondaryTitle时,我必须输入一个指向徽标的URI。如果我尝试将其指向应用程序包之外的任何文件,则URI会失败。我试图让用户使用filepicker

选择文件
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");
                });
            });
        }
    });
}

然后复制该文件并将其保存在app目录中。使用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);
                });

        }
    });

1 个答案:

答案 0 :(得分:3)

您尝试将已挑选的文件复制到应用包位置(InstalledLocation),而不是复制到应用数据文件夹中。包位置是只读的,因此CopyAsync应该失败。使用StorageFolder ^ localFolder = ApplicationData :: Current-&gt; LocalFolder;代替。

此外,您确实需要ms-appdata:/// local中的///,因为它是省略包ID的简写,但您只需要URI中的一个/其他地方。

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