【发布时间】:2014-07-01 17:42:55
【问题描述】:
我正在尝试重用我的自定义控件,覆盖派生控件中的一些事件处理程序。
代码如下:
public partial class ControlBase : UserControl {
public ControlBase() {
this.InitializeComponent();
//Buttons
PickFileButton.Click += pickFile;
}
protected virtual async void pickFile(object sender, RoutedEventArgs e) {
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
picker.FileTypeFilter.Add(".wmv");
picker.FileTypeFilter.Add(".mp4");
var file = await picker.PickSingleFileAsync();
if (file == null) return;
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
inputFile = file;
InputVideoElement.SetSource(stream, file.ContentType);
}
}
public sealed partial class DerivedControl : ControlBase {
public DerivedControl() {
this.InitializeComponent();
PickFileButton.Click += pickFile;
}
//This handler called twice
protected async override void pickFile(object sender, RoutedEventArgs e) {
base.pickFile(sender, e);
//some other actions
}
当我尝试调试它时,我看到以下内容:
当我单击派生控件上的按钮时,它调用override void pickFile(),它调用基本实现。在基本方法pickFile() 中执行丰富var file = await picker.PickSingleFileAsync();,然后,派生处理程序pickFile() 第二次调用,动作重复直到var file = await picker.PickSingleFileAsync(); 再次,之后我得到System.UnauthorizedAccessException。
与基本控制按钮相同的操作可以正常工作。可能是什么问题?提前致谢
【问题讨论】:
标签: c# asynchronous windows-runtime async-await