我有一个MainPage和MainPageViewModel,在viewmodel上,我具有以下属性,将一个listview绑定到:
public ObservableCollection<TargetDrawingModel> TargetDrawings { get; set; }
在视图模型上,我有以下方法(通过与按钮绑定到的相应命令运行):
private void AddTargetDrawingClick()
{
Dataaccess da = new Dataaccess();
TargetDrawings.Add(da.AddtoTargetDrawingList());
}
最后,Dataaccess类中的AddToTargetDrawingList()方法:
public TargetDrawingModel AddtoTargetDrawingList()
{
TargetDrawingModel output = new TargetDrawingModel();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Select The Target Drawings To Add To Add To The Comparison";
openFileDialog.Filter = "Drawing (*.dwg) | *.dwg";
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == true)
{
foreach (string file in openFileDialog.FileNames)
{
TargetDrawingModel targetDrawing = new TargetDrawingModel();
targetDrawing.DrawingPath = file;
return targetDrawing;
}
}
output.DrawingPath = "nothing added";
return output;
}
该方法可以添加文件,但是如果我的用户未选择图形(取消打开文件对话框),我不想返回任何内容,但是,如果我不这样做提供一个返回值,该方法将不会生成,因为“并非所有代码路径都返回一个值”。这就是为什么我当前返回字符串“ nothing add”,但是显然不会这样做。
我怀疑是因为我正在ObservableCollection<TargetDrawingModel>
上调用Add方法。它要求我退货,但是我不确定该如何做。
如何修改此方法,以允许用户取消选择任何内容?