Share via

Conditional if

Eduardo Gomez Romero 1,375 Reputation points
2024-04-05T12:14:10.0566667+00:00

I am modifying my app on the widows side

I have created a handler for SearchBar and Entry

public class BorderlessEntry : Entry {
}
public class BorderlessSearchBar : SearchBar {
}

I put this in App

        Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("BorderlessEntry", (handler, view) => {
            if (view is BorderlessEntry) {
#if ANDROID
        handler.PlatformView.Background = null;
        handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
#elif WINDOWS
                handler.PlatformView.BorderThickness = new Microsoft.UI.Xaml.Thickness(0);
#elif IOS || MACCATALYST
                handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
                handler.PlatformView.Layer.BorderWidth = 0;
                handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#endif
            }
        });
        Microsoft.Maui.Handlers.SearchBarHandler.Mapper.AppendToMapping("BorderlessSearchBar", (handler, view) => {
            if (view is BorderlessSearchBar) {
#if ANDROID
        handler.PlatformView.Background = null;
        handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
#elif WINDOWS
                handler.PlatformView.BorderThickness = new Microsoft.UI.Xaml.Thickness(0);
#elif IOS || MACCATALYST
                handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
                handler.PlatformView.Layer.BorderWidth = 0;
                handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#endif
            }
        });

But I also had to modify the Windows App, and also included some modifications

<maui:MauiWinUIApplication
    x:Class="DemyAI.WinUI.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DemyAI.WinUI"
    xmlns:maui="using:Microsoft.Maui">
    <maui:MauiWinUIApplication.Resources>
        <Thickness x:Key="TextControlBorderThemeThickness">0</Thickness>
        <Thickness x:Key="TextControlBorderThemeThicknessFocused">0</Thickness>
        <DataTemplate x:Key="MauiAppTitleBarTemplate">
            <Grid>
                <Border
                    VerticalAlignment="Stretch"
                    Background="#e8e8e8"
                    Canvas.ZIndex="1">
                    <TextBlock
                        Margin="10,0,0,0"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center"
                        FontWeight="Bold"
                        Text="Demy IA" />
                </Border>
            </Grid>
        </DataTemplate>
    </maui:MauiWinUIApplication.Resources>
</maui:MauiWinUIApplication>

I know that windows works fine, but I want to test this on Android

I also created a behavior for cursors
I only Implamented this on MacOS and Windows

#if MACCATALYST
using DemyAI.Platforms.MacCatalyst;
#elif WINDOWS
using DemyAI.Platforms.Windows;
#endif
namespace DemyAI.Behaviors {
    public class CursorBehavior {
        public static readonly BindableProperty CursorProperty = BindableProperty.CreateAttached("Cursor", typeof(CursorIcon), typeof(CursorBehavior), CursorIcon.Arrow, propertyChanged: CursorChanged);
        private static void CursorChanged(BindableObject bindable, object oldvalue, object newvalue) {
            if (bindable is VisualElement visualElement) {
                visualElement.SetCustomCursor((CursorIcon)newvalue, Application.Current?.MainPage?.Handler?.MauiContext!);
            }
        }
        public static CursorIcon GetCursor(BindableObject view) => (CursorIcon)view.GetValue(CursorProperty);
        public static void SetCursor(BindableObject view, CursorIcon value) => view.SetValue(CursorProperty, value);
    }
}

So if I want to run this is android I will git an error

I am also using this behavior in XAML

So I know I get this errors

User's image

If Iam using the cursor behavior here

  <Label
      Grid.RowSpan="2"
      Grid.Column="1"
      behaviors:CursorBehavior.Cursor="Hand"

If I use android, it will ignore this line?

I dont understand the errors

because

handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
is part of my if

#elif IOS || MACCATALYST
                handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
                handler.PlatformView.Layer.BorderWidth = 0;
                handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;

#endif

Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

Answer accepted by question author

Michael Taylor 61,221 Reputation points
2024-04-05T16:13:11.19+00:00

The compiler says App.xaml.cs at line 46 is in error. Show us that line of code. We have no way of knowing in your larger codebase what that line is.

The other error is in the same boat. In general errors in Error List are not ordered so the later error could be a cascading error from the earlier one. Look in the Output window and see which error occurred first in the compiler. That is the error you should resolve and the later error may clear up on its own.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.