C# 等待用户单击带有Rg.Plugins.popup的弹出窗口
我显示一个弹出窗口,我必须等待用户的选择,才能将某些内容放入条目控件中,问题是我决定条目中文本内容的if语句与弹出窗口显示的同时执行 我试着让这个方法等待,但不起作用,这就是我所拥有的 Donesn现在不重要了if声明我只是在尝试 这是我的弹出类,我想等到单击其中一个按钮:C# 等待用户单击带有Rg.Plugins.popup的弹出窗口,c#,xamarin,xamarin.forms,async-await,popup,C#,Xamarin,Xamarin.forms,Async Await,Popup,我显示一个弹出窗口,我必须等待用户的选择,才能将某些内容放入条目控件中,问题是我决定条目中文本内容的if语句与弹出窗口显示的同时执行 我试着让这个方法等待,但不起作用,这就是我所拥有的 Donesn现在不重要了if声明我只是在尝试 这是我的弹出类,我想等到单击其中一个按钮: public partial class PopupElegirRFC : PopupPage { string sEmisor = ""; string sReceptor = ""; publ
public partial class PopupElegirRFC : PopupPage
{
string sEmisor = "";
string sReceptor = "";
public PopupElegirRFC (string emisor, string receptor)
{
InitializeComponent ();
lblREmisor.Text = "Emisor: " + emisor;
lblRReceptor.Text = "Receptor: " + receptor;
sEmisor = emisor;
sReceptor = receptor;
}
private void BtnEmisor_Clicked(object sender, EventArgs e)
{
PopupNavigation.Instance.PopAllAsync();
VGlobales.sRFSeleccionado = sEmisor;
}
private void BtnReceptor_Clicked(object sender, EventArgs e)
{
PopupNavigation.Instance.PopAllAsync();
VGlobales.sRFSeleccionado = sReceptor;
}
}
这是我希望等待用户选择的代码:
case 2:
await PopupNavigation.Instance.PushAsync(new Popups.PopupElegirRFC(list[0], list[1]));
if (VGlobales.sRFSeleccionado == list[1])
{
RFCavalidar.Text = list[1];
VGlobales.sRFSeleccionado = "";
}
else
{
RFCavalidar.Text = list[0];
VGlobales.sRFSeleccionado = "";
}
break;
代码会执行,但它会直接转到if,而不是等待用户的选择
我想在弹出窗口中等待,直到其中的两个按钮都被点击。因此,我可以按照jason的建议进行if验证,您可以使用MessagingCenter评估是否单击了哪个按钮 在弹出页面中单击按钮
private void BtnEmisor_Clicked(object sender, EventArgs e)
{
MessagingCenter.Send((App)Application.Current,"BtnEmisor_Clicked");
PopupNavigation.Instance.PopAllAsync();
VGlobales.sRFSeleccionado = sEmisor;
}
在非弹出页面中
MessagingCenter.Subscribe((App)Application.Current, "BtnEmisor_Clicked", (sender) =>
{
// Do task on that button click
});
这是个老问题,但我的回答可以帮助别人。 添加TaskCompletionSource并等待返回 弹出类:
public partial class PopupElegirRFC : PopupPage
{
private TaskCompletionSource<string> taskCompletionSource;
public Task<string> PopupClosedTask { get { return taskCompletionSource.Task; } }
string sEmisor = "";
string sReceptor = "";
public PopupElegirRFC (string emisor, string receptor)
{
InitializeComponent();
lblREmisor.Text = "Emisor: " + emisor;
lblRReceptor.Text = "Receptor: " + receptor;
sEmisor = emisor;
sReceptor = receptor;
}
protected override void OnAppearing ()
{
base.OnAppearing();
taskCompletionSource = new TaskCompletionSource<string>();
}
private void BtnEmisor_Clicked (object sender, EventArgs e)
{
taskCompletionSource.SetResult(sEmisor);
PopupNavigation.Instance.PopAllAsync();
}
private void BtnReceptor_Clicked (object sender, EventArgs e)
{
taskCompletionSource.SetResult(sReceptor);
PopupNavigation.Instance.PopAllAsync();
}
}
这个想法来自下面的链接:
您的弹出页面需要引发事件或通过MessagingCenter传递消息,让呼叫页面知道它已被解除。它工作正常,但代码在弹出后仍会执行部分。您还需要更改以下内容:MessagingCenter.SubscribeApp…您是否在订阅内的弹出窗口后执行了代码?MessagingCenter.SubscribeApp
var popupElegirRFC = new Popups.PopupElegirRFC(list[0], list[1]);
await PopupNavigation.Instance.PushAsync(popupElegirRFC);
string result = await popupElegirRFC.PopupClosedTask;
if (result == list[1])
{
RFCavalidar.Text = list[1];
}
else
{
RFCavalidar.Text = list[0];
}