简体   繁体   English

在 Xamarin Forms 中触发属性更改方法

[英]Fire a method on Property Change in Xamarin Forms

I'm trying to call a method on a property change, This property currently changes when my Fire-base Push notification is received, Now when that happens I want to fire a method to then display a DisplayAlert.我正在尝试调用属性更改的方法,当收到我的 Fire-base 推送通知时,此属性当前会更改,现在当发生这种情况时,我想触发一个方法,然后显示 DisplayAlert。

Why?为什么? Because I essentially have flash sale's that are separated into a category from my API that I can fetch via a name so when I create a notification I pass the title as the name the display alert pops they click okay and it takes it to them to that page and set the name I need to fetch the correct "Flash sale"因为我基本上有闪购,它们从我的 API 中分成一个类别,我可以通过名称获取它们,所以当我创建通知时,我将标题作为名称传递,显示警报会弹出,他们单击确定,然后将其带到那个页面并设置名称我需要获取正确的“闪购”

Atleast this is my solution to this至少这是我对此的解决方案

My attempt so far到目前为止我的尝试

App.cs应用程序


  Myapp.Views.Home mPage;


 CrossFirebasePushNotification.Current.Subscribe("general");
            CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
            {
                System.Diagnostics.Debug.WriteLine($"TOKEN REC: {p.Token}");
            };
            System.Diagnostics.Debug.WriteLine($"TOKEN: {CrossFirebasePushNotification.Current.Token}");

            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {
                try
                {
                    System.Diagnostics.Debug.WriteLine("Received");
                    if (p.Data.ContainsKey("body"))
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            mPage.Message = $"{p.Data["body"]}";
                        });

                    }
                }
                catch (Exception ex)
                {

                }
            };
            CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
            {
                //System.Diagnostics.Debug.WriteLine(p.Identifier);

                System.Diagnostics.Debug.WriteLine("Opened");
                foreach (var data in p.Data)
                {
                    System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
                }

                if (!string.IsNullOrEmpty(p.Identifier))
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {


                    });
                }
                else if (p.Data.ContainsKey("color"))
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        mPage.Navigation.PushAsync(new ContentPage()
                        {
                            BackgroundColor = Color.FromHex($"{p.Data["color"]}")

                        });
                    });

                }
                else if (p.Data.ContainsKey("aps.alert.title"))
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        mPage.TitleMessage = $"{p.Data["aps.alert.title"]}";
                    });

                }
            };

Home

 public string Message
        {
            get
            {

                return Message;
            }
            set
            {
                Message = value;
                RaisePropertyChanged("Message");
            }
        }

        public string TitleMessage
        {
            get
            {

                return TitleMessage;

            }
            set
            {
                TitleMessage = value;
                RaisePropertyChanged("TitleMessage");
            }
        }

        protected void RaisePropertyChanged(string propertyName = "")
        {
            if (PropertyChanged != null)
            {

                App.Current.MainPage.DisplayAlert(TitleMessage, Message, "ok");
            }
        }

"The app just crashes after a while" is probably because you're trying to display the dialog outside of the UI thread. “应用程序只是在一段时间后崩溃”可能是因为您试图在 UI 线程之外显示对话框。

if (PropertyChanged != null)
{
    App.Current.MainPage.DisplayAlert(TitleMessage, Message, "ok");
}

After you have checked for subscribers to the PropertyChanged event, you need to actually invoke the event rather than try to display the alert.在检查了 PropertyChanged 事件的订阅者之后,您需要实际调用事件而不是尝试显示警报。 The event handler of the subscriber can then deal with displaying the alert.订阅者的事件处理程序然后可以处理显示警报。

eg例如

if (PropertyChanged != null)
{
    PropertyChanged.Invoke(this, <your event args>);
}

Which can be shortened using the null conditional operator to:可以使用 null 条件运算符将其缩短为:

PropertyChanged?.Invoke(this, <your event args>);

Your class handling the displaying of the dialog can then subscribe like:然后,处理对话框显示的类可以像这样订阅:

yourInstanceName.PropertyChanged += HandleMyDialog;

If I understand you correctly, you can do the following.如果我理解正确,您可以执行以下操作。 This will run MyMethod if TitleMessage changes:如果 TitleMessage 更改,这将运行 MyMethod:

public string TitleMessage
        {
            get
            {

                return TitleMessage;

            }
            set
            {
                if(TitleMessage != value){
                    TitleMessage = value;
                    RaisePropertyChanged("TitleMessage");
                    MyMethod();
                }

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 xamarin.forms 中的 listview 和数据库 fire base 中删除单元格 - delete cell from listview and database fire base in xamarin forms Firebase 性能为 Xamarin.Forms - Firebase performance for Xamarin.Forms OrderBy 属性不起作用 - Xamarin Firebase - OrderBy property not working - Xamarin Firebase AngularFirestore 更新方法导致 observable 触发两次 - AngularFirestore update method causing observable to fire twice 在 xamarin.forms 中获取 fcm 令牌 - get fcm token in xamarin.forms Xamarin Forms Firebase Auth OAuth sign in for Apple, Microsoft, Twitter, ...? - Xamarin Forms Firebase Auth OAuth sign in for Apple, Microsoft, Twitter, ...? Firebase 实时数据库订阅在 Xamarin Forms 中没有插入/更新时触发 - Firebase Realtime Database subscribe fires Without Insert/Update in Xamarin Forms 在 buttonImg 上动态发送 id 单击以从 fire base xamarin 表单中删除 - Send id dynamically on buttonImg click to delete from fire base xamarin form 来自 xamarin.forms 的 Android 推送通知按钮单击挂起 - Android push notification from xamarin.forms button click hangs Xamarin Forms Android - 使用 MSAL 库进行部署时的身份验证失败 - Xamarin Forms Android - Authentication Failure on Deployment using MSAL Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM