Firebase 对消息中心的表单呼叫次数每次都会增加

Firebase 对消息中心的表单呼叫次数每次都会增加,firebase,xamarin.forms,firebase-storage,messagingcenter,Firebase,Xamarin.forms,Firebase Storage,Messagingcenter,我有一个问题,我发送消息一次,订户被调用一次,但下次它被调用两次,以此类推。。。这是我的密码。 我是邮件发送者 public void OnSuccess(Java.Lang.Object result) { UploadTask.TaskSnapshot taskSnapShot = (UploadTask.TaskSnapshot)result; string downloadURL = taskSnapShot.DownloadUrl.ToStri

我有一个问题,我发送消息一次,订户被调用一次,但下次它被调用两次,以此类推。。。这是我的密码。 我是邮件发送者

public void OnSuccess(Java.Lang.Object result)
    {
        UploadTask.TaskSnapshot taskSnapShot = (UploadTask.TaskSnapshot)result;

        string downloadURL = taskSnapShot.DownloadUrl.ToString();
        string fileName = taskSnapShot.Metadata.Name;

        GBPaperReceipt.Model.ImageFile imageFile = new Model.ImageFile
        {
            FileName = fileName,
            FilePath = downloadURL
        };
        MessagingCenter.Send((App)Xamarin.Forms.Application.Current, MessageStrings.ImageUploadEvent, imageFile);

        //save this live storage image url in receipt table

        //MessagingCenter.Send<Xamarin.Forms.Application, string>((Xamarin.Forms.Application)Xamarin.Forms.Application.Current, ChatModuleConstant.UploadMediaEvent, downloadURL);
    }
public void OnSuccess(Java.Lang.Object结果)
{
UploadTask.TaskSnapshot TaskSnapshot=(UploadTask.TaskSnapshot)结果;
字符串downloadURL=taskSnapShot.downloadURL.ToString();
字符串文件名=taskSnapShot.Metadata.Name;
gbPaperReceivement.Model.ImageFile ImageFile=新的Model.ImageFile
{
FileName=FileName,
FilePath=downloadURL
};
MessagingCenter.Send((App)Xamarin.Forms.Application.Current,MessageStrings.ImageUploadEvent,imageFile);
//将此实时存储映像url保存在接收表中
//MessagingCenter.Send((Xamarin.Forms.Application)Xamarin.Forms.Application.Current,ChatModuleConstant.UploadMediaEvent,downloadURL);
}
这是信息接收器

MessagingCenter.Subscribe<App, ImageFile>((App)Application.Current, MessageStrings.ImageUploadEvent,async (a, imageFile) =>
        {
            _viewModel.Receipt.ImagePath = imageFile.FilePath;
            _viewModel.Receipt.ImageName = imageFile.FileName;
            try
            {
                await DependencyService.Get<IReceiptService>().SaveReceipt(_viewModel.Receipt);
            }
            catch (Exception ex)
            {
                await DisplayAlert(
                            "Error!", ex.Message, "OK");
            }


            DependencyService.Get<ICamera>().DeletePhoto(_viewModel._imageToBeDeletedOnSaveCommand);
            Dialogs.HideLoading();
            Application.Current.MainPage = new NavigationPage(new DashboardPage());
        });
MessagingCenter.Subscribe((App)Application.Current,MessageStrings.ImageUploadEvent,async(a,imageFile)=>
{
_viewModel.receive.ImagePath=imageFile.FilePath;
_viewModel.receive.ImageName=imageFile.FileName;
尝试
{
等待DependencyService.Get().SaveReceipt(_viewModel.Receipt);
}
捕获(例外情况除外)
{
等待显示警报(
“错误!”,例如消息“OK”);
}
DependencyService.Get().DeletePhoto(_viewModel._imagestobedeletedonsaveCommand);
Dialogs.HideLoading();
Application.Current.MainPage=新导航页面(新仪表板页面());
});
退订

protected override void OnDisappearing()
    {
        base.OnDisappearing();
        MessagingCenter.Unsubscribe<App, string>((App)Application.Current, MessageStrings.ErrorEvent);
        MessagingCenter.Unsubscribe<App, string>((App)Application.Current, MessageStrings.ImageUploadEvent);
    }
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe((App)Application.Current,MessageStrings.ErrorEvent);
MessagingCenter.Unsubscribe((App)Application.Current,MessageStrings.ImageUploadEvent);
}

尤其是在导航页面中使用您的页面时,每当页面进入视图时,都会添加您的订阅事件。如果您来回导航几次,您对messagingcenter的订阅将被添加多次,从而导致事件双倍触发

最安全的方法是在页面构造函数中订阅,即使在这种情况下,也可能需要先取消订阅,然后再订阅

你的出现/消失方法也可能有效,但是我不完全确定出现/消失方法是否能保证你开火

但是,您也可以尝试将取消订阅移动到base.OnDisappearing()前面,因为您应该在调用基类执行页面内部分解之前取消订阅


如果这不起作用,请在构造函数中订阅

你在哪里调用Subscribe?我在我的页面的on-Emerging功能中调用它。这就是为什么。每次你的页面显示时,你都会再次订阅。我在onDisappearing()处松开IBE,让我编辑帖子。我找到了解决方案。。。我没有用相同的数据类型取消订阅。我找到了解决方案。。。我没有用相同的数据类型取消订阅。