更新用户界面,然后添加延迟

时间:2018-10-01 06:26:22

标签: xamarin xamarin.forms xamarin.android

我正在使用Visual Studio 2017处理Xamarin表单项目。我想更新UI,然后添加延迟,然后导航到另一页。延迟后,我尝试了以下代码和UI更新,然后立即导航到下一页。

private void btnAccept_Clicked(object sender, System.EventArgs e)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        SetSuccess();
    });
}



private void SetSuccess()
{
    this.lblStatus.Text = "Thank you";
    this.imageFinger.Source = "kioskQrCodeSuccess.png";
    this.scanProgressBar.IsVisible = false;
    this.LabelInfo.IsVisible = false;

    Stopwatch s = new Stopwatch();
    s.Start();
    while (s.Elapsed < TimeSpan.FromSeconds(3))
    {
    }

    s.Stop();

    App.Current.MainPage = new NavigationPage(new Profile());
}

2 个答案:

答案 0 :(得分:2)

改为尝试Device.StartTimer

import pandas as pd

from bokeh.models import ColumnDataSource, ColorBar, CategoricalColorMapper
from bokeh.plotting import figure, show
from bokeh.palettes import Spectral6
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.layouts import widgetbox

from bokeh.sampledata.iris import flowers as df
source = ColumnDataSource(df)

mapper = CategoricalColorMapper(
    factors=['setosa', 'virginica', 'versicolor'], 
    palette=['red', 'green', 'blue']) 

plot = figure(x_axis_label='petal_length', y_axis_label='sepal_length',plot_width=400,plot_height=400)
plot.circle('petal_length', 'sepal_length',size=4, source=source, 
            color={'field': 'species', 
                   'transform': mapper}) 

species=list (df['species'].unique())

menu = Select(options=species,value='setosa', title='Species')

# Add callback to widgets
def callback(attr, old,new):
    source_data=pd.DataFrame(source.data)
    new_data= source_data.loc[source_data['species']==menu.value]
    new_data_dict=dict (new_data)
    source.data=new_data_dict

menu.on_change('value', callback)

layout = column(menu, plot)
curdoc().add_root(layout)

答案 1 :(得分:2)

您可以考虑在异步事件处理程序中使用Task.Delay

private async void btnAccept_Clicked(object sender, System.EventArgs e) {
    //On UI Thread
    SetSuccess();

    //Non blocking wait
    await Task.Delay(TimeSpan.FromSeconds(3));

    //Back on UI thread
    App.Current.MainPage = new NavigationPage(new Profile());
}

private void SetSuccess() {
    this.lblStatus.Text = "Thank you";
    this.imageFinger.Source = "kioskQrCodeSuccess.png";
    this.scanProgressBar.IsVisible = false;
    this.LabelInfo.IsVisible = false;
}