我正在开始一个新的C#项目,我想知道如何在dll中创建与应用程序环境交互的函数,例如,返回可执行路径的函数或为应用程序设置全屏模式的函数。
我试过这个,但我不知道如何调用函数内的代码:
public class StdMethods
{
public string GetExePath()
{
return System.Reflection.Assembly.GetExecutingAssembly().Location;
}
public void SetFullScreenMode()
{
this.Top = 0;
this.Left = 0;
this.Width = SystemParameters.WorkArea.Width;
this.Height = SystemParameters.WorkArea.Height;
}
}
我已经尝试过使用System.Windows.Window并将一个Window窗口参数传递给第二个函数,但它不起作用,未来有什么想法和建议在我的下一个项目中使用这种函数?非常感谢。
你可以制作如下的静态类和方法。
public static class StdMethods
{
public static string GetExePath()
{
return System.Reflection.Assembly.GetExecutingAssembly().Location;
}
public static void SetFullScreenMode()
{
this.Top = 0;
this.Left = 0;
this.Width = SystemParameters.WorkArea.Width;
this.Height = SystemParameters.WorkArea.Height;
}
}
并像StdMethods.SetFullScreenMode();
一样使用它
无论你称之为public
(以及通常在Visual Studio中命名为“类库项目”)的dll
,对消费者来说确实是可见的。所以在你的情况下public class HelpfulMethodsHolder {}
将工作。
边注。不要这样做。 public class HelpfulMethodsHolder {}
可能看起来和感觉完全错误的设计。弄乱你的架构之前要三思而后行。