-
May 14th, 2021, 03:09 AM
#1
Thread Starter
New Member
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.
-
May 14th, 2021, 04:14 AM
#2
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.
-
May 14th, 2021, 05:29 AM
#3
Re: Trying to understand C# XAML
Originally Posted by Dim Wit
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.
-
May 14th, 2021, 10:31 PM
#4
Thread Starter
New Member
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.
-
May 18th, 2021, 04:19 PM
#5
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|