Passing an Action to a Custom Control through Bindable Property in .NET MAUI

The main idea:

I want to pass an Action from a ViewModel to a CustomControl, such that whenever these Actions are invoked from the ViewModel, it automatically invokes a method within the CustomControl.

The detailed idea:

I want to be able to show some messages on command. So I have these Actions on my MainViewModel that are invoked whenever I need them to show.

public class MainViewModel
{
    // assuming these get and set methods are basically normal gets and sets
    public Action ShowErrorMsg
    {
        get => GetValue<Action>();
        set => SetValue(value);
    }

    public Action ShowSuccessMsg
    {
        get => GetValue<Action>();
        set => SetValue(value);
    }

    public void SendMessage()
    {
        // some send message logic here
        // ...

        // invoke the actions based on result
        if (sendSuccess)
        {
            ShowSuccessMsg?.Invoke();
        }
        else ShowErrorMsg?.Invoke();
    }
}

I have a CustomLabel control class that extends from Label, and it sits in the MainView.xaml like this, and I want it to pass in said Actions:

<controls:CustomLabel Text="Error!" TextColor="Red"
                      ShowMessageAction="{Binding ShowErrorMsg}" />

<controls:CustomLabel Text="Success!" TextColor="Green"
                      ShowMessageAction="{Binding ShowSuccessMsg}" />

In the CustomLabel class, a BindableProperty will be assigned with any arbitrary Action and I want it to be invoked whenever the Action in MainViewModel is invoked. From there, an animation is fired, and the text fades out in 8 seconds.

public class CustomLabel : Label
{ 
    public static readonly BindableProperty ShowMessageActionProperty =
       BindableProperty.Create(nameof(ShowMessageAction), 
                               typeof(Action), 
                               typeof(CustomLabel), 
                               propertyChanged: OnShowMessageChanged);

    public Action ShowMessageAction
    {
        get => (Action)GetValue(ShowMessageActionProperty);
        set => SetValue(ShowMessageActionProperty, value);
    }
    
    // this should be fired when there is a new Action binded
    public static void OnShowMessageChanged(BindableObject sender, object oldValue, object newValue)
    {
        var thisLabel = sender as CustomLabel;

        var action = newValue as Action;
       
        // subscribe the animation method to the Action that this class is bound to
        action += thisLabel.ShowMessageAnimation;
    }

    // animation is fired when action is called, fades out in 8 seconds
    private void ShowMessageAnimation()
    {
        this.IsVisible = true;

        Timer timer = new Timer(8000);
        timer.Elapsed += (_, _) =>
        {
            Application.Current.Dispatcher.Dispatch(async () =>
            {
                await this.FadeTo(0, 1000);
                this.IsVisible = false;
                this.Opacity = 1;
            });
        };

        timer.Start();
    }
}

However, unless I explicitly specify the Action from within CustomLabel, the Action from MainViewModel and ShowMessageAction don’t sync as it should (sometimes the propertyChanged isn’t even fired at all). Is there a fundamental gap in my knowledge, or is there a workaround/ a better way to approach this that I am completely missing?

What I have tried:

  • I have tried assigning ShowMessageAction with MainViewModel.ShowMessage in the constructor and that works fine, but does not allow me to be flexible on which Action I want to pass in.
  • I have tried subscribing within the Set method in ShowMessageAction property, manually subscribing from MainView.xaml.cs, to no avail.

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật