简体   繁体   English

如何使用 Visual Studio C# 中其他项目的 class

[英]How to use class from other project in visual studio C#

I have a Xamarin.Forms project and a C# console app project.我有一个 Xamarin.Forms 项目和一个 C# 控制台应用程序项目。 I want to use one class from the console app in my Xamarin.Forms project.我想在我的 Xamarin.Forms 项目的控制台应用程序中使用一个 class。

I added the console app project to the solutions explorer of the Xamarin.Forms project.我将控制台应用程序项目添加到 Xamarin.Forms 项目的解决方案资源管理器中。

Unfortunately, I can't figure out how to use the class from the console app in one of the Xamarin files.不幸的是,我无法弄清楚如何在 Xamarin 文件之一中的控制台应用程序中使用 class。

I always get the error message:我总是收到错误消息:

The name 'MyClass' does not exist in the current context.当前上下文中不存在名称“MyClass”。

I tried to press alt+enter to show potential fixes but it does not offer me the option of importing/using the class.我尝试按 alt+enter 来显示潜在的修复,但它没有为我提供导入/使用 class 的选项。

I also wrote manual using directives in various forms but it still does not seem to make the class accessible.我还在各种 forms 中编写了使用指令的手册,但它似乎仍然无法使 class 可访问。

The only way I was able to use the class was by adding the class directly to the Xamarin project by adding it with add->existing item.我能够使用 class 的唯一方法是将 class 直接添加到 Xamarin 项目中,方法是使用 add->existing item 添加它。 The problem with this is that it imports a copy of the class.问题在于它导入了 class 的副本。 Since I'm still working on the class within the other project the added class is fast outdated and I have to manually copy its contents over.由于我仍在另一个项目中处理 class ,因此添加的 class 很快就过时了,我必须手动复制其内容。

How can I use a class from an external project without making a copy of the file?如何在不复制文件的情况下使用来自外部项目的 class?

Instead of access class from console app(its exe) try creating new reusable library add that class and use in both projects also you can write wrapper class in both projects而不是从控制台应用程序(其 exe)访问 class 尝试创建新的可重用库添加 class 并在两个项目中使用也可以在两个项目中编写包装器 class

Try to add a reference to the second project in your first project.尝试在您的第一个项目中添加对第二个项目的引用。 To do this, right-click on your project, select Add Reference then select the project in your solution.为此,请右键单击您的项目 select Add Reference然后 select 解决方案中的项目。 Once your main project references the second project, then you can access its public types.一旦您的主项目引用了第二个项目,您就可以访问它的公共类型。

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

相关问题 如何在 Visual Studio 中使用 C# 中其他文件中的类? - How to use class from other files in C# with visual studio? 如何在Visual Studio 2010中引用C#类库项目? - How to reference a C# Class Library project in Visual Studio 2010? 如何在Visual Studio 2012 C#项目中使用SQLConfigDataSource - How to Use SQLConfigDataSource in a Visual Studio 2012 C# Project 如何正确使用C#Visual Studio 2010中另一个类中的一个类的方法 - How can I properly use methods from a class in another class in C# Visual Studio 2010 如何从Visual C#中的其他项目插入表格 - how insert form from project other in visual c# 在 Visual Studio 2010/C# 中引用其他项目中定义的类型 - Referring a type defined in other project in Visual Studio 2010/C# 在c#中如何从我的其他项目中调用一个类? - in c# how to call a class from my other project? 如何在“ Visual Studio 2013”​​中创建由其他库项目组成的统一c#库项目 - How to create an unifying c# library project in “visual studio 2013” consisting of other library projects 如何在 Visual Studio 或其他构建工具的一个 C# 项目中生成 dll 和 exe 的混合体? - how to produce a mix of dll and exe in one C# project in visual studio or other build tools? Visual C#Express 2010从Visual Studio导入项目 - Visual C# Express 2010 Importing a project from Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM