由於開發需求,需要開發一個類似Win圖片瀏覽的工具
當然也涉及到了拖拽打開的需求
按照固有思路:
<Grid x:Name="grid1" AllowDrop="True" Drop="grid1_Drop" DragEnter="grid1_DragEnter">
private void grid1_Drop(object sender, DragEventArgs e) { m_fpath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); string directory = System.IO.Path.GetDirectoryName(m_fpath); imgArray = GetImgCollection(directory); RefreshImage(m_fpath); } private void grid1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effects = DragDropEffects.Link; } else { e.Effects = DragDropEffects.None; } }
但是發現無法觸發 grid1_DragEnter 事件,Google發現這個系統權限有關,Vista后微軟增加了UAC,使得應用無法跨權限操作。
由於我是以管理員身份打開應用的,而我桌面的圖片權限是用戶級的,所以無法拖拽圖片。
解決方案:
用戶權限打開應用,就能很好的支持拖拽
用管理員權限打開,且支持拖拽,我也看到了一些案例,后期更新: