【发布时间】:2018-10-20 03:53:15
【问题描述】:
我是第一次开发 UWP 应用。我一直使用 Windows Forms 和 WPF,但这些应用程序的潜在设计是华丽的,但问题是它们是一种沙盒,尤其是对于我当前的项目。基本上,我正在制作一个游戏启动器,您可以在其中添加游戏,程序会创建一个游戏列表。 (可以将它想象成适用于具有现代 Windows 10 Material 设计的一切的 Steam)。
现在,我几乎完成了所有事情,但只有两件事。首先,也是最重要的,是找到通往游戏的道路。我创建了一个文件选择器,我可以选择 .exe 文件,我可以获取它们的名称,但是 file.Path 属性不起作用,它返回一个空路径。我以为这可能是因为 UWP 的沙盒设计,但我深陷其中,我真的不想放弃它。
第二个问题是这些文件的启动。我设法使用 Steam 的特定 URI 在 Steam 游戏上做到这一点。 ("steam://rungameid/xxxxxx" 其中 xxxxxx 是游戏 id) 但是如果我只是将路径作为 URI 就行不通了。
你怎么看?有什么解决办法吗?让我给你看一些相关的代码:
public async void SelectEXE()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".exe");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
stuffWrite[0] = file.Name;
stuffWrite[1] = file.Path;
count++;
WriteCount();
WriteItem(count, string.Join(",", stuffWrite));
UpdateList();
}
}
async void LaunchGame(string uriToLaunch)
{
Uri uri = new Uri(uriToLaunch);
games_list.Items.Add(uriToLaunch);
uri = new Uri("steam://rungameid/730"); //replace with actual path
await Windows.System.Launcher.LaunchUriAsync(uri);
}
【问题讨论】:
-
我以前也从未使用过 UWP,但我刚刚尝试了您的代码,
file.Path很好地返回了文件的完整路径。旁注:you should avoid usingasync void. -
但是,我刚刚检查了the docs,它说:“不要依赖此属性来访问文件,因为某些文件可能没有文件系统路径。例如如果文件由 URI 支持,或者是使用文件选择器选择的,则不能保证该文件具有文件系统路径。” 但它们没有引用任何替代方法,因此我不知道。也许有 UWP 经验的人可以帮助你。祝你好运!
-
谢谢!是的,我不知道,它的行为很奇怪。
标签: c# .net uwp storagefile