Xamarin表示跨平台:强制重绘

问题描述 投票:0回答:1

我知道我知道:这可能是最常见的问题之一。在你向我发送一些LMGTFY链接之前,让我告诉我这个问题已经有几个小时了,我已经尝试了Invalidate,PostInvalidate,RunOnUIThread等;没有成功。我不抛弃解决方案可能是前面提到的解决方案之一,我没有正确使用它。我实际上正在学习Xamarin,做我的第一个跨平台应用程序,所以我对框架的了解非常差。

那么现在让我们回到我的具体问题,让我们看看是否有人可以帮助我。我想要一个我的应用程序的介绍页面,上面有一个进度条和一个文本说明正在做什么应用程序(启动应用程序时,它调用WS来下载更改,并且必须从文本文件加载信息并将其放入某些内容中用于所有页面的静态数据结构)。我想在加载页面中做的是以下顺序:1。更改文本以告诉应用程序在做什么。 2.调用WS或加载文件。 3.更新进度条。 4.如果加载了所有内容,请转到下一个更新或欢迎页面。

我得到的实际代码是在完成所有工作后页面加载,所以我看到进度条已完成,最后一个文本发生了变化。但这是一个静态页面,我没有看到进度条增长,文本也没有变化。

这是我的代码:

 public partial class LoadingPage : ContentPage
 { 
        InitializeComponent();
        this.lpb.Text = "Connecting to web server to check updates";
        App.localInfo.updateInfo(); //Connect web server to check updates
        this.pb.Progress = 0.2;
        this.lpb.Text = "Loading info from local files";
        App.localInfo.cargarInfo(); //Load local files to memory for quick access
        this.pb.Progress = 0.7;
        this.lpb.Text = "Doing other stuff"; //This is only for proves

        Task.Run(async () =>
        {
            await Task.Delay(2000);
        });

        this.pb.Progress = 1;
        this.lpb.Text = "Load completed. The app will start now";
 } 

这是我的ContentPage:

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Prueba1.Views.LoadingPage">
     <ContentPage.Content>
         <StackLayout>
             <ProgressBar x:Name="pb" Progress="0.0" ProgressColor="Red"/>
             <Label x:Name="lpb" Text="Welcome to Xamarin.Forms!"/>
         </StackLayout>
     </ContentPage.Content>
 </ContentPage>

这只是alpha版本。我想具体一点,因为我必须加载大约10个不同的文本文件,我想在App.localInfo方法中更新进度条和标签。但首先我必须学习如何做这些简单的事情,然后尝试更复杂的事情。

提前致谢!

xamarin.forms cross-platform repaint
1个回答
0
投票

不要像你一样设置进度属性,而是尝试在异步方法中使用进度条的ProgressTo方法。就像是:

public MainPage()
    {
        InitializeComponent();

        FireProgressBar();
    }

    async void FireProgressBar()
    {
        lpb.Text = "Connecting to web server to check updates";

        // Task.Delay to simulate network call.
        await Task.Delay(2000);

        await pb.ProgressTo(.2, 250, Easing.Linear);

        lpb.Text = "Loading info from local files";

        // Task.Delay to simulate network call.
        await Task.Delay(2000);

        await pb.ProgressTo(.7, 250, Easing.Linear);

        lpb.Text = "Doing other stuff";

        // Task.Delay to simulate network call.
        await Task.Delay(2000);

        await pb.ProgressTo(1.0, 250, Easing.Linear);

        lpb.Text = "Load completed. The app will start now";
    }

这具有实际看到进度条移动的额外好处,而不仅仅是从一个值突然移动到下一个值。

© www.soinside.com 2019 - 2024. All rights reserved.