I'm starting to wonder if it's just some setting somewhere, but adding event handlers to controls in XAML is proving to be frustrating in .NET Maui. For instance, if I just want to create this button:
<Button Text="Good job"
x:Name="GoodJobButton"
Clicked="GoodJobButton_Clicked"/>
It will create a perfectly fine Event Handler in the code behind:
private void GoodJobButton_Clicked(object sender, EventArgs e)
{
//Do cool magic
}
But it's not actually attaching the handler to the control. I get an error unless I do this in the constructor:
GoodJobButton.Clicked += GoodJobButton_Clicked;
And WOW, I don't want to have to do that for everything I have in this app. I'm migrating over from Xamarin, and this is pretty much the last problem I have to solve until I'm off and running again.
Anyway, please tell me this is a bug or something. Otherwise, shame me if you must. Thanks!
Related
My project became corrupt today and since fixing i have noticed that at the end of events like button click its adding _1 at the end of everything
for example
private void createNewUserToolStripMenuItem_Click_1(object sender, EventArgs e)
{
}
where as it should obviously be
private void createNewUserToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I have no idea what has happened but its causing me to repeat a lot of work. I have not duplicated controls or anything like that so i am very confused
Does anyone have any ideas? Please this is over 2 years of work and its looking like its knackered
Thanks to Idle_Mind this worked -
The designer lost the fact that the control was wired up and when you double click it it sees that there is already a method with that name so it adds one. All you need to do is select that control, go to the Events listing in the Properties pane, find that event, and then change the listed handler using the dropdown back to the original method. –
Basically I am getting this error
EventHandler "PressMeButton_Clicked" with correct signature not found in type "login.MainPage" (login)
login is the name of my project and login_Clicked is my clicked event handler
I'm trying to make a simple login page,and on the documentation I've seen various ways of Clicked working but everytime I try to write the method on the xaml.cs file when I compile it keeps giving the error above, if I remove the method it works.
<Button x:Name="PressMeButton"
Text="Press Me!"
Pressed="PressMeButton_Pressed"
Clicked="PressMeButton_Clicked" />
this is the XAML part you can see on Clicked the name I have for the event
and
private void PressMeButton_Clicked(object sender, EventArgs e)
{
(sender as Button).Text = "I was just clicked!";
}
So I expect to show me a text saying I was clickd but it does not even compile.
This is an old question, but I ran into a similar issue. There was no good explanation why it would give the error. It would even happen on an empty handler that the IDE generated itself. I tried cleaning the solution, rebuilding, etc.
I found that there were some stuck build tasks going still. They appeared in the bottom bar that normally shows "Ready". For me, the solution was restarting VS. Once it started back up, and build worked fine.
event handlers in the code-behind should be protected, not private
protected void PressMeButton_Clicked(object sender, EventArgs e)
{
(sender as Button).Text = "I was just clicked!";
}
I has rey use VS 2015 with xamarin forms to create a cross platform project. I add the xaml page which contain a text box and button. In the code behind, I has code as below
public Inno()
{
InitializeComponent();
btntest.Clicked += Btntest_Clicked;
}
Private void Btntest_Clicked(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(Name.Text))
{
return;
}
}
When I run the android simulator to test the page, it fire the constructor. But when I clicke the button, it not fire the click event. Anything goes wrong? Please help. Thanks
Theres a few things you can try:
hook up the event handler in the xaml, see if that works - because you are subscribing to the event in the constructor this shouldn't make a difference functionality wise.
Edit: to do this, in your xaml:
<Button Clicked="buttonClicked" />
and Press tab when intellisense prompts you to create a new handler.
as #hankide said - set a breakpoint and make sure it isn't actually entering it and that the problem isn't the code inside the event handler.
You need to make sure that your btntest object has been properly mapped to the button on the XAML side of things. By default, XAML objects don't have "names" as you may be accustomed to in WinForms or WebForms and need to have a name property assigned to them. For me, I typically have my buttons marked up as:
<Button x:Name=btntest Text="Click Me></Button>
Then in the code behind I have to find the button by name in order to wire up the event:
var testButton = this.FindByName<Button>("btntest");
testButton.Clicked += Btntest_Clicked;
From there everything works as expected.
Try this:
btntest.Clicked += new EventHandler(Btntest_Clicked);
Thanks everyone for the answer...not sure why I close the solution and re-open it, it work fine.
I'm using both winform and incorporating wpf through ElementHost.
How can I call a WPF ICommand from a Winforms button's click event? These are all new to me so bear with me with these kind of questions.
My current code is
CarView car = (CarView) CarHost.Child;
CarViewModel cvm = (CarViewModel) car.DataContext;
cvm.SaveCommand.Execute(null);
So by doing that I was able to call the SaveCommand, but I dont get any data.
Thanks in advance.
I might be missing something here. Normally you'd do something like:
<Button Command="{Binding MyCommand}" />
And then when someone clicks the button, MyCommand's Execute method is called.
I suppose, from a codebehind, you could call:
private void OnClick(object sender, RoutedEventArgs e)
{
if (MyCommand.CanExecute(null))
MyCommand.Execute(null);
}
But except for very specific circumstances (which you haven't mentioned) I'm not sure why you'd do it that way. I think you definitely need to give us a little more information.
I'm having a problem setting up an event on a form. Here's the setup:
I was given a page with two controls, two different versions of a form for the end-user to fill out- standard and advanced. The two forms have different code and javascript, so the original dev put them in separate web user controls. Aside from the controls is a LinkButton that switches to Advanced mode.
<uc1:Standard runat="server" ID="StandardForm" />
<uc1:Advanced runat="server" ID="AdvancedForm" />
<asp:LinkButton runat="server" ID="lnkAdvanced" Text="Go To Advanced" OnClick="lnkAdvanced_Click" />
lnkAdvanced_Click just takes all the info currently entered to the advanced and flips the Visible.
My problem is that one of the bosses wants the 'Go to Advanced' button inside the standard form, but the .Visible code is on the page. So I thought it could be done using an event, but it doesn't seem to be working.
I tried to set up the event like this:
public event EventHandler AdvanceClick;
protected void lnkAdvanced_Click(object sender, EventArgs e) {
AdvanceClick(sender, e);
}
And when that didn't work, I tried to set up a delegate:
public delegate void AdvancedEventHandler(object sender, EventArgs e);
public event AdvancedEventHandler AdvanceClick;
When I moved the button to the standard form, I expected to be able to type something like:
StandardForm.AdvanceClick += new AdvancedEventHandler(GoToAdvanced);
But it doesn't seem to recognize any events within the control! I get an error: "Standard does not contain a definition for 'AdvanceClick' and no extension method 'AdvanceClick accepting a first argument of type 'Standard' could be found" It finds the other properties just fine, am I going about this the wrong way?
// in your Standard user control
public event EventHandler AdvancedClick;
private void lbtnAdvanced_Click(object sender, EventArgs e)
{
OnAdvancedClick(e);
}
protected void OnAdvancedClick(EventArgs e)
{
if (AdvancedClick != null)
AdvancedClick(this, e);
}
// on your page
StandardForm.AdvancedClick += new EventHandler(StandardForm_AdvancedClick);
private void StandardForm_AdvancedClick(object sender, EventArgs e)
{
// toggle logic here
}
If the standard form has the "Switch to advanced" button. Then, clearly, it has to know about the Advanced form and thus they seem to be pretty tightly coupled. If this is the case, it seems to me that you might as well just have the advanced form as a child of the standard form then... or better yet, merge them into one control.
If you don't like these options you might want to create a third controls which hosts the button and the two forms, along with the logic to move data between them and toggle their visibility.
I personally recommend the single control option. Having tighly coupled controls usually just leads to confusion down the road. You could loosen up the dependency in various ways, but think hard about it before you do so.
In the legacy project I currently work on we have a bunch of examples such as serach forms and search results being split up into multiple controls, but then in the end needing each others instances to function properly. As I said earlier, I wont reccomend this path.
You shouldn't need the delegate because you've created a standard event.
Try in your form load or thereabouts:
StandardForm.AdvanceClick += new EventHandler(GoToAdvanced);
Then somewhere on the page that hosts the 2 user controls
protected void GoToAdvanced(object sender, EventArgs e)
{
//Code that was previously in lnkAdvanced_Click on page.
}
Edit:
It does sound like the setup is wrong.
Can you post the markup for the Host page (at this point we are assuming it is simply the 2 user controls).
Then we are also assuming that the AdvanceClick event is declared in the Standard UC but the error message would indicate that it doesn't.. and the lnkAdvanced_Click method is in the Standard UC?
Then we are assuming the code that is attempting to attach to the custom event is declared in the Host page.
If you could confirm or deny the assumptions i'm sure we could get this cleared up.