Results 1 to 5 of 5

Thread: Trying to understand C# XAML

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    10

    Trying to understand C# XAML

    I'm trying to learn C# and XAML in WPF and I have some questions.

    1: If I create a listbox in XAML and a listbox in C# are they the same? Isn't XAML supposed to be better in terms of different screen resolutions, more flexibility?

    2: If I have XAML code that makes a label, how am I expected to change things about that label such as the content or height? Am I expected to do it via C#?

    3: I can make a child form in C#, but how do I dynamically put things on that child form? Something like frmchild.mybutton.width = 100;?

    Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,695

    Re: Trying to understand C# XAML

    You seem to be conflating things here. It appears that what you're actually comparing is Windows Presentation Foundation (WPF) and Windows Forms. Both technologies use VB or C# code, but WPF also uses XAML to define the UI. Note that UWP, i.e. Microsoft Store apps, also use XAML.

    If you create a Windows Forms project then you add and configure controls in the designer and that generates C# code. If you create a WPF project then you add and configure controls in the designer and that generates XAML code. The designer is still essentially the same so you still drag and drop controls to create your layout and configure them in the Properties window. You can do certain things directly in the XAML code and that can be easier than using the designer in many cases.

    I suggest that you do some research on WPF and read a tutorial or two. You can use WPF in basically the same way as you did Windows Forms, i.e. handling events and writing code to take actions in those event handlers, but WPF is most effective when you use the Model-View-ViewModel (MVVM) design pattern. In that case, your WPF Windows (the Views) contain as little C# code as possible and all your logic goes into ViewModel classes which are then bound to the Views.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,022

    Re: Trying to understand C# XAML

    Quote Originally Posted by Dim Wit View Post
    I'm trying to learn C# and XAML in WPF and I have some questions.

    1: If I create a listbox in XAML and a listbox in C# are they the same? Isn't XAML supposed to be better in terms of different screen resolutions, more flexibility?

    2: If I have XAML code that makes a label, how am I expected to change things about that label such as the content or height? Am I expected to do it via C#?

    3: I can make a child form in C#, but how do I dynamically put things on that child form? Something like frmchild.mybutton.width = 100;?

    Thanks in advance.
    Welcome to the forums!

    I would suggest you do some research at Microsoft WPF documentation such as Data Binding and XAML. Below are some of the links to get you started.
    Get started with WPF
    Windows Presentation Foundation documentation

    Regards,

    KGC
    Last edited by KGComputers; May 14th, 2021 at 05:41 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    10

    Re: Trying to understand C# XAML

    It appears that what you're actually comparing is Windows Presentation Foundation (WPF) and Windows Forms.
    Windows Forms? I'm trying to make an app using WPF. Unless there's WF in WPF, but I don't think so. Maybe I'm wrong?

    Note that UWP, i.e. Microsoft Store apps, also use XAML.
    I was under the impression that UWP apps are apps that can be used on a variety of devices (TV, laptop) and that a person can still make a WF or WPF app and upload it to the MS Store.

    I suggest that you do some research
    I have done some research and I still have a long way to go. Unfortunately, the videos and websites and pdfs about WPF and XAML that I've looked at don't answer questions 1 and 2, that's why I posted here (although, in terms of question 2, I'm starting to think that XAML is used to set up a sort of template layout that I then adjust in C#) .

    In addition, I just thought I'd tack on question 3 while I was posting because I was hoping someone nice would tell me what the code is and therefore I have one less thing to research.

    Speaking of which, is it just me or do other people find the VS help files overly complex and esoteric?

    And yes I also need to check out MVVM.

    Thanks for the help and the welcome.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,671

    Re: Trying to understand C# XAML

    I'm starting to think that XAML is used to set up a sort of template layout that I then adjust in C#) .
    Close... XAML is a presentation layer. That's it. Nothing more. Just like Windows Forms is a presentation layer (of sorts). XAML doesn't do anything else except describe what the presentation should look like. Nothing more, nothing less. So, yes, if you then need to update the caption of a label, you need to do that in C#. XAML can't do anything more than present the screen to the user any more than HTML can. To make it do anything more, the backing language needs to be applied (C# for XAML, JS for HTML) ... That's what lead to JMC's comment about comparing Windows Forms to WPF ... You're talking about two parts of the same thing as if they were different things. They aren't. XAML and C# go hand in hand. They're two parts of the same formula.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width