Xamarin XAML語言教程隱藏文件使用Progress屬性設(shè)置進(jìn)度條
Xamarin XAML語言教程隱藏文件中使用Progress屬性設(shè)置進(jìn)度條進(jìn)度,開發(fā)者除了可以在XAML中使用Progress屬性設(shè)置進(jìn)度條的當(dāng)前進(jìn)度外,還可以在代碼隱藏文件中使用Progress屬性來設(shè)置進(jìn)度條的當(dāng)前進(jìn)度。這時(shí),首先需要在XAML文件中,使用x:Name屬性為進(jìn)度條定義一個(gè)名稱,然后在代碼隱藏文件中通過定義的名稱對Progress屬性進(jìn)行設(shè)置即可。
【示例12-7:ProgressBarProgressOne】以下將在代碼隱藏文件中實(shí)現(xiàn)對進(jìn)度條當(dāng)前進(jìn)行的設(shè)置。具體的操作步驟如下:
(1)MainPage.xaml文件,編寫代碼,對內(nèi)容頁面進(jìn)行布局。代碼如下:
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ProgressBarProgressOne"
x:Class="ProgressBarProgressOne.MainPage">
VerticalOptions="Center">
Clicked="SetProgressPointTwo"/>
Clicked="SetProgressPointSix"/>
Clicked="SetProgressOne"/>
(2)打開MainPage.xaml.cs文件,編寫代碼,實(shí)現(xiàn)通過按鈕控制進(jìn)度條當(dāng)前進(jìn)度的功能。代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ProgressBarProgressOne
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
//將進(jìn)度條當(dāng)前的進(jìn)度設(shè)置為0.2
void SetProgressPointTwo(object sender, EventArgs args)
{
progressBar.Progress = 0.2;
}
//將進(jìn)度條當(dāng)前的進(jìn)度設(shè)置為0.6
void SetProgressPointSix(object sender, EventArgs args)
{
progressBar.Progress = 0.6;
}
//將進(jìn)度條當(dāng)前的進(jìn)度設(shè)置為1
void SetProgressOne(object sender, EventArgs args)
{
progressBar.Progress = 1;
}
}
}
此時(shí)運(yùn)行程序,會(huì)看到如圖12.24~12.25所示的效果。當(dāng)開發(fā)者輕拍某一按鈕后,會(huì)看到進(jìn)度條中顯示對應(yīng)的進(jìn)度,效果類似于圖12.26~12.27所示。