c# - .Net Maui Slim Bindings (Native Library Interop) - Stack Overflow

admin2025-04-29  2

I'm an experienced WPF developer trying to break into mobile development through .Net Maui.

I spent the whole day trying to understand how to build the simple sample from the Maui.NativeLibraryInterop repository.

I'm struggling to understand a problem that seems to be quite simple: how to use the 'using' directives properly.

I found this repository, this blog post and this shallow youtube content, that all try to give an intro to the topic, but that unfortunately doesn't even touch on what seems to be the most trivial starting point: how to compile this thing and make it work!

The solution is composed by 3 projects: the Maui one and two binding templates, one for Android and another for iOS/Mac.

I started with the Android one... It all suggests that the native java project should just be compiled using Android Studio and, voila, evertything should work fine and I'd be able to use such 'slim bindings' in the class below.

    namespace MauiSample;
    
    #if IOS || MACCATALYST
    using NewBinding = NewBindingMaciOS.DotnetNewBinding;
    #elif ANDROID
    using NewBinding = NewBindingAndroid.DotnetNewBinding;
    #elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID)
    using NewBinding = System.Object;
    #endif
    
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            // Call the native binding, which will append a platform specific string to the input string
            var labelText = NewBinding.GetString("Community Toolkit");
    
            newBindingSampleLabel.Text = "Hello, " + labelText;
        }
    
        async void OnDocsButtonClicked(object sender, EventArgs e)
        {
            try
            {
                Uri uri = new Uri(";);
                await Browser.Default.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
            }
            catch (Exception ex)
            {
                throw new Exception("Browser failed to launch", ex);
            }
        }
    }

However, I couldn't find any means of using 'using' directives with no '.cs' file anywhere else in the code, as per the official github repository sample.

    #if IOS || MACCATALYST
    using NewBinding = NewBindingMaciOS.DotnetNewBinding;
    #elif ANDROID
    using NewBinding = NewBindingAndroid.DotnetNewBinding;
    #elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID)
    using NewBinding = System.Object;
    #endif

Where is this 'NewBindingMaciOS.DotnetNewBinding' and 'NewBindingAndroid.DotnetNewBinding' coming from? Well, as that seemed to be an error, I tried to replace it as follows, with no success though...

    #if IOS || MACCATALYST
    using NewBinding = NewBinding.Android.Binding;
    #elif ANDROID
    using NewBinding = NewBinding.MaciOS.Binding;
    #elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID)
    using NewBinding = System.Object;
    #endif

Perhaps I'm missing something very, very basic, that is preventing me to move forward. I'd appreciate of anyone could assist.

I'm an experienced WPF developer trying to break into mobile development through .Net Maui.

I spent the whole day trying to understand how to build the simple sample from the Maui.NativeLibraryInterop repository.

I'm struggling to understand a problem that seems to be quite simple: how to use the 'using' directives properly.

I found this repository, this blog post and this shallow youtube content, that all try to give an intro to the topic, but that unfortunately doesn't even touch on what seems to be the most trivial starting point: how to compile this thing and make it work!

The solution is composed by 3 projects: the Maui one and two binding templates, one for Android and another for iOS/Mac.

I started with the Android one... It all suggests that the native java project should just be compiled using Android Studio and, voila, evertything should work fine and I'd be able to use such 'slim bindings' in the class below.

    namespace MauiSample;
    
    #if IOS || MACCATALYST
    using NewBinding = NewBindingMaciOS.DotnetNewBinding;
    #elif ANDROID
    using NewBinding = NewBindingAndroid.DotnetNewBinding;
    #elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID)
    using NewBinding = System.Object;
    #endif
    
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            // Call the native binding, which will append a platform specific string to the input string
            var labelText = NewBinding.GetString("Community Toolkit");
    
            newBindingSampleLabel.Text = "Hello, " + labelText;
        }
    
        async void OnDocsButtonClicked(object sender, EventArgs e)
        {
            try
            {
                Uri uri = new Uri("https://learn.microsoft.com/dotnet/communitytoolkit/maui/native-library-interop/get-started");
                await Browser.Default.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
            }
            catch (Exception ex)
            {
                throw new Exception("Browser failed to launch", ex);
            }
        }
    }

However, I couldn't find any means of using 'using' directives with no '.cs' file anywhere else in the code, as per the official github repository sample.

    #if IOS || MACCATALYST
    using NewBinding = NewBindingMaciOS.DotnetNewBinding;
    #elif ANDROID
    using NewBinding = NewBindingAndroid.DotnetNewBinding;
    #elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID)
    using NewBinding = System.Object;
    #endif

Where is this 'NewBindingMaciOS.DotnetNewBinding' and 'NewBindingAndroid.DotnetNewBinding' coming from? Well, as that seemed to be an error, I tried to replace it as follows, with no success though...

    #if IOS || MACCATALYST
    using NewBinding = NewBinding.Android.Binding;
    #elif ANDROID
    using NewBinding = NewBinding.MaciOS.Binding;
    #elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID)
    using NewBinding = System.Object;
    #endif

Perhaps I'm missing something very, very basic, that is preventing me to move forward. I'd appreciate of anyone could assist.

Share Improve this question edited Jan 11 at 21:20 President James K. Polk 42.1k30 gold badges110 silver badges146 bronze badges asked Jan 6 at 23:04 roccaforteroccaforte 1218 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To use the 'using' directives properly in the .NET MAUI platform, you need to understand how to manage namespaces and platform-specific code. Here are some key points:

Global Using Directives: .NET MAUI includes several implicit global using directives, which means you don't need to add them manually. For example, the Microsoft.Maui.Controls namespace is included by default.

Platform-Specific Code: When writing platform-specific code, you often need to use conditional compilation. This involves adding using directives for platform-specific namespaces within #if directives. For instance, if you are implementing a custom effect, you might need to include platform-specific namespaces like Microsoft.Maui.Controls.Platform.

Conditional Compilation Example: Here is an example of how to use conditional compilation with using directives in a .NET MAUI project:

#if ANDROID
using Android.Content;
using Android.Views;
#elif IOS
using UIKit;
#endif
using Microsoft.Maui.Controls;

Where is this 'NewBindingMaciOS.DotnetNewBinding' and 'NewBindingAndroid.DotnetNewBinding' coming from?

I highly recommend checking out the documentation at Getting started with Native Library Interop which may include some of the explanations you are looking for.

As per the docs, NewBindingAndroid.DotnetNewBinding is located at CommunityToolkit/Maui.NativeLibraryInterop.

转载请注明原文地址:http://anycun.com/QandA/1745940017a91407.html