微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

例外:无法加载文件或程序集 XXXX 或其依赖项之一系统找不到指定的文件

如何解决例外:无法加载文件或程序集 XXXX 或其依赖项之一系统找不到指定的文件

我有一个 C# 解决方案,其中包含 3 个 C# .Net 框架 4.7.2 项目,其中一个简单的类库、一个 Windows 窗体控件库和一个 Windows 窗体测试应用程序。我创建了一个控件,它调用公共类“ColorHelper”中的静态函数,用于根据控件的前景色检索辅助混合颜色。当此函数与控件在同一个项目中时,这在设计器和运行时都可以正常工作,但在放置在第三个项目(即类库)中时会引发此异常。我已经引用了这个 3r 项目并且还包含了 using 语句。语法看起来不错,编译没有错误,但是当我想将控件放在测试表单上时,会抛出异常。

using System;
using System.Drawing;

namespace ASD.Drawing.Helpers
{
    public class ColorHelper : object
    {

        public static double BlendColor(double foreColor,double backColor,double alpha)
        {
            return Math.Min(Math.Max(backColor + alpha * (foreColor - backColor),0.0D),255.0D);
        }

        /// <summary>
        /// Adjust the color by lighten or darken the color
        /// </summary>
        /// <param name="color">The base color</param>
        /// <param name="gradientPercentage">The percentage of gradient. Negative to darken the color and negative to lighten the color.</param>
        /// <returns></returns>
        public static Color GradientColor(Color color,int gradientPercentage)
        {
            if (gradientPercentage == 100) return color;

            //if positive then blend with white else blend with black
            float backColor = gradientPercentage > 0 ? 255.0F : 0.0F;

            // 0 = transparent foreColor; 1 = opaque foreColor
            double alpha = 1.0F - Math.Abs(Math.Max(Math.Min(gradientPercentage,100),-100)) / 100.0;

            byte r = (byte)BlendColor(color.R,backColor,alpha);
            byte g = (byte)BlendColor(color.G,alpha);
            byte b = (byte)BlendColor(color.B,alpha);

            return Color.FromArgb(color.A,r,g,b);
        }
    };
}

函数是从 Windows 窗体控件库调用

using ASD.Drawing.Helpers;
using ASD.Forms.Controls;
using System;
using System.Drawing;
using System.Drawing.drawing2d;

namespace ASD.Forms.Renderers
{
    /// <summary>
    /// Base class for the button renderers
    /// </summary>
    public class ButtonRenderer : BaseRenderer
    {
        ...

        /// <summary>
        /// Draw the body of the control
        /// </summary>
        /// <param name="Gr"></param>
        /// <param name="rc"></param>
        /// <returns></returns>
        public virtual bool DrawBody(Graphics Gr,RectangleF rc)
        {
            if (Button == null)
                return false;

            Color bodyColor = Button.ButtonColor;
            Color cDark = ColorHelper.GradientColor(bodyColor,-80);
            ...
        }
        ...
    }
}

自从我编程以来已经有很长时间了(大约 5 年)我已经忘记了很多,我真的很茫然。有人可以帮我吗?

解决方法

首先,您在 ASD.Forms.UITest 项目中对 ASD.Forms 的引用已损坏。删除它,编译 ASD.Forms 和 ASD.Drawing 项目并再次添加 ASD.Forms 对 ASD.Forms.UITest 的引用,然后确保将 ASD.Forms.UITest 项目设置为启动项目(右键单击项目 > 设置为启动项目)为什么启动项目必须有一个启动文件(例如一个 .exe)。之后,它应该可以正常工作了:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。