Xamarin Forms crossPlatform:强制重绘
Xamarin Forms crossPlatform: force to repaint
我知道我知道:这可能是最常被问到的问题之一。在你给我发送一些 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";
}
这是我的内容页面:
<?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 方法中的进度条和标签。但首先我必须学习如何做这些简单的东西,然后再尝试更复杂的东西。
提前致谢!
不要像现在这样设置进度 属性,而是尝试在异步方法中使用进度条的 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";
}
这有一个额外的好处,即实际看到进度条在移动,而不仅仅是从一个值跳到下一个值。
我知道我知道:这可能是最常被问到的问题之一。在你给我发送一些 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";
}
这是我的内容页面:
<?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 方法中的进度条和标签。但首先我必须学习如何做这些简单的东西,然后再尝试更复杂的东西。
提前致谢!
不要像现在这样设置进度 属性,而是尝试在异步方法中使用进度条的 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";
}
这有一个额外的好处,即实际看到进度条在移动,而不仅仅是从一个值跳到下一个值。