在一个.NET库中混合使用WPF和WinForm [英] Mixing WPF and WinForm in one .NET Library

查看:650
本文介绍了在一个.NET库中混合使用WPF和WinForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案有WPF项目和WinForm项目。

My solution have WPF projects and WinForm projects.

在这两个项目中,我都使用静态类作为常用方法。

In both project, I use a static class which is used as a common method.

大多数方法都是相同的,但有几种方法是不同的,以适应每个框架(wpf,winform)。

Most methods are identical but several methods are different to fit to each framework(wpf,winform).

每当我添加两个框架中使用的新方法时,我必须修改每个方法源代码。

Whenever I add new method used in both framework, I have to modify each source code.

所以我想把静态类作为类库(dll)让我修改它一次并应用所有项目。

So I want to make the static class as a Class Library(dll) for me to modify it just one time and apply all project.

我的时间点是

1。是否可以使用#if?

1. Is it possible to mix WPF and WinForm code in the Class Library using C# directives like #if?

2等C#指令在类库中混合使用WPF和WinForm代码。是否可以将修改后的类库应用于自动引用它的项目?

2. Is it possible to apply modified Class Library to the project which reference it automatically?

推荐答案

您好,

 首先,#if,#else等指令仅在编译而非运行时使用。见下文

 First, the #if, #else, etc. directives are used only at compile not run time. See below

ref:https://docs.microsoft.com/en-us/dotnet/csharp/language- reference / preprocessor-directives /

ref: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/

 

 其次,是的,您可以编写支持WF或WPF应用程序的单个DLL。请参阅下面的简单示例;

 Second, yes, you can code a single DLL that supports WF or WPF applications. See simple example below;

 

  ;以上是一个非常简单的示例,其中WF和WPF可以访问包含

 The above is a very simple example where WF and WPF have access to a DLL that contains

的DLL,该对象类型字符串已声明并已分配为"Hello"。 与在C#代码项目

an object type string declared and assigned "Hello".  Unlike using C++ DLLs in C# code project

中使用C ++ DLL不同,它需要在使用C ++ DLL之前进行特殊编码。

that requires special coding before using the C++ DLL.

 

 在我的示例中,首先创建了DLL。然后在他们的

 In my example, the DLL was created first. Then the a copy was made for WF and WPF in their

各个目录中为WF和WPF制作了一份副本。 所有代码文件都在主解决方案目录下,而每个项目

respective directories.  All the code files are under the main solution directory while each project

都有自己的工作目录。

 

 但是,如果您希望DLL支持特定的WF或WPF功能,那么您将需要

 However, if you want your DLL to support specific WF or WPF functionality then you will need

代码来支持这些差异。大多数DLL都被编码为提供通用例程,无论

code to support those differences. Most DLLs are coded to provide common routines regardless

是否都在调用应用程序。 这就是DLL的重点,以及C#在WF或WPF中是MSIL的事实。

of the calling app.  That is the point for DLLs and the fact that C# is MSIL wether in WF or WPF.

 

 希望这有助于:)

 Hope this Helps :)


这篇关于在一个.NET库中混合使用WPF和WinForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆