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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Freedom to Code on Low-Code Platforms
  • Five Free AI Tools for Programmers to 10X Their Productivity
  • Difference Between High-Level and Low-Level Programming Languages
  • Thoughts About Writing Clear Code

Trending

  • Spring Authentication With MetaMask
  • Supercharging Productivity in Microservice Development With AI Tools
  • DevSecOps: Integrating Security Into Your DevOps Workflow
  • An Introduction to Build Servers and Continuous Integration

XAML Coding Convention

$$anonymous$$ user avatar by
$$anonymous$$
·
Dec. 22, 12 · Interview
Like (0)
Save
Tweet
Share
4.64K 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

  • Freedom to Code on Low-Code Platforms
  • Five Free AI Tools for Programmers to 10X Their Productivity
  • Difference Between High-Level and Low-Level Programming Languages
  • Thoughts About Writing Clear Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: