在需要返回值的方法中时,"自定义对话框"窗口将被禁用



只有在使用Task.Delay((时,页面才会显示!

注意:更新后提供了更多关于我的代码/调用的信息,我还发现,如果自定义对话框窗口方法在任何有返回方法的地方被调用,那么对话框窗口将被禁用(至少对话框上什么都不起作用(,如果我在void方法中放置相同的调用,我不需要等待任务。延迟,一切都是响应的,例如活动指示器正在旋转等等…

在这里页面被初始化。。。

public void InitLoadingPage(ContentPage loadingPage = null)
{
// check if the page parameter is available
if (loadingPage != null)
{
// build the loading page with native base
loadingPage.Parent = Xamarin.Forms.Application.Current.MainPage;
loadingPage.Layout(new Rectangle(0, 0,
Xamarin.Forms.Application.Current.MainPage.Width,
Xamarin.Forms.Application.Current.MainPage.Height));
var renderer = loadingPage.GetOrCreateRenderer();
_nativeView = renderer.View;
_dialog = new Dialog(CrossCurrentActivity.Current.Activity);
_dialog.RequestWindowFeature((int)WindowFeatures.NoTitle);
_dialog.SetCancelable(true);
_dialog.SetContentView(_nativeView);
Window window = _dialog.Window;
window.SetLayout(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
window.ClearFlags(WindowManagerFlags.DimBehind);
window.SetBackgroundDrawable(new ColorDrawable(Android.Graphics.Color.Transparent));
_isInitialized = true;
}
}

这是显示页面的方法。。

public void ShowLoadingPage()
{
// check if the user has set the page or not
if (!_isInitialized)
InitLoadingPage(new LoadingPageDefault()); // set the default page
// showing the native loading page
_dialog.Show();
}

它被称为…

DependencyService.Get<ILodingPageService>().InitLoadingPage(new NetWorkScanPage());
DependencyService.Get<ILodingPageService>().ShowLoadingPage();
while (BaseObject.IsBusy)
{
await Task.Delay(1000).ConfigureAwait(true);
}
BaseObject.IsBusy = true;

我的问题是…有没有一种方法可以在不使用等待任务的情况下显示窗口。延迟(1000(?

我通过将我调用的方法放在一个不可用的任务中来解决这个问题,我知道这是因为我仍在学习如何正确编码,但无论如何我都会发布我的解决方案,以防像我这样的人有同样的问题:-(

public async Task<bool> SomeMethod()
{
DependencyService.Get<ILodingPageService>().InitLoadingPage(new YourLoadingPage());
DependencyService.Get<ILodingPageService>().ShowLoadingPage();
var SomeValue = false;
if (await WhileIsBusy())
{
// Do stuff...
SomeValue = true; // Some other Method that returns a value
}
return SomeValue;
}
public static async Task<bool> WhileIsBusy(int frequency = 25, int timeout = -1)
{
bool IsBusy = true; // This can be a property in your BaseViewModel
bool Completed = false;
var waitTask = Task.Run(async () =>
{
while (IsBusy)
{
await Task.Delay(frequency);
Completed = SomeMethodToReturn();
IsBusy = false;
}
});
if (waitTask != await Task.WhenAny(waitTask, Task.Delay(timeout)))
{
throw new TimeoutException();
}
return Completed;
}

这将使LoadingPage在等待返回值时保持活动状态,当所有操作完成时,您可以调用。。。

DependencyService.Get<ILodingPageService>().HideLoadingPage();

我希望这能帮助像我这样仍在努力学习的人。

最新更新