简体   繁体   English

C# - 不能在同一源中的 WPF 项目中使用 .NET 框架项目中的类

[英]C# - Can't use classes from a .NET Framework project in a WPF project in the same Source

UPDATE: I tried working out by making every class in Code public, but it doesn't seem to accept my class hierarchy.更新:我尝试通过将代码中的每个 class 公开,但它似乎不接受我的 class 层次结构。

This is my first post so please bear with me.这是我的第一篇文章,所以请多多包涵。 Besides that, i'm a pretty big noob, so do excuse me if something dumb comes along除此之外,我是一个相当大的菜鸟,所以如果出现一些愚蠢的事情请见谅

I'm currently doing a school project and I have a fully fleshed out .NET Framework project in Visual Studio.我目前正在做一个学校项目,我在 Visual Studio 中有一个完整的 .NET 框架项目。 Now I have to visualize it with a WPF app.现在我必须使用 WPF 应用程序将其可视化。 So I made a new WPF project in the same source.所以我在同一个源中创建了一个新的 WPF 项目。

Let's respectively call them Code and Visualization .我们分别称它们为CodeVisualization

I've given Visualization a project reference to Code and put using Code;我为 Visualization 提供了一个对 Code 的项目引用并using Code; at the top of the XAML.cs I made a button in Visualization and I want it's OnClick event to use在 XAML.cs 的顶部,我在 Visualization 中创建了一个按钮,我希望使用它的OnClick事件

Code.Start();代码.Start();

What somehow seems to sort of work is making every class in Code public , but I don't remember that being a good practice, but do correct me if i'm wrong!不知何故,似乎某种工作是让Code中的每个class public ,但我不记得这是一个好的做法,但如果我错了,请纠正我!

I've put multiple hours into finding a solution with none to be really found before.我花了几个小时来寻找一个以前没有真正找到的解决方案。 Seeing as nobody seems to have posted this question before I must be missing something really simple.在我必须错过一些非常简单的东西之前,似乎没有人发布过这个问题。

Cheers!干杯!

namespace Code
{
    class Program
    {
        public void Start() 
        {
            /// Do something
        }
    }
}

using Code;

namespace Visualization
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            // Use the Start() function from Code
        }
    }
}

To use classes and their functions and properties from another project, you have to declare them as public .要使用另一个项目中的类及其函数和属性,您必须将它们声明为public

namespace Code
{
    public class Program
    {
        public void Start() 
        {
            // Do something
        }
    }
}

Then go to the other project, right click > Add > Reference > select your project containing the code above (assuming you're using Visual Studio IDE).然后 go 到另一个项目,右键单击 > 添加 > 参考 > select 您的项目包含上面的代码(假设您使用的是 Visual Studio IDE)。 After, you can access the public functions and properties:之后,您可以访问公共函数和属性:

using Code;

namespace Visualization
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            Program p = new Program();
            p.Start();
        }
    }
}

If you are worried about security, then ensure that public functions and properties you deem are safe to expose to other projects.如果您担心安全性,请确保您认为可以安全地向其他项目公开的public功能和属性。 For example, what we just did to use the Start function, any other project or some 3rd party program could do also.例如,我们刚刚使用Start function 所做的,任何其他项目或一些 3rd 方程序也可以。 The only difference is that a reference would be made to the project's.dll produced instead of the project itself.唯一的区别是将引用项目的.dll 而不是项目本身。

A basic rule of thumb (at least for me) is that if there are anything I don't want to expose, then don't make them public and have a public function that can be called to perform different actions.一个基本的经验法则(至少对我而言)是,如果有任何我不想公开的内容,那么不要将它们public ,并拥有一个可以调用以执行不同操作的公共 function。 This way I can limit what actions and information can be performed or accessed:这样我可以限制可以执行或访问的操作和信息:

//within some project
namespace Code
{
    public class Program
    {
        // can't be access from another project directly
        private string _privateText { get;set; }

        // can be accessed directly
        public string PublicText { get;set; }

        public void Start() 
        {
            // Do something
        }

        public string getPrivateText() 
        {
            // here you can limit what actions are done and what information to return
            return _privateText;
        }
    }
}

You can then do the following:然后,您可以执行以下操作:

// within another project
using Code;

namespace Visualization
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            Program p = new Program();
            string s1 = p.getPrivateText();
            string s2 = p.PublicText;
            p.Start();
        }
    }
}

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM