我正在使用import { Component, OnInit, Inject } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from 'src/app/services/auth.service';
import { WINDOW } from 'src/app/services/window.service';
import { environment } from '../../../environments/environment';
@Component({
selector: 'app-invoice-detail',
templateUrl: './invoice-detail.component.html',
styleUrls: ['./invoice-detail.component.css']
})
export class InvoiceDetailComponent implements OnInit {
_id: Number;
singleInvoiceDetail: any;
siteUrl: String;
userDetail: any;
totalAmount : number;
rzp1: any;
title = 'app';
options : any;
clientKey: String;
paymentId: Number = 11;
constructor(
private activatedRoute: ActivatedRoute,
private auth: AuthService, private router: Router,
@Inject(WINDOW)private window: Window) {
activatedRoute.data.subscribe((data: any)=> {
if(data.profileDetails.status==="success") {
this.userDetail = data.profileDetails.data;
}
})
}
ngOnInit() {
this.activatedRoute.paramMap.subscribe(params=>{
this._id = parseInt(params.get('id'));
});
this.activatedRoute.data.subscribe((res: any)=> {
if(res.invoiceDetails.status===200) {
this.singleInvoiceDetail = res.invoiceDetails.body.data.find((invoice: any) => invoice.id === this._id);
this.auth.razorPayClientKey().subscribe((res: any)=>{
this.clientKey = res.data.key;
this.loadRazorPay();
});
}
}, (err: any)=>{
alert("Error fetiching invoice details");
})
}
loadRazorPay(){
this.options = {
'key': this.clientKey,
'amount': this.singleInvoiceDetail.total_amount*100,//'2000', // 2000 paise = INR 20
'name': 'KudosHub',
'description': 'payment',
'image': 'theme/images/logo.svg',
'handler': function(response: any) {
alert(response.razorpay_payment_id);
alert(this.paymentId);
this.success();
},
'prefill': {
'name': this.userDetail.first_name+" "+this.userDetail.last_name,
'email': this.userDetail.email,
},
'notes': {
'address': ''
},
'theme': {
'color': '#F37254'
}
};
}
public initPay(): void {
this.rzp1 = window.Razorpay(this.options);
this.rzp1.open();
}
success() {
console.log("Success");
}
}
方法加载页面的数据,但这会导致在数据不变的情况下加载数据,例如从另一个页面调用PopupAsync()。所以我认为消息传递中心会有所帮助。我在页面上做了一个标记字段,并订阅了任何来自外部的消息,以确定是否更新数据,
例如,来自MenuViewModel的内容(用户首次打开页面时,因此我需要加载数据):
OnAppearing
以及var p = new FeedbackListPage();
MessagingCenter.Send(this, "loadData", "1");
await Navigation.PushAsync(p);
的构造函数中:
FeedbackListPage
和InitializeComponent();
BindingContext = vm = new FeedbackViewModel(Navigation);
MessagingCenter.Subscribe<string>(this, "loadData", ( _loadData) =>
{
loadDataStr = _loadData;
});
中的
OnAppearing
问题在于订阅者的操作从未被调用!
答案 0 :(得分:1)
解决方案:
MessagingCenter的API:
1 。Subscribe<TSender> (object subscriber, string message, Action<TSender> callback, TSender source = null)
2 。Subscribe<TSender, TArgs> (object subscriber, string message,Action<TSender, TArgs> callback, TSender source = null)
因此,如果要使用Argument
传递MessageCenter
,则应同时定义Sender
和Args
:
MessagingCenter.Subscribe<MainPage,string>(this, "loadData", (sender,_loadData) =>
{
loadDataStr = _loadData;
});
答案 1 :(得分:0)
您可以尝试以下操作:
要从任何页面更新数据时,请使用MessagingCenter.Send
发送信号,然后在ViewModel的构造函数中使用MessagingCenter.Subscribe
执行所需的操作
发送:
MessagingCenter.Send<namespace.App>((namespace.App)Xamarin.Forms.Application.Current, "update");
订阅:
MessagingCenter.Subscribe<namespace.App>((namespace.App)Application.Current, "update", (sender) => {
// update - get data
});