Maui.Switch icon indicating copy to clipboard operation
Maui.Switch copied to clipboard

🐞WinUI Crash

Open Strypper opened this issue 1 year ago • 5 comments

Exception

Microsoft.UI.Xaml.Controls.Frame.NavigationFailed was unhandled.

Implementation

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
    x:Class="MAUIsland.IosSwitchContentView"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:switch="clr-namespace:IeuanWalker.Maui.Switch;assembly=IeuanWalker.Maui.Switch"
    x:Name="root">
    <switch:CustomSwitch
        HeightRequest="40"
        HorizontalKnobMargin="1"
        IsToggled="{Binding IsToggled, Source={x:Reference root}}"
        KnobBackgroundColor="White"
        KnobHeight="36"
        KnobLimit="Boundary"
        KnobWidth="36"
        SemanticProperties.Hint="{Binding AccessibilityHint, Source={x:Reference root}}"
        SwitchPanUpdate="CustomSwitch_SwitchPanUpdate"
        Toggled="CustomSwitch_Toggled"
        ToggledCommand="{Binding ToggledCommand, Source={x:Reference root}}"
        WidthRequest="70">
        <switch:CustomSwitch.StrokeShape>
            <RoundRectangle CornerRadius="20" />
        </switch:CustomSwitch.StrokeShape>
        <switch:CustomSwitch.KnobStrokeShape>
            <RoundRectangle CornerRadius="18" />
        </switch:CustomSwitch.KnobStrokeShape>
        <switch:CustomSwitch.BackgroundContent>
            <Grid
                ColumnDefinitions="35,35"
                HeightRequest="40"
                WidthRequest="70">
                <Label
                    Grid.Column="0"
                    FontAttributes="Bold"
                    FontAutoScalingEnabled="False"
                    FontSize="12"
                    HorizontalOptions="Center"
                    Text=" |"
                    TextColor="White"
                    VerticalTextAlignment="Center" />
                <Label
                    Grid.Column="1"
                    FontAttributes="Bold"
                    FontAutoScalingEnabled="False"
                    FontSize="12"
                    HorizontalOptions="Center"
                    Text="〇"
                    TextColor="#BBBBBB"
                    VerticalTextAlignment="Center" />
            </Grid>
        </switch:CustomSwitch.BackgroundContent>
    </switch:CustomSwitch>
</ContentView>
using IeuanWalker.Maui.Switch;
using IeuanWalker.Maui.Switch.Events;
using IeuanWalker.Maui.Switch.Helpers;

namespace MAUIsland;

public partial class IosSwitchContentView : ContentView
{
    #region [ CTor ]

    public IosSwitchContentView()
    {
        InitializeComponent();
    }
    #endregion

    #region [ Properties ]
    public static readonly BindableProperty IsToggledProperty = BindableProperty.Create(nameof(IsToggled), typeof(bool), typeof(IosSwitchContentView), false, BindingMode.TwoWay);

    public bool IsToggled
    {
        get => (bool)GetValue(IsToggledProperty);
        set => SetValue(IsToggledProperty, value);
    }

    public static readonly BindableProperty AccessibilityHintProperty = BindableProperty.Create(nameof(AccessibilityHint), typeof(string), typeof(IosSwitchContentView), string.Empty);

    public string AccessibilityHint
    {
        get => (string)GetValue(AccessibilityHintProperty);
        set => SetValue(AccessibilityHintProperty, value);
    }

    public static readonly BindableProperty ToggledCommandProperty = BindableProperty.Create(nameof(ToggledCommand), typeof(ICommand), typeof(IosSwitchContentView));

    public ICommand ToggledCommand
    {
        get => (ICommand)GetValue(ToggledCommandProperty);
        set => SetValue(ToggledCommandProperty, value);
    }
    #endregion

    #region [ Event Handlers ]
    public event EventHandler<ToggledEventArgs>? Toggled = null;

    static void CustomSwitch_SwitchPanUpdate(CustomSwitch customSwitch, SwitchPanUpdatedEventArgs e)
    {
        //Color Animation
        Color fromColor = e.IsToggled ? Color.FromArgb("#4ACC64") : Color.FromArgb("#EBECEC");
        Color toColor = e.IsToggled ? Color.FromArgb("#EBECEC") : Color.FromArgb("#4ACC64");

        double t = e.Percentage * 0.01;

        customSwitch.BackgroundColor = ColorAnimationUtil.ColorAnimation(fromColor, toColor, t);
    }

    void CustomSwitch_Toggled(object sender, ToggledEventArgs e)
    {
        Toggled?.Invoke(sender, e);
    }
    #endregion
}

Strypper avatar Jun 30 '23 06:06 Strypper

Reopening, if someone from the community wants to add windows support feel free to create a PR. Only platform code is around accessibility.

Its not something ill be supporting personally, but happy for the community to add support/ maintain it

IeuanWalker avatar Jun 30 '23 12:06 IeuanWalker

It's the same for iOS as well, app crashes. It only works in Android and in that too it does not show Switch. I have used it like this:

<switch:CustomSwitch
        HorizontalOptions="Start"
        IsToggled="{Binding CorrectOnSite}"
        BackgroundColor="{StaticResource MidGray}"
        KnobBackgroundColor="{StaticResource WhiteSmoke}" />

divyesh008 avatar Aug 09 '24 12:08 divyesh008

@divyesh008 can you create a repo, im actively using it in a number of production apps without an issue on iOS

IeuanWalker avatar Aug 09 '24 18:08 IeuanWalker

Yes my bad, it did work in iOS but in Windows its crashing.

divyesh008 avatar Aug 25 '24 06:08 divyesh008

@divyesh008 sorry only iOS and Android is supported

IeuanWalker avatar Aug 25 '24 07:08 IeuanWalker