C# 如果选择nothing,文件选择器c和XAML中出现异常?
我的应用程序有问题,我用“选择”按钮从电脑中选择文件,但若我并没有选择任何文件,我会遇到异常 希望有人能帮忙 谢谢 编辑:带有Try/Catch块的代码C# 如果选择nothing,文件选择器c和XAML中出现异常?,c#,xaml,C#,Xaml,我的应用程序有问题,我用“选择”按钮从电脑中选择文件,但若我并没有选择任何文件,我会遇到异常 希望有人能帮忙 谢谢 编辑:带有Try/Catch块的代码 openPicker.FileTypeFilter.Add(".wmv"); openPicker.FileTypeFilter.Add(".mp4"); openPicker.FileTypeFilter.Add(".wma"); openPicker.FileTypeFilter.Add(".mp3"); StorageFile fi
openPicker.FileTypeFilter.Add(".wmv");
openPicker.FileTypeFilter.Add(".mp4");
openPicker.FileTypeFilter.Add(".wma");
openPicker.FileTypeFilter.Add(".mp3");
StorageFile file = await openPicker.PickSingleFileAsync();
var stream = (dynamic)null;
try {
stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
} catch (Exception ex) {
Frame.Navigate(typeof(music)); // optional to back to choose page
}
在文本中发布实际代码和错误消息,不清楚图片中的文本内容。根据您的标题判断,您知道您可能不会选择任何内容,因此您应该添加一些错误处理MadeYourDay.Windows.exe中发生的“System.NullReferenceException”类型的首次意外异常其他信息:对象引用未设置为对象的实例。这是异常所说的,我使用了try-and-catch,但失败得到了相同的结果exception@downvoter,您想解释一下吗?也许您可以尝试捕获{}而不是使用参数。或者,您可以在try块上添加file变量,甚至在catch块之后添加finally块。你能告诉我你是如何使用try/catch的吗?openPicker.FileTypeFilter.Add.wmv;openPicker.FileTypeFilter.Add.mp4;openPicker.FileTypeFilter.Add.wma;openPicker.FileTypeFilter.Add.mp3;StorageFile file=await openPicker.PickSingleFileAsync;var流=动态完整;请尝试{stream=await file.OpenAsyncWindows.Storage.FileAccessMode.Read;}捕获异常ex{Frame.Navigatetypeofmusic;//返回选择页面可选}音乐是另一个xaml页面,我刚刚重定向到它
var stream;
StorageFile file;
try{
file = await openPicker.PickSingleFileAsync();
stream = await file.OpenAsync(Windows.Storage.FileAcessMode.Read);
}catch(Exception ex){
//log or something
}
//the rest of the code