简体   繁体   中英

C# WPF application: What is “this”?

I have been playing a lot with WPF applications in C# and there are a lot of things that are not really clear to me, I have been trying to look it up and play around with it to figure it out but without much success since english is my second tongue and I am still not that good with terminology nor with programming...

1: What is "this" in the main class? When I create the new WPF application in XAML I get window and a grid. However, I dislike XAML greatly and like to write code for all the elements and objects that I need so I delete that first grid, make a class, define my grid and to add it I have to write something like

   this.AddChild(myGrid);

which is fine, but if I want to use "this" from my main class in other classes, it becomes a bit complicated to me. So, which UIElement or Object is "this"? How do I define it so it can be used in methods? "this", I suppose refers to the Window that is created at beginning, but what UIElement or Object is that Window?

2: Extended classes?? I have been watching a lot of java tutorials lately, simply to learn more about programming. There, to use the objects from other class you can simply write:

   public class class1 extends class2{}

and everything is perfect, I have found out that I can mimic that same thing in C# WPF unless it's the main class, since main class extends :Window and I guess since it's defined as partial class... Is there a way to "extend" multiple classes or go around this?

Any help on clearing this up would be great :)

You should learn Object Oriented Programming in C#

  1. this means the current instance of the class. So in each class this refers to a different object. this can usually be omitted and just AddChild(myGrid) can be used.
  2. extends (or : in C#) means that the first class ( class1 ) inherits from the second ( class2 ) thus having access to methods and variables that are defined in class2 that are not marked private .

For the part about 'this' and its identity, the Window sits in a hierarchy of classes and can assume the identity of any of its ancestors. For example...

    public MainWindow()
    {
        InitializeComponent();
        var contentControl = this as ContentControl;
        var control = this as Control;
        var frameworkElement = this as FrameworkElement;
        var uiElement = this as UIElement;
        var dependencyObject = this as DependencyObject;
        var dispatcher = this as DispatcherObject;
    }

...all of the assignments in this snippet are legal. Also, there are more exotic assignments such as

var x = this as IInputElement;

The key here is to examine the framework and the various assignments available to each class. As others have pointed out, offline reading is essential to a quick learning curve.

The etymology of 'this' as a keyword in an object oriented context extends back to the late 1970's when it first appeared in an early specification for C++.

Finally, Xaml is one of the most attractive features of WPF for lots of reasons, and if Xaml isn't compatible with your approach, you MIGHT be better off in WinForms or Swing or similar tightly bound framework.

Simply said this is the class you are in.

For an example

class dim
{
    int sum = 0;
    public void num(int sum){
        this.sum = sum; //Places the sum from num to the sum in dim
    }
}

Extending a class is basically termed as Inheritance in Object Oriented Programming. There are several types of inheritance like single,multiple,multi-level,hierarchial,hybrid.But C# and also Java doesn't support inhertance from more than one class, because multiple inheritance creates a lot of ambiguity.

A feature that replaces multiple inheritance is the use of interfaces. Instead of 'extending from a class' we 'implement an interface' using the keyword 'implements'.An interface is just a skeleton class where you declare method signatures and the interface will be implemented in the class where you 'implement the interface'.The important point is you can implement more than one interface

To get an overview about Inheritance and Interfaces,the following link would be helpful:

http://msdn.microsoft.com/en-us/library/ms228387(v=vs.80).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM