0

I am now two weeks into using UWP in my C# course. And as it stands, I am getting increasingly annoyed with the way XML element properties are ordered. See how a certain section looks:

<Button x:Name="_newJourney" Content="Begin a New Journey" Margin="0,400,0,0" VerticalAlignment="Top" Height="72" Width="442" FontSize="36" Click="_newJourney_Click" Grid.Column="1" HorizontalAlignment="Center"/>
<Button Content="Load Game(?)" Height="72" Width="442" FontSize="36" Grid.Column="1" Margin="0,0,0,0" HorizontalAlignment="Center"/>
<Button Content="Options" Height="72" Width="442" FontSize="36" Margin="0,606,0,0" VerticalAlignment="Top" Grid.Column="1" HorizontalAlignment="Center"/>
<TextBox Margin="41,324,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Height="55" Grid.Column="1" HorizontalAlignment="Left" Width="379"/>
<Button x:Name="_newJourney_Copy" Content="⟲" Margin="431,324,38,0" VerticalAlignment="Top" Height="55" FontSize="36" Click="_newJourney_Click" Grid.Column="1" HorizontalAlignment="Stretch"/>

And, I'll be honest, I am standing in front of a question, do I simply reorder the element by myself, or do I ask if there is a way to actually refactor this with a quick action? My solution is to simply start off by asking.

I have tried googling, but similar to when I started programming it is rather difficult as I am unaware of the correct terms for XML / UWP designer. I.e. I don't actually think it's called properties, I don't even know if element is a correct term, I just took it from HTML.

In general, our entire class is experiencing issues searching for UWP help, we keep getting WPF tips instead :/

So, below is how I wish I could do. It would essentially sort the element properties within the XML file. The following example does include indentations, but I believe that is primarily just to show the ordering for you. I have no hope that any technique for this would perform the indentations as I've done. The actual ordering of elements is just a suggestion, I don't care if x:Name is in the beginning or the end, as long as it is consistent.

<Button  x:Name="_newJourney"       Content="New Journey"   Width="442" Height="72" Margin="0,400,0,0"      VerticalAlignment="Top"  HorizontalAlignment="Center" FontSize="36" Click="_newJourney_Click" Grid.Column="1" />
<Button  x:Name="_loadGame"         Content="Load Game(?)"  Width="442" Height="72" Margin="0,0,0,0"        HorizontalAlignment="Center" FontSize="36"  Grid.Column="1"/>
<Button  x:Name="_options"          Content="Options"       Width="442" Height="72" Margin="0,606,0,0"      VerticalAlignment="Top"  HorizontalAlignment="Center" FontSize="36"    Grid.Column="1"/>
<TextBox x:Name="_gameSeed"         Text="TextBox"          Width="379" Height="55" Margin="41,324,0,0"     VerticalAlignment="Top"  HorizontalAlignment="Left" TextWrapping="Wrap"   Grid.Column="1"  />
<Button  x:Name="_refreshGameSeed"  Content="⟲"            Width="55"  Height="55" Margin="431,324,38,0"    VerticalAlignment="Top" HorizontalAlignment="Stretch"  FontSize="36" Click="_newJourney_Click" Grid.Column="1" />

Again, another clarification. My idea for this would be something similar to CTRL-K, CTRL-F, which will auto indent code, this even works in XML. So simply a command or option that does an auto-sorting of the elements.

I've tried to be as thorough as I can possibly be, but I do fear that I am lacking a lot of term-knowledge. So if it's unclear, try reading between the lines to see what my request is. If it's still impossible, do request a clarification.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Bishiba
  • 103
  • 8

1 Answers1

0

If you care about the order in which the properties are set in the XAML markup, you should type the markup yourself in the text editor.

If you rely on the designer to set the properties for you, you'll typically end up with something like in your first code snippet.

And no, as far as I know, there is no way to tell Visual Studio to order the attributes for you once they have been added to the markup.

The lesson here should be learn how to write XAML and stop using the designer to write code/markup for you.

You can even disable the designer altogether if you want to. In WinUI, which is the next generation UI framework for Windows, the designer support has been dropped.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Well, I mean... I do write XML myself, I only showed the very latest thing that I did, essentially me dragging the elements into an estimated position. And was about to begin actually manipulating the element within the XML. And like I wrote, I do organize these myself the way I want them. Just looking for a faster way to do it, I am a stickler about code organization and such. And the UWP is school project, can't escape it :'( – Bishiba Apr 07 '22 at 11:29
  • "Dragging the elements into an estimated position" using the designed is not recommended if you care about "clean" and well-organized XAML markup. That's for sure. – mm8 Apr 07 '22 at 11:38
  • Any documentation for this and why it would be this way? - I don't see why dragging a button to the bottom left, into a specific grid column/row or other element like a canvas would cause problems. If I want it to be in the canvas, and I know I want to change Margin, Width, Height etc. then I simply don't need to write that out. I can just change it, it's a starting point, nothing else. And it's also easy to simply change the location of it. But yes... The actual editor is trash and never does what you'd want it to, that I know. Just seems like the effective way to do it, with a start point? – Bishiba Apr 07 '22 at 11:55
  • It doesn't cause "problems". You may accuse it of causing a cosmetic mess though but there is no documentation for this. Bottom line: Don't use the designer if you are not happy with the markup that it creates for you. – mm8 Apr 07 '22 at 12:03
  • I think then that we agree, apart from me wanting a rough starting point. It is a recurring problem for my class mates that they can't position their elements properly due to primarily using the designer. But rest assured, I mainly use the XML, or I actually mostly use C# to fix things as it is quite a bit more dynamic. Realizing I am basically socializing now, thanks for your information, I will keep it close and share with the class. – Bishiba Apr 07 '22 at 12:25
  • "It is a recurring problem for my class mates that they can't position their elements properly due to primarily using the designer" That's why they shouldn't use it. – mm8 Apr 07 '22 at 12:27
  • Indeed, but they keep trying. – Bishiba Apr 07 '22 at 12:47