【发布时间】:2018-11-10 14:58:34
【问题描述】:
我一直在尝试为我正在制作的 UWP 应用程序实现一项功能。我在我的 XAML 文档中声明了一个 MediaElement,如下所示:
<MediaElement x:Name="_media" AutoPlay="True" IsLooping="True" Margin="0,0,8,227" AreTransportControlsEnabled="true" RenderTransformOrigin="0.499,0.41" >
<MediaElement.RenderTransform>
<CompositeTransform ScaleX="1"/>
</MediaElement.RenderTransform>
</MediaElement>
我需要能够从计算机中的任何位置加载视频,但 UWP 无法访问所有目录。我正在通过一个按钮(下面的代码)加载视频文件。长话短说,我尝试将文件复制到 LocalAppData 文件夹中,因为我知道我有权访问那里的文件。
第一个问题: 我可以按照我在下面的代码中使用它的方式通过 CopyFileA() 复制文件吗?
第二个问题 复制文件后,我需要将它添加到我的应用程序的Assets。如何通过代码做到这一点?
这是我尝试加载文件时运行的代码。
void SDKTemplate::Scenario4_ReproVideo::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
//This lines are here to get the path of the local folder of my app
Windows::Storage::StorageFolder^ f = Windows::Storage::ApplicationData::Current->LocalFolder;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_local_path;
Platform::String^ path_ss = f->Path;
txtBlockOutput->Text = path_ss;
std::string path_local = convert_local_path.to_bytes(path_ss->Data());
FileOpenPicker^ fop = ref new FileOpenPicker();
fop->SuggestedStartLocation = PickerLocationId::Desktop;
fop->FileTypeFilter->Append(".mp4");
fop->FileTypeFilter->Append(".wmv");
create_task(fop->PickSingleFileAsync()).then([this](StorageFile^ file) {
if (file)
{
//We take the path of the file
Platform::String^ path = file->Path;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_path;
std::string sourcePath = convert_path.to_bytes(path->Data());
//We take the name of the file
Platform::String^ name = file->Name;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_name;
std::string name_s = convert_name.to_bytes(name->Data());
//We find out the local path (again) not sure if needed
Platform::String^ path_ss = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_local_path;
std::string path_local = convert_local_path.to_bytes(path_ss->Data());
// Use Path class to manipulate file and directory paths.
std::string sourceFile = sourcePath +"/" + name_s;
std::string destFile = path_local +"/"+ name_s;
if (CopyFileA(sourceFile.c_str(), destFile.c_str(), FALSE)) {
txtBlockOutput->Text = "File is copied into local directory";
}
else { txtBlockOutput->Text = "File didn't copy"; }
_media->Source = ref new Uri(destFile);
}
else
{
txtBlockOutput->Text = "Operation cancelled.";
}
});
【问题讨论】:
-
“我尝试将文件复制到 LocalAppData 文件夹,因为我知道我有权访问那里的文件” - 嗯,如果您无权读取当前位置的文件,那么显然您也无法复制它(复制意味着从一个位置读取文件,然后将其写入其他位置)——这似乎毫无意义。
-
确实毫无意义,但我对此很陌生,所以我尝试了我想出的每一个解决方案。
-
如果您使用
FileOpenPicker,那么您已经可以访问该StorageFile。您不需要复制它,当然也不需要broadFileSystemAccess功能。您是否正在尝试解决其他问题?
标签: c++ visual-studio xaml uwp c++-cx