DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • AI Coding Assistants: Capabilities, Tools, Trends, and Comparisons
  • Understanding the 5 Levels of LeetCode to Crack Coding Interview
  • The Rise of Vibe Coding: Innovation at the Cost of Security
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices

Trending

  • Testing Distributed Microservices Using XState
  • Analysis of the Data Processing Framework of Pandas and Snowpark Pandas API
  • Securing Software Delivery: Zero Trust CI/CD Patterns for Modern Pipelines
  • Migrating SQL Failover Clusters Without Downtime: A Practical Guide

XAML Coding Convention

By 
$$anonymous$$ user avatar
$$anonymous$$
·
Dec. 22, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

over the past year, i have built my own coding conventions for c#. i also always manage to convince my colleagues to follow my coding convention if they don’t already have one. i’m a real freak about following coding conventions; if i see someone modifying one of my files and not following my conventions, i might have trouble sleeping at night (ok, not that much, but…).

with the help of the great visual studio add-on resharper , it’s easy to format code with rules. you only need to press ctrl-e/ctrl-c to format a document. resharper is a must have tool for visual studio.

for the past two years, since the release of the windows phone platform, i have been using the xaml language to program my user interfaces. finding coding conventions for c# is pretty easy, but for xaml, it was a bit more of a challenge. my first move was to check the default microsoft projects, but i concluded that even they are a bit messy even for today.

here is an example of a windows store grid app project:

<listview.groupstyle>
    <groupstyle>
        <groupstyle.headertemplate>
            <datatemplate>
                <grid margin="7,7,0,0">
                    <button
                        automationproperties.name="group title"
                        click="header_click"
                        style="{staticresource textprimarybuttonstyle}">
                        <stackpanel orientation="horizontal">
                            <textblock text="{binding title}" margin="3,-7,10,10" style="{staticresource groupheadertextstyle}" />
                            <textblock text="{staticresource chevronglyph}" fontfamily="segoe ui symbol" margin="0,-7,0,10" style="{staticresource groupheadertextstyle}"/>
                        </stackpanel>
                    </button>
                </grid>
            </datatemplate>
        </groupstyle.headertemplate>
    </groupstyle>
</listview.groupstyle>

first of all, there are not any empty lines, and secondly the button has attributes on separate lines, but for the textblock elements, the attributes are on the same lines without any order.

with time, i developed my own xaml coding convention that i would like to share. one of the reasons that i developed my own xaml coding convention is i don’t like to use the properties window, because it is hard to have an overview of the properties that are not set to default.

image

my coding convention is resumed in 5 points:

1- put empty lines between elements.

don’t be afraid to put empty lines. it makes reading the code easier.

<grid height="250"
      verticalalignment="top">

    <image source="{binding featurearticle1.thumbnail}"
           style="{staticresource imagethumbnailstyle}" />

    <stackpanel style="{staticresource stackpanelsummarystyle}">

    <textblock fontsize="22"
               style="{staticresource textblockauthorstyle}"
               text="{binding featurearticle1.author}" />

    <textblock fontsize="26"
               height="70"
               style="{staticresource textblocksummarystyle}"
               text="{binding featurearticle1.title}" />

    </stackpanel>

</grid>

my exceptions are the grid.columndefinition and grid.rowdefinitions, because they only have one line attribute.

<grid.columndefinitions>
    <columndefinition width="200" />
    <columndefinition width="200" />
</grid.columndefinitions>

<grid.rowdefinitions>
    <rowdefinition height="200" />
    <rowdefinition height="140" />
</grid.rowdefinitions>

2- put one attribute per line.

<textblock fontweight="bold"
           foreground="white"
           horizontalalignment="right"
           margin="0,0,12,0"
           text="{binding articlescounttext}"
           textwrapping="wrap" />

3- order the attributes alphabetically.

<image source="/assets/shares/neutralimage.png"
       height="125"
       horizontalalignment="center"
       width="125"
       stretch="uniformtofill"
       verticalalignment="center" />

some will argue that height and width should be side by side or on the adjacent line, but i still prefer the alphabetical order, because it is much easier to read when you know what order your definitions are in. also, if there is an element with many attributes, it is much easier to check whether an attribute is missing.

4- put the attached properties at the beginning and in an alphabetic order.

<button grid.column="1"
        grid.row="2"
        command="{binding showwritercommand}"
        commandparameter="{binding writerashley}"
        style="{staticresource hubtilebuttonstyle}" />

the grid.column / grid.row are the classic examples.

5- definition of styles can be less strict.

when i’m creating styles with expression blend, i tend to leave them as-is when they are big. it is more about saving time than anything else. however, when a style is small, i don’t put empty lines and i put the properties in an alphabetic order like this:

<style x:key="gridfeaturestyle"
        targettype="grid">
    <setter property="height"
            value="194" />
    <setter property="verticalalignment"
            value="top" />
    <setter property="width"
            value="194" />
</style>

conclusion

it might not be the perfect solution for you, but if you do not have one, my convention is a good start especially if you are sharing code with colleagues.

my motto about coding convention is the following: it is better to have a coding convention than not having one!

happy coding!

Coding (social sciences)

Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • AI Coding Assistants: Capabilities, Tools, Trends, and Comparisons
  • Understanding the 5 Levels of LeetCode to Crack Coding Interview
  • The Rise of Vibe Coding: Innovation at the Cost of Security
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: