简体   繁体   English

如何在将文本添加到 Xamarin Forms 中的条目之前加载视图

[英]How to load view before add text to entry in Xamarin Forms

I have a problem with OnAppearing function.我对 OnAppearing function 有疑问。 I want to load the page first before adding the a text inside my entry.我想先加载页面,然后再在我的条目中添加文本。 What is happening right now is that it is happening early meaning the text was inserted before the page has been loaded.现在发生的事情是它发生在早期,这意味着文本是在页面加载之前插入的。 Is there a way to solve this?有没有办法解决这个问题?

protected override async void OnAppearing()
{
    base.OnAppearing();
    entEncodedBy.Text = "Admin";
}

You can use any overloads of await Task.Delay();您可以使用await Task.Delay();的任何重载。 to wait a long as you want and then change the text.等待很长时间,然后更改文本。

Or或者

You can use Device.StartTimer(TimeSpan, Func<Boolean>);您可以使用Device.StartTimer(TimeSpan, Func<Boolean>);

I not sure, but you can try add only await base.OnAppearing();我不确定,但您可以尝试仅添加 await base.OnAppearing(); or you can just await Task.Run(()=>base.OnAppearing());或者你可以等待 Task.Run(()=>base.OnAppearing()); if this first method dont work.如果第一种方法不起作用。

A simple method is to use MessagingCenter , you can refer to the following code:一个简单的方法是使用MessagingCenter ,可以参考以下代码:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        MessagingCenter.Subscribe<MainPage>(this, "Hi", async (sender) =>
        {
            await Task.Delay(2000); // 2000ms == 2s
            entEncodedBy.Text = "Admin";
        });
    }

    protected override  void OnAppearing()
    {
        base.OnAppearing();

        MessagingCenter.Send<MainPage>(this, "Hi");
    }
}

For more details, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center有关更多详细信息,您可以查看: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM