25 abril, 2024

Formulario de Inicio con WPF

Por JL Mota

Windows Presentation Foundation (WPF) es una tecnología de Microsoft, presentada como parte de Windows Vista. Permite el desarrollo de interfaces de interacción en Windows tomando características de aplicaciones Windows y de aplicaciones web.

WPF ofrece una amplia infraestructura y potencia gráfica con la que es posible desarrollar aplicaciones visualmente atractivas, con facilidades de interacción que incluyen animación, vídeo, audio, documentos, navegación o gráficos 3D. Separa, con el lenguaje declarativo XAML y los lenguajes de programación de .NET, la interfaz de interacción de la lógica del negocio, propiciando una arquitectura Modelo Vista Controlador para el desarrollo de las aplicaciones.

  1. Crear un proyecto nuevo con WPF y C#. Y estructurar las carpetas del proyecto de la siguiente manera.

2. En la carpeta View, crear una nueva pagina WPF llamada LoginView.xaml

3. Agregar el siguiente código :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<Window x:Class="WPF_LoginForm.View.LoginView"
        xmlns:local="clr-namespace:WPF_LoginForm.View"
        mc:Ignorable="d"
        Title="LoginView" Height="550" Width="800"
        WindowStyle="None"
        ResizeMode="NoResize"
        WindowStartupLocation="CenterScreen"
        Background="Transparent"
        AllowsTransparency="True"
        MouseDown="Window_MouseDown">
 
    <Border CornerRadius="12">
        <Border.Background>
            <ImageBrush ImageSource="/Images/ITL.jpeg"
                        Stretch="Fill"/>
        </Border.Background>
 
        <Border CornerRadius="10"                   
            BorderThickness="2"
            Opacity="0.95">
 
            <Border.BorderBrush>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="#462AD8" Offset="0"/>
                    <GradientStop Color="#DA34AE" Offset="0.75"/>
                    <GradientStop Color="#8A16C1" Offset="1"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
 
            <Border.Background>
                <LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
                    <GradientStop Color="#060531" Offset="0"/>
                    <GradientStop Color="#1B1448" Offset="1"/>
                </LinearGradientBrush>
            </Border.Background>
 
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
 
                <Grid Grid.Row="0">
 
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="25"/>
                        <ColumnDefinition Width="25"/>
                        <ColumnDefinition Width="5"/>
                    </Grid.ColumnDefinitions>
 
                    <TextBlock Text="Acceso"
                               Foreground="DarkGray"
                               FontSize="10"
                               FontFamily="Montserrat"
                               Grid.Column="0"
                               VerticalAlignment="Center"
                               Margin="10,0,0,0"/>
 
                    <Button x:Name="btnMinimize"                          
                            BorderThickness="0"
                            Content="-"
                            Foreground="White"
                            FontSize="16"
                            FontFamily="Montserrat"
                            Cursor="Hand"
                            Grid.Column="1"
                            Click="btnMinimize_Click">
 
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Background" Value="#28AEED"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="#278BEF"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
 
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Border Width="18" Height="18"
                                        CornerRadius="9"
                                        Background="{TemplateBinding Background}">
                                    <ContentPresenter VerticalAlignment="Center"
                                                      HorizontalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
 
                    </Button>
 
                    <Button x:Name="btnClose"                         
                            BorderThickness="0"
                            Content="X"
                            Foreground="White"
                            FontSize="12"
                            FontFamily="Montserrat"
                            Cursor="Hand"
                            Grid.Column="2"
                            Click="btnClose_Click">
 
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Background" Value="#DA34AE"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="#C62DAE"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
 
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Border Width="18" Height="18"
                                        CornerRadius="9"
                                        Background="{TemplateBinding Background}">
                                    <ContentPresenter VerticalAlignment="Center"
                                                      HorizontalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </Grid>
 
                <StackPanel Width="220"
                            Grid.Row="1"
                            Orientation="Vertical"
                            Margin="0,35,0,0">
 
                    <Image Source="/Images/Logo.png"
                           Width="100" Height="100"/>
 
                    <TextBlock Text="El Blog de Mota"
                               Foreground="White"
                               FontSize="25"
                               FontWeight="Medium"
                               FontFamily="Montserrat"
                               HorizontalAlignment="Center"/>
 
                    <TextBlock Text="Aprendiendo a utilizar WPF con un acceso."
                               Foreground="LightGray"
                               FontSize="12"
                               FontWeight="Medium"
                               FontFamily="Montserrat"
                               TextWrapping="Wrap"
                               TextAlignment="Center"
                               Margin="0,5,0,0"/>
 
 
                    <TextBlock Text="Usuario"
                               Foreground="DarkGray"
                               FontSize="12"
                               FontWeight="Medium"
                               FontFamily="Montserrat"                            
                               Margin="0,35,0,0"/>
 
                    <TextBox x:Name="txtUser"
                             FontSize="13"
                             FontWeight="Medium"
                             FontFamily="Montserrat"                           
                             Foreground="White"
                             CaretBrush="LightGray"
                             BorderBrush="DarkGray"
                             BorderThickness="0,0,0,2"
                             Height="28"
                             VerticalContentAlignment="Center"
                             Margin="0,5,0,0"
                             Padding="20,0,0,0">
 
                        <TextBox.Background>
                            <ImageBrush ImageSource="/Images/user-icon.png"
                                        Stretch="None"
                                        AlignmentX="Left"/>
                        </TextBox.Background>
                    </TextBox>
 
                    <TextBlock Text="Password"
                               Foreground="DarkGray"
                               FontSize="12"
                               FontWeight="Medium"
                               FontFamily="Montserrat"                            
                               Margin="0,15,0,0"/>
 
                    <PasswordBox x:Name="txtPass"
                             FontSize="13"
                             FontWeight="Medium"
                             FontFamily="Montserrat"                           
                             Foreground="White"
                             CaretBrush="LightGray"
                             BorderBrush="DarkGray"
                             BorderThickness="0,0,0,2"
                             Height="28"
                             VerticalContentAlignment="Center"
                             Margin="0,5,0,0"
                             Padding="20,0,0,0">
 
                        <PasswordBox.Background>
                            <ImageBrush ImageSource="/Images/key-icon.png"
                                        Stretch="None"
                                        AlignmentX="Left"/>
                        </PasswordBox.Background>
                    </PasswordBox>
 
                    <Button x:Name="btnLogin"                         
                            BorderThickness="0"
                            Content="Acceder"
                            Foreground="White"
                            FontSize="12"
                            FontFamily="Montserrat"
                            Cursor="Hand"                          
                            Margin="0,50,0,0"
                            Click="btnLogin_Click">
 
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Background" Value="#462AD8"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="#28AEED"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
 
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Border Width="150" Height="40"
                                        CornerRadius="20"
                                        Background="{TemplateBinding Background}">
                                    <ContentPresenter VerticalAlignment="Center"
                                                      HorizontalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
 
                    <StackPanel Orientation="Horizontal"
                                HorizontalAlignment="Center"
                                Margin="0,15,0,0">
 
                        <TextBlock Text="Olvidaste el password?"
                               Foreground="DarkGray"
                               FontSize="12"
                               FontWeight="Medium"
                               FontFamily="Montserrat"/>
 
                        <TextBlock Text="Reiniciar"
                               Foreground="White"
                               FontSize="12"
                               FontWeight="Medium"
                               FontFamily="Montserrat"
                               Cursor="Hand"
                               Margin="8,0,0,0"/>
 
                    </StackPanel>
 
                </StackPanel>
 
            </Grid>
 
        </Border>
         
    </Border>
 
</Window>

4. Modificar el archivo App.xaml para que la aplicación pueda ejecutar como pagina de inicio, la nueva ventana que acabamos de crear. En este caso modificamos la etiqueta StartupUri y la apuntamos a nuestro archivo LoginView.xaml

Dejo el siguiente link para descargar el proyecto de GitHub.

https://github.com/Instituto-Tecnologico-de-la-Laguna/WPF-LoginForm.git