Xamarin表单更改和启用图像按钮

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

Sample UI design

有人知道如何更改Imagebutton的来源吗?参考图片链接,当用户单击step1(MainPage)时,它将重定向用户到另一个页面(SecondPage)。用户单击SecondPage中的“完成”按钮后,我想更改我的imagebutton源,同时启用点击事件。我不确定不要从一页到另一页调用该函数。

(P.S我的btnclickcount有时似乎不起作用...我只想记录第一次单击按钮的开始日期时间)

   <StackLayout>
    <!-- Place new controls here -->

    <Label x:Name="lblStartDT" Text="startTest" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand"/>

    <Button Text="go to secondPage" Clicked="btnOffline_Clicked"  HorizontalOptions="Center"
   VerticalOptions="CenterAndExpand"/>

    <Label x:Name="lblEndDT" Text="endTest" 
   HorizontalOptions="Center"
   VerticalOptions="CenterAndExpand" />

    <ImageButton x:Name="MyImageButton" Source="onlinetool.png" IsEnabled="False" 
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand" />


    <ImageButton x:Name="MyImageButton2" Source="pack.png" IsEnabled="False" 
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand" />

public partial class SecondPage : ContentPage
{
    Label MainPagelblEndDT;
    MainPage mainPage;
    ImageButton myImageBtn;
    public SecondPage()
    {
        InitializeComponent();
    }

    public SecondPage(MainPage mainP, Label lblEndDT)
    {
        InitializeComponent();

        //Get the lblEndDT reference here
        MainPagelblEndDT = lblEndDT;
        //Get the MainPage reference here
        mainPage = mainP;
    }

    public SecondPage(MainPage mainP, Label lblEndDT, ImageButton imageBtn)
    {
        InitializeComponent();

        //Get the lblEndDT reference here
        MainPagelblEndDT = lblEndDT;
        //Get the MainPage reference here
        mainPage = mainP;
        //Get the ImageButton reference here
        myImageBtn = imageBtn;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        string edt = DateTime.Now.ToString();

        //Use it
        MainPagelblEndDT.Text = edt;

        mainPage.previouspagevalue = MainPagelblEndDT.Text;

        //change the source of imageBtn
        myImageBtn.Source = "pacakging";
        myImageBtn.IsEnabled = true;

        Navigation.PopAsync();
      }

}

 private void btnOffline_Clicked(object sender, EventArgs e)
    {

        int btnclickcount = 0;
        btnclickcount++;

        if(btnclickcount == 1)
        {
            string currentDT = DateTime.Now.ToString();
            lblStartDT.Text = currentDT;
        }

        Navigation.PushAsync(new SecondPage(this, lblEndDT, MyImageButton2));



    }
c# xaml xamarin xamarin.forms
1个回答
0
投票

您也可以将imageButton的引用传递给SecondPage,并在完成的方法中更改Source。让我告诉你如何做:

MainPage xaml中,创建一个名为ImageButtonMyImageButton

<StackLayout>
    <!-- Place new controls here -->

    <Label x:Name="lblStartDT" Text="startTest" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand"/>

    <Button Text="go to secondPage" Clicked="btnOffline_Clicked"  HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand"/>

    <Label x:Name="lblEndDT" Text="endTest" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />

    <ImageButton x:Name="MyImageButton" Source="Images.png" IsEnabled="False" 
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand" />

</StackLayout>

在后面的代码中,将MyImageButton传递给SecondPage:

    private void btnOffline_Clicked(object sender, EventArgs e)
    {
        //Pass the parametere you need when you go to SecondPage 
        //Navigation.PushAsync(new SecondPage(this, lblEndDT));

        Navigation.PushAsync(new SecondPage(this, lblEndDT, MyImageButton));

        string currentDT = DateTime.Now.ToString();
        lblStartDT.Text = currentDT;

    }

在SecondPage中,获取myImageBtn的引用并在done方法中对其进行修改:

public partial class SecondPage : ContentPage
{
    Label MainPagelblEndDT;
    MainPage mainPage;
    ImageButton myImageBtn;
    public SecondPage()
    {
        InitializeComponent();
    }

    public SecondPage(MainPage mainP,Label lblEndDT)
    {
        InitializeComponent();

        //Get the lblEndDT reference here
        MainPagelblEndDT = lblEndDT;
        //Get the MainPage reference here
        mainPage = mainP;
    }

    public SecondPage(MainPage mainP, Label lblEndDT, ImageButton imageBtn)
    {
        InitializeComponent();

        //Get the lblEndDT reference here
        MainPagelblEndDT = lblEndDT;
        //Get the MainPage reference here
        mainPage = mainP;
        //Get the ImageButton reference here
        myImageBtn = imageBtn;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {           
        string edt = DateTime.Now.ToString();

        //Use it
        MainPagelblEndDT.Text = edt;      

        mainPage.previouspagevalue = MainPagelblEndDT.Text;

        //change the source of imageBtn
        myImageBtn.Source = "Images1";
        myImageBtn.IsEnabled = true;

        Navigation.PopAsync();
    }
}

示例项目可用here

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