Câu hỏi phỏng vấn Xamarin
Câu hỏi

How To Share Code Between Cross-Platform Applications on Xamarin?

Câu trả lời

To share code between cross-platform applications on Xamarin, there are several strategies you can employ. The main methods include using .NET Standard Libraries, Shared Projects, and Portable Class Libraries (PCLs). Here’s a detailed overview of each approach:

.NET Standard Libraries

Overview

.NET Standard is a specification of .NET APIs that are intended to be available on all .NET implementations. It provides a uniform API for all .NET platforms, including Xamarin, .NET Core, and .NET Framework. This makes it the preferred option for sharing code across different platforms.

Benefits

  • Uniform API: .NET Standard libraries provide a consistent set of APIs across all .NET platforms.
  • Compatibility: .NET Standard 2.0 offers the best compatibility with existing .NET Framework code and is recommended for new applications and porting existing PCLs[1][2][6].
  • Refactoring: Refactoring operations update all affected references, making it easier to maintain the codebase[8].

How to Create

  1. Visual Studio for Mac:

    • Open the Add New Project dialog.
    • Select the .NET Core category and then .NET Standard Library.
    • Choose .NET Standard 2.0 as the target framework.
    • Name the project and click Create[1].
  2. Visual Studio 2017/2019:

    • Create a new project and select Class Library (.NET Standard).
    • Add necessary NuGet packages, such as Xamarin.Forms, to the project[3][14].

Disadvantages

  • No Compiler Directives: Unlike Shared Projects, .NET Standard Libraries do not support compiler directives like #if __IOS__ for platform-specific code[8].

Shared Projects

Overview

Shared Projects allow you to write common code that is referenced by multiple application projects. The code is compiled as part of each referencing project and can include compiler directives to handle platform-specific functionality.

Benefits

  • Platform-Specific Code: You can use compiler directives to include platform-specific code within the shared codebase[8].
  • No Separate Assembly: Shared Projects do not produce a separate compiled output; the code is included in the referencing projects' assemblies[8].

How to Create

  1. Visual Studio:
    • Create a new project and select Shared Project.
    • Add common code files to the Shared Project.
    • Reference the Shared Project from platform-specific projects (e.g., iOS, Android, Windows)[7][8].

Disadvantages

  • No IntelliSense for XAML: Shared Projects do not provide IntelliSense support for XAML files, which can make development more challenging[14].
  • Refactoring Issues: Refactorings that affect code inside inactive compiler directives will not update the code inside those directives[8].

Portable Class Libraries (PCLs)

Overview

PCLs allow you to build and distribute assemblies that contain shared code to run on multiple platforms. However, PCLs are considered deprecated in favor of .NET Standard.

Benefits

  • **Cross-Platf...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What are Triggers? How many types of Triggers are available?

senior

What is MVVM Cross?

junior

Explain Lifecycle methods of Xamarin.Forms app

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào