5 Replies - 929 Views - Last Post: 29 March 2017 - 03:15 AM

#1 JohnRipper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-March 17

Opinion about my XAML code

Posted 28 March 2017 - 05:57 AM

Hello ! I am new to the forum. I don't know if this is the best place to put my topic. If not, i apoligize. So... i am creating an application for medecins in WPF. Since i am not so much experienced with WPF(only with C#)... i wanted an opinion about my XAML code. Please do not "hate" if you don't like something in my code structure. Here is the code:

<Controls:MetroWindow x:Class="MedicalReminder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:local="clr-namespace:MedicalReminder"
        mc:Ignorable="d"
        Title="Revolution Pill Reminder" Height="450.305" Width="990.318" ResizeMode="CanMinimize" TitleCaps="False" GlowBrush="{DynamicResource AccentColorBrush}" BorderThickness="2" Loaded="MetroWindow_Loaded" Closing="MetroWindow_Closing" Icon="Medicalwp-Medical-Pills-blue.ico">
    <Grid Margin="0,0,4,0">
        <TabControl x:Name="tabControl" TabStripPlacement="Left" Height="422" VerticalAlignment="Top" Margin="10,0,0,0">
            <TabItem Header="Pill List">
                <Grid Background="#FFE5E5E5" Margin="0,0,-6,0" HorizontalAlignment="Left" Width="802">
                    <ListView x:Name="PillList" HorizontalAlignment="Left" Height="412" VerticalAlignment="Top" Width="802" ContextMenuOpening="PillList_ContextMenuOpening">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="ID" Width="30" DisplayMemberBinding="{Binding ID}"/>
                                <GridViewColumn Header="Pill Name" Width="200" DisplayMemberBinding="{Binding PillName}"/>
                                <GridViewColumn Header="Alert Time" Width="200" DisplayMemberBinding="{Binding AlertTime}"/>
                                <GridViewColumn Header="Sound Alert" Width="200" DisplayMemberBinding="{Binding SoundAlert}"/>
                                <GridViewColumn Header="Status" Width="170" DisplayMemberBinding="{Binding Status}"/>
                            </GridView>
                        </ListView.View>
                        <ListView.ContextMenu>
                            <ContextMenu>
                                <MenuItem Name="AddNewPillContext" Header="Add New Pill" Click="AddNewPill"/>
                                <MenuItem Name="EditPillContext" Header="Edit Pill" IsEnabled="False" Click="EditPill"/>
                                <MenuItem Name="RemovePillContext" Header="Remove Pill" IsEnabled="False" Click="RemovePill"/>
                                <MenuItem Name="PillTakenContext" Header="Pill Taken" IsEnabled="False" Click="PillTakenEvent"/>
                            </ContextMenu>
                        </ListView.ContextMenu>
                    </ListView>
                </Grid>
            </TabItem>
            <TabItem Header="Settings">
                <Grid Background="#FFE5E5E5">
                    <Label x:Name="SettingsLabel" Content="Settings" HorizontalAlignment="Left" Margin="279,10,0,0" VerticalAlignment="Top" Width="156" Height="68" FontSize="36"/>
                    <GroupBox x:Name="SettingsGroupBox" Header="Common Settings" HorizontalAlignment="Left" Height="319" Margin="10,83,0,0" VerticalAlignment="Top" Width="775">
                        <Grid HorizontalAlignment="Left" Height="284" VerticalAlignment="Top" Width="765" Margin="0,0,-2,-4">
                            <CheckBox x:Name="SavePillsCheckBox" Content="Save Pills To External File(Recommended)" HorizontalAlignment="Left" Margin="24,236,0,0" VerticalAlignment="Top" Checked="SavePillsChecked_Changed" Unchecked="SavePillsChecked_Changed"/>
                            <Label x:Name="ChooseSoundLabel" Content="Choose Alert Sound:" HorizontalAlignment="Left" Margin="24,47,0,0" VerticalAlignment="Top"/>
                            <RadioButton x:Name="ArmyTrumpetSoundBtn" Content="Army Trumpet" HorizontalAlignment="Left" Margin="24,93,0,0" VerticalAlignment="Top" IsChecked="True" Checked="SoundCheck_Changed"/>
                            <RadioButton x:Name="DizzySoundBtn" Content="Dizzy Sound" HorizontalAlignment="Left" Margin="166,93,0,0" VerticalAlignment="Top" Checked="SoundCheck_Changed"/>
                            <CheckBox x:Name="StartUpCheckBox" Content="Start Program at Windows Startup" HorizontalAlignment="Left" Margin="24,164,0,0" VerticalAlignment="Top" Width="211" Checked="StartupChecked_Changed" Unchecked="StartupChecked_Changed"/>
                        </Grid>

                    </GroupBox>
                </Grid>
            </TabItem>
            <TabItem Header="About">
                <Grid Background="#FFE5E5E5">
                    <GroupBox x:Name="groupBox" Header="About Information" Height="319" Margin="10,83,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="775">
                        <TextBlock x:Name="AboutTextBlock" HorizontalAlignment="Left" Margin="29,27,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="190" Width="636">Revolution's Pill Reminder is an open source program under the MIT license. The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. <LineBreak/><LineBreak/> Credits: <LineBreak/>Copyright (c) 2016 MahApps</TextBlock>
                    </GroupBox>
                    <Label x:Name="AboutLabel" Content="About" HorizontalAlignment="Left" Margin="279,10,0,0" VerticalAlignment="Top" Width="156" Height="68" FontSize="36"/>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Controls:MetroWindow>



P.S For some reason the format of the xaml code has been spoiled using the code tags. I don't know how to dix it :(

Is This A Good Question/Topic? 0
  • +

Replies To: Opinion about my XAML code

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 83
  • View blog
  • Posts: 359
  • Joined: 12-January 11

Re: Opinion about my XAML code

Posted 28 March 2017 - 08:34 AM

I don't understand what you are asking. Is there something specific you are trying to get answered?

If you are just asking for an opinion (which there are many), all I can say is that I can't stand it when people use the View code behind to apply data to the view. I much prefer the MVVM methodology where you create a ViewModel for each View and bind the View to the properties of the ViewModel. Then, you only have to add one line of code to the code behind for the main view (unless you are using MEF). Most other views/user controls can have their DataContext set using binding.

DataContext = new FooViewModel();



This creates a good separation of concerns between the View, the data for the view, the business logic, and the data access layer.
Was This Post Helpful? 1
  • +
  • -

#3 Altair128  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 25
  • Joined: 03-May 11

Re: Opinion about my XAML code

Posted 28 March 2017 - 09:00 AM

Agree with Psyguy regarding using MVVM which will also allow you to unit test your ViewModels. Regarding creating the ViewModel, have a look at the PRISM framework which can automatically create the viewmodel based on a naming convention so no code behind needed at all.

See this tutorial on PRISM, https://msdn.microso...=pandp.40).aspx specifically the section on Creating the View Model Using a View Model Locator.

Good Luck!
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: Opinion about my XAML code

Posted 28 March 2017 - 01:00 PM

This looks like a 1998 WinForms coder jumped into XAML as a new way to make UI - without reading anything about XAML/WPF/MVVM. No use of any modern principals: Its WinForms written in XAML, with no actual WPF concepts.

Everything has names: That's always a big red flag.
Every property is hard coded such as IsVisible, IsChecked - instead of being binded to a value on the ViewModel (as others have pointed out)

Clearly done by drag-n-drop based on the totally screwy margin and sizes.

If you want to hit my tutorial site there are articles on MVVM, design pratices, reducing repitition by defining Styles and so on. The site is for Xamarin instead of WPF, but its all XAML/MVVM: Just using Xamarin instead of WPF as the foundation technology so it can be cross platform instead of Windows-only. But its all same-same. Just a StackLayout intead of a StackPanel etc., but that shouldn't throw off too many people.

This post has been edited by tlhIn`toq: 28 March 2017 - 01:05 PM

Was This Post Helpful? 1
  • +
  • -

#5 JohnRipper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-March 17

Re: Opinion about my XAML code

Posted 28 March 2017 - 03:08 PM

View PosttlhIn`toq, on 28 March 2017 - 01:00 PM, said:

This looks like a 1998 WinForms coder jumped into XAML as a new way to make UI - without reading anything about XAML/WPF/MVVM. No use of any modern principals: Its WinForms written in XAML, with no actual WPF concepts.

Everything has names: That's always a big red flag.
Every property is hard coded such as IsVisible, IsChecked - instead of being binded to a value on the ViewModel (as others have pointed out)

Clearly done by drag-n-drop based on the totally screwy margin and sizes.

If you want to hit my tutorial site there are articles on MVVM, design pratices, reducing repitition by defining Styles and so on. The site is for Xamarin instead of WPF, but its all XAML/MVVM: Just using Xamarin instead of WPF as the foundation technology so it can be cross platform instead of Windows-only. But its all same-same. Just a StackLayout intead of a StackPanel etc., but that shouldn't throw off too many people.

Yeah... i am clearly guilty here. Ofcourse i jumped from winforms. If i was an expert, i will never had ask for you opinions for my code :P Anyway. I saw you your website.. but since i want to read from multiple resources at the same time(it's just my way), are these two websites good for learning XAML(because this is my problem if i understand correctly): www.tutorialspoint.com/xaml and http://www.wpf-tutor...rted/hello-wpf/ And one more question, which is WPF-relevant, but not topic-relevant. I saw somewhere, that if you become a good (XAML)Designer, this will help you after, creating web applications(which is what i am interested to do, because everyone asks for web devs). Is that true ?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: Opinion about my XAML code

Posted 29 March 2017 - 03:15 AM

Tutorialspoint.com - I like their site and their articles. They are clean and concise. I've pointed people there in the past to see how a specific thing was done. There are plenty times when you just need to see the syntax of how to do something and they are great for that. I see them sort of like an on-line dictionary for XAML: When you have a need to look up a thing you can go there and see the definition and an example.
I don't know the other one.

But what you need when starting out is a *course* not a dictionary. A guided instructional series that takes you through the *why* and foundation concepts... explains how XAML/MVVM is different from WinForms. Its the difference between 'instruction' and 'definition'. For this purpose you can't beat a good WPF textbook. You're going back to school basically and should approach it as such. Think of it this way... If you wanted to learn Russian would you read a dictionary cover to cover, or take a course in Russian where you learn the syntax and structure - latter looking up things in the dictionary when you had a foundational understanding of the overall system?

Jumped in - What I mean by that is that you just assumed everything from your WinForm days would transfer over to XAML. "Its just the same, only in a different language" kind of thinking. That just isn't the case. You "jumped in" and started banging on the keyboard, rather than picking up a book and reading about this new technology that is about 15+ years ahead of WinForms. Kind of like saying... "I can drive a car. I'll jump in the cockpit of a plane and teach myself flying by just doing it - its not that much different"

I would suggest hitting up Amazon for a couple good "Learn WPF/MVVM in 60 days" type books and work them cover to cover before trying to make any program from scratch. That will get you the most conceptual information in the shortest amount of time. From there you build easy applications and work up; just like you did learning WinForms. Beleive it or not, most of your WinForms really doesn't carry over. The C# does, but that only handles the logic. Everything UI is different. All the dirty entanglement between UI and logic from WinForms should not carry over to your WPF architecture: If it does you're not doing it right. Once you have a book or two under your belt then you'll be able to evaluate the myriad of websites for your own needs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1