2012-08-05 38 views
1

我有2個問題與組合GeckoFX和C#。GeckoFX和C#!單擊按鈕以刪除文件時出錯

1.當我點擊一個按鈕時,我的應用程序將打開一個OpenFileDialog(由C#代碼生成)來更改img標記的src屬性。我使用這個img的context菜單來做到這一點。我的問題是,如果我點擊一次按鈕,打開OpenFileDialog後,我點擊img(沒有contextmenu)OpenFileDialog再次打開。

2,當我選擇新形象這個IMG,我不能刪除舊文件(使用C#代碼) 這裏是我的代碼

[HTML和Javascript代碼]

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('.div_image).bind('contextmenu',function(){ 
     $('#contextmenu_image').css({top: e.pageY+'px',left: e.pageX+'px'}).show(); 
    }); 

}); 
</script> 
<div class="div_image" style="position: absolute; top: '20px;left:'20px;"><img id="img123" class="image" src="" style="width: 100%;height: 100%;"/></div> 
<ul class="contextmenu" id="contextmenu_image" style="width: 100px; display: none;"> 
    <li class="properties">Properties</li> 
    <li class="del">Delete</li> 
    <button id="choose_image">Choose image</button> 
</ul> 

[ C#代碼]

private void ChooseImage() 
    { 
     if (geckoWebBrowser1.Document.ActiveElement.GetAttribute("id") == "choose_image") 
     { 

      OpenFileDialog open = new OpenFileDialog(); 
      open.Filter = 
       "Image (*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG)|*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG|" + 
       "All files (*.*)|*.*"; 
      open.Title = "Choose an image"; 

      DialogResult result = open.ShowDialog(); 
      if (result == System.Windows.Forms.DialogResult.OK) 
      { 
       string srcFile = open.FileName; 
       string fileName = System.IO.Path.GetFileName(srcFile); 
       string fileExtent = System.IO.Path.GetExtension(srcFile); 
       string desDir = Application.StartupPath + "\\test\\images\\"; 
       Random r = new Random(); 
       string newFileName = "i_"; 
       for (int i = 1; i <= 10; i++) 
       { 
        newFileName += Convert.ToString(r.Next(0, 9)); 
       } 
       newFileName += fileExtent; 
       System.IO.File.Copy(srcFile, desDir + newFileName); 
       //Find old image 
       string old_image = geckoWebBrowser1.Document.GetElementById("img123").GetAttribute("src"); 
       geckoWebBrowser1.Document.GetElementById("img123").SetAttribute("src", "images/" + newFileName); 
       if (old_image != "") 

        System.IO.File.Delete(desDir + old_image);//Delete old file,but unable 



      } 
     } 
    } 
private void geckoWebBrowser1_DomClick(object sender, GeckoDomEventArgs e) 
    { 
     ChooseImage(); 
    } 

對不起,因爲我的英語不好

回答

1

爲了您的杉木st問題,我建議改變你點擊事件的方式:

browser.OnBrowserClick + = new System.EventHandler(OnBrowserClick);

然後,你會得到一個說法,告訴你被點擊了什麼:

private void OnBrowserClick(object sender, EventArgs e) 
{ 
    var ge = e as GeckoDomEventArgs; 
    if (ge.Target.ClassName =="choose_image") 
    { 
     //Handle the click... 

關於第二個問題,我想也許在瀏覽器緊緊依靠該文件,但在我的實驗,這不。我建議你要確保該文件是真的有:

var oldPath = Path.Combine(desDir); 
if(File.Exists(oldPath)) 
{ 
    try 
    { 
     File.Delete(oldPath); 
    } 
    catch(Exception error) 
    { 
     //do something about not being able to delete the file yet 
    } 
} 

如果你想看看一些開源代碼做了很多這樣的東西與geckofx,看到我Bloom project,特別EditingView。 cs和Browser.cs。

+0

非常感謝!我試着在函數的底部使用這段代碼 geckoWebBrowser1.Document.ActiveElement.Blur(); 而對於第二個錯誤,我錯誤path.Hum!再次感謝! Iam將嘗試你的代碼! – 2012-08-06 08:43:00