TOC

This article is currently in the process of being translated into Slovenian (~49% done).

Aplikacija WPF:

Okno

Pri ustvarjanju aplikacije WPF boste najprej spoznali razred (ang. class) Okno (ang. Window). Služi kot osnova okna in vam nudi standardno obrobo, naslovno vrstico ter gumbe za maksimiranje, zmanjšanje in zapiranje. Okno WPF je kombinacija datoteke XAML (.xaml), kjer je element <Window> koren, in datoteke CodeBehind (.cs). Če uporabljate Visual Studio (Express) in ustvarite novo aplikacijo WPF, bo ustvaril privzeto okno za vas, ki bo videti nekako takole:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>

    </Grid>
</Window>

Atribut x: class datoteki XAML pove, kateri razred naj uporabi, v tem primeru Window1, ki ga je Visual Studio ustvaril za nas. Najdete ga v drevesu projekta v VS kot podrejeno vozlišče datoteke XAML. Privzeto je videti nekako takole:

using System;
using System.Windows;
using System.Windows.Controls;
//…more using statements

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

Kot lahko vidite, je razred Window1 opredeljen kot delen. V času izvajanja je združen z vašo datoteko XAML in tako dobite celotno okno. To dejansko počne ukaz InitializeComponent (), ki zažene polno delujoče okno.

Če se vrnemo k datoteki XAML, boste na elementu Window opazili še nekaj drugih zanimivih atributov, na primer Title, ki določa naslov okna (prikazan v naslovni vrstici) ter začetno širino in višino. Obstaja tudi nekaj definicij imenskega prostora, o katerih bomo govorili v poglavjih XAML.

Opazili boste tudi, da je Visual Studio za nas v oknu ustvaril kontrolnik Mreže (ang. Grid). Mreža je ena od plošč WPF, in čeprav je to lahko katera koli plošča ali nadzor, ima lahko okno samo ENI podrejeni nadzor, zato je v večini primerov dobra izbira Plošča (ang. Panel), ki lahko vsebuje več podrejenih kontrol. Kasneje bomo v tej vadnici veliko podrobneje preučili različne vrste plošč, ki jih lahko uporabite, saj so v WPF zelo pomembne.

Pomembne lastnosti okna

Razred WPF okno ima veliko zanimivih atributov, ki jih lahko uporabite, da nadzorujete izgled in obnašanje vašega okna v aplikaciji. Tukaj je kratek seznam najzanimivejših:

Icon - Z njo lahko določite ikono okna, ki je največkrat prikazana v zgornjem levem kotu, levo od naslova.

ResizeMode - This controls whether and how the end-user can resize your window. The default is CanResize, which allows the user to resize the window like any other window, either by using the maximize/minimize buttons or by dragging one of the edges. CanMinimize will allow the user to minimize the window, but not to maximize it or drag it bigger or smaller. NoResize is the strictest one, where the maximize and minimize buttons are removed and the window can't be dragged bigger or smaller.

ShowInTaskbar - The default is true, but if you set it to false, your window won't be represented in the Windows taskbar. Useful for non-primary windows or for applications that should minimize to the tray.

SizeToContent - Decide if the Window should resize itself to automatically fit its content. The default is Manual, which means that the window doesn't automatically resize. Other options are Width, Height and WidthAndHeight, and each of them will automatically adjust the window size horizontally, vertically or both.

Topmost - The default is false, but if set to true, your Window will stay on top of other windows unless minimized. Only useful for special situations.

WindowStartupLocation - Controls the initial position of your window. The default is Manual, which means that the window will be initially positioned according to the Top and Left properties of your window. Other options are CenterOwner, which will position the window in the center of it's owner window, and CenterScreen, which will position the window in the center of the screen.

WindowState - Controls the initial window state. It can be either Normal, Maximized or Minimized. The default is Normal, which is what you should use unless you want your window to start either maximized or minimized.

There are lots of other attributes though, so have a look for yourself and then move on to the next chapter.