Formatting Code Blocks in your posts.

ChaseFlorellChaseFlorell CAInsider, University mod
edited May 2016 in General

The Markdown link provided to the top right of every editor is helpful, but doesn't cover code formatting (other than a small mention of Monospace). Here's a quick guide to formatting your code blocks in the Xamarin forums editor. This will help others better read and understand your code, and help us better answer your questions.


Inline code:
simply wrap your code in backticks.

Example: `var foo = "bar";`
Result: var foo = "bar";


Code Block: (option a)
Indent everything 4 characters, and leave an empty row above and below your block.

Exampe:

----public class Foo
----{
--------// stuff
----}

Result:

public class Foo
{
    // stuff
}

Code Block: (option b - preferred)
wrap your code with 3 backticks above and 3 backticks below - note after the start backticks, you can optionally add a language specifier

Example:
```xaml
<StackLayout>
----<Label Text="Foo" />
</StackLayout>
```
Result:

<StackLayout>
    <Label Text="Foo" />
</StackLayout>

the dashes are there to portray spaces.

Posts

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    Thanks for posting this @ChaseFlorell

    And... at the readers of this thread... you can find more hints to the post-formatting, forum generally and more (in the .pdf) here:
    http://forums.xamarin.com/discussion/36821/quickstart-guide-to-start-developing-with-xamarin-forms#latest

  • JohnHJohnH GBMember ✭✭✭✭✭

    Nice, I didn't know about code block option b above, but it is now my preferred method!

  • DonCB2BDonCB2B USMember ✭✭✭

    Thanks, it's very helpful.

  • GuyProvostGuyProvost CAMember ✭✭✭
    edited December 2016

    Great to post this... Reading the posts it feels like 9 users out of 10 mess up code pasting. This post should be a sticky!

  • EasyEddyEasyEddy USMember ✭✭

    Thank you, that was helpful!

    //Eddy

  • JohnTolleJohnTolle USMember ✭✭

    The Markdown link in the tiny text You can use Markdown in your post. below the post/question/comment entry area really needs to include an additional "Code" link to this post or a more formal document on the subject. I've seen a decent number of posts where posters are complaining that there's no way to properly markdown code!

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Something like Markdown Syntax or (better since it covers the code blocks and syntax highlighting used here) Markdown Cheatsheet.

  • JohnTolleJohnTolle USMember ✭✭

    The first one is a cool format for sure, but I agree that the second is more appropriate since it includes things like the triple back-tick method for marking code blocks. Not that the current link to the Wikipedia article is bad, but it excludes a lot of important stuff. Sure, we could update the Wikipedia article, but who does that!? ;)

  • JohnTolleJohnTolle USMember ✭✭

    There, I went and did it. Now the WikiPolice are going to get me...

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    @JohnTolle said:
    There, I went and did it. Now the WikiPolice are going to get me...

    Looks like they did. Your code blocks example was removed because:

    There is already enough examples to make the point in the article. Also, your style must not deviate from that of the article.

    Nice try!

    It looks like the best option is going to be for either Xamarin or Vanilla to change the link to something that actually documents Markdown usage instead of the Wikipedia article.

  • JohnTolleJohnTolle USMember ✭✭

    Sigh. Figures. Jerks. I wonder if I had put the codeblock example in their lame table format if they would have allowed it to remain. Ah, well.

    OK, Representatives of Xamarin, who can we beg to change that little Markdown link to something that actually includes code block examples such as the second one that @DaveHunt supplied above?

  • JohnTolleJohnTolle USMember ✭✭

    Bueller? Bueller? I know that if I let this go, it might very well just fade into the ether...

  • Jean-DanielGasserJean-DanielGasser CHMember ✭✭

    Yes, I just spent 20 minutes trying to format a block of code.
    Now thanks this post I will never loose time in this case.
    Thank you !

  • stemadostemado Member ✭✭✭
    edited December 2018

    You can also highlight your code and then press Ctrl + o to format the block of code (will visibly move each highlighted line four spaces).

    Thanks @SebastianSeidel.9226 for this time saver.

    @FredyWenger can you add this one to your pdf?

  • LearnEverythingLearnEverything USMember ✭✭✭

    CODE BLOCK between ~~~~

    ~~~~delete this
    This is a
    piece of code
    in a block
    ~~~~delete this

Sign In or Register to comment.