C# Xamarin表单更改和启用图像按钮
有人知道如何更改Imagebutton源代码吗?参考图片链接,当用户点击step1(主页)时,它会将用户重定向到另一页(第二页)。一旦用户单击SecondPage中的“完成”按钮,我想更改imagebutton源,同时启用click事件。我不确定是否从一个页面调用该函数到另一个页面 (p.S我的btnclickcount有时似乎不起作用……我只想在第一次单击按钮时记录开始日期时间) 第三页C# Xamarin表单更改和启用图像按钮,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,有人知道如何更改Imagebutton源代码吗?参考图片链接,当用户点击step1(主页)时,它会将用户重定向到另一页(第二页)。一旦用户单击SecondPage中的“完成”按钮,我想更改imagebutton源,同时启用click事件。我不确定是否从一个页面调用该函数到另一个页面 (p.S我的btnclickcount有时似乎不起作用……我只想在第一次单击按钮时记录开始日期时间) 第三页 public partial class ThirdPage : ContentPage { L
public partial class ThirdPage : ContentPage
{
Label wopLblOnlineEndDT;
MainPage mainpage;
ImageButton btntroubleshoot;
Label wolblOnlineStatus;
public ThirdPage()
{
InitializeComponent();
}
public ThirdPage(MainPage woPage, Label lblOnlineEndDT, ImageButton btnTS, Label lblOnlineStatus)
{
InitializeComponent();
mainpage = woPage;
wopLblOnlineEndDT = lblOnlineEndDT;
btntroubleshoot = btnTS;
wolblOnlineStatus = lblOnlineStatus;
}
private void BtnDone_Clicked(object sender, EventArgs e)
{
string edt = DateTime.Now.ToString();
wopLblOnlineEndDT.Text = edt;
mainpage.mainpagevalue = wopLblOnlineEndDT.Text;
btntroubleshoot.Source = "troubleshooting";
btntroubleshoot.IsEnabled = true;
wolblOnlineStatus.Text = "COMPLETED";
wolblOnlineStatus.TextColor = Color.FromRgb(0, 214, 54);
Navigation.PopAsync();
}
}
XAML
您也可以将
imageButton
的引用传递到第二页
,并在done方法中更改源代码。让我告诉你怎么做:
在主页xaml中,创建名为MyImageButton
的ImageButton
:
<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>
在第二页中,获取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();
}
}
有一个示例项目可用。将按钮不透明度绑定到类似于0.5的值,并禁用按钮
<ImageButton Command="{Binding EditActivityCommand}"
BackgroundColor="Transparent"
Opacity="{Binding Opacity}"
IsEnabled="{Binding IsAdmin}">
您好,再次感谢您的帮助!非常感谢。我可以澄清一下关于创建公共第二页的问题吗?在主页上执行navigation.pushasync时,它是否仅适用于特定的按钮单击事件及其在xaml中声明的变量?因为我尝试在我的btnoffline click事件中更改另一个图像按钮源,所以它不起作用。我认为在public secondpage中传入变量意味着它正在调用button ID?@Charis您所需要做的就是获取要更改的控件的引用。向我们展示不起作用的代码,然后我们可以更好地帮助你。嗨,我已经更新了我的代码。我想更改myimagebutton2源并保留myimagebutton源。但在我点击按钮后,它重叠了。我在调整了一些代码后,它就工作了!:)非常感谢。我的时间戳仍然有问题。我不确定是不是因为pushasync,我的按钮点击计数将重新启动,因此我的条件不起作用。关于点击计数的代码在哪里?顺便说一句,如果这个答案能解决你的问题,你能帮我标记一下吗。
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;
}
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();
}
}
<ImageButton Command="{Binding EditActivityCommand}"
BackgroundColor="Transparent"
Opacity="{Binding Opacity}"
IsEnabled="{Binding IsAdmin}">