Here is the problem:
You begin to write a question, and you include your issue / non-working code
You find the answer in the meantime, and you use the "Answer your own question – share your knowledge, Q&A-style" feature.
Then you realize it makes no sense anymore to post the wrong non-working code in the question itself, because you have working code in the answer! And non-working code would be visual pollution because it's pointless: you have the answer already.
You post it, and you get downvoted because people think "It looks like you want us to write some code for you" (which is obviously wrong -> other criticisms can be made, but not this one).
More generally, I feel that the "Answer your own question – share your knowledge, Q&A-style" philosophy is generally not welcome on Stack Overflow. Why?
In my self-answered question I really wanted to share the effort of searching for the answer, finding the right answer (okay, it was in a blog, but it took time to find the relevant one, and I had to slightly modify the code to make it work), and post it for future reference, for other people.
I have no other interest. (If it was possible, I would be okay with no voting on self-Q&A, to show that it's not to gain reputation).
Please believe me that was really done in no other interest than future reference.