【问题标题】:GeckoFX and C# ! Error on clicking on a button to delete a fileGeckoFX 和 C# !单击按钮删除文件时出错
【发布时间】:2012-08-06 06:31:55
【问题描述】:

我在将 GeckoFX 和 C# 结合使用时遇到了 2 个问题。

1.当我单击一个按钮时,我的应用程序将打开一个 OpenFileDialog(由 C# 代码生成)来更改 img 标签的 src 属性。我为此 img 使用上下文菜单来做到这一点。我的问题是,如果我单击一次打开 OpenFileDialog 的按钮,然后单击 img(没有上下文菜单),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();
    }

对不起,因为我的英语不好

【问题讨论】:

    标签: c# geckofx


    【解决方案1】:

    对于您的第一个问题,我建议您更改连接点击事件的方式:

    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。

    【讨论】:

    • 非常感谢!我尝试在函数 geckoWebBrowser1.Document.ActiveElement.Blur(); 底部使用此代码;对于第二个错误,我在路径上出错了。嗯!再次感谢!我要试试你的代码!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多