Bonjour,
je viens de me mettre à MVVM light sur une solution windows universal apps et je voulais savoir comment gérer la navigation entre les vues.
Actuellement j'ai fais ça dans le ViewModel de ma première page qui sera le menu de l'application:
Mon viewModelAncestor au cas où:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class MenuViewModel : ViewModelAncestor { public RelayCommand<string> NavigateTo { get; set; } public MenuViewModel() { NavigateTo = new RelayCommand<string>(Goto); } private void Goto(string page) { switch (page) { case "Simulator": this.Frame.Navigate(typeof(SimulatorPage)); break; default: this.Frame.Navigate(typeof(SimulatorPage)); break; } } }
Et ma vue (j'ai pas encore ajouté les commandes parameter :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class ViewModelAncestor : ViewModelBase { public Frame Frame { get { return ((Frame)Window.Current.Content); } } #region Prise en charge de la navigation RelayCommand _goBackCommand; RelayCommand _goForwardCommand; /// <summary> /// <see cref="RelayCommand"/> utilisée pour la liaison à la propriété Command du bouton Précédent /// pour accéder à l'élément le plus récent de l'historique de navigation vers l'arrière, si un frame /// gère son propre historique de navigation. /// /// La commande<see cref="RelayCommand"/> est configurée pour utiliser la méthode virtuelle <see cref="GoBack"/> /// comme action d'exécution et <see cref="CanGoBack"/> pour CanExecute. /// </summary> public RelayCommand GoBackCommand { get { if (_goBackCommand == null) { _goBackCommand = new RelayCommand( () => this.GoBack(), () => this.CanGoBack()); } return _goBackCommand; } set { _goBackCommand = value; } } /// <summary> /// <see cref="RelayCommand"/> utilisée pour accéder à l'élément le plus récent du /// l'historique de navigation avant, si un frame gère son propre historique de navigation. /// /// La commande<see cref="RelayCommand"/> est configurée pour utiliser la méthode virtuelle <see cref="GoForward"/> /// comme action d'exécution et <see cref="CanGoForward"/> pour CanExecute. /// </summary> public RelayCommand GoForwardCommand { get { if (_goForwardCommand == null) { _goForwardCommand = new RelayCommand( () => this.GoForward(), () => this.CanGoForward()); } return _goForwardCommand; } } /// <summary> /// Méthode virtuelle utilisée par la propriété <see cref="GoBackCommand"/> /// pour déterminer si le <see cref="Frame"/> peut reculer. /// </summary> /// <returns> /// true si le <see cref="Frame"/> possède au moins une entrée /// dans l'historique de navigation vers l'arrière. /// </returns> public virtual bool CanGoBack() { return this.Frame != null && this.Frame.CanGoBack; } /// <summary> /// Méthode virtuelle utilisée par la propriété <see cref="GoForwardCommand"/> /// pour déterminer si le <see cref="Frame"/> peut avancer. /// </summary> /// <returns> /// true si le <see cref="Frame"/> possède au moins une entrée /// dans l'historique de navigation vers l'avant. /// </returns> public virtual bool CanGoForward() { return this.Frame != null && this.Frame.CanGoForward; } /// <summary> /// Méthode virtuelle utilisée par la propriété <see cref="GoBackCommand"/> /// pour appeler la méthode <see cref="Windows.UI.Xaml.Controls.Frame.GoBack"/>. /// </summary> public virtual void GoBack() { if (this.Frame != null && this.Frame.CanGoBack) this.Frame.GoBack(); } /// <summary> /// Méthode virtuelle utilisée par la propriété <see cref="GoForwardCommand"/> /// pour appeler la méthode <see cref="Windows.UI.Xaml.Controls.Frame.GoForward"/>. /// </summary> public virtual void GoForward() { if (this.Frame != null && this.Frame.CanGoForward) this.Frame.GoForward(); } #endregion }
Le problème c'est que je voudrais pouvoir passer des paramètres d'une page à une autre ou d'un ViewModel à un autre, gérer les différents états de mon application ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <Page x:Class="FlyffSimulator.MenuPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:FlyffSimulator" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{Binding Menu, Mode=TwoWay, Source={StaticResource Locator}}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"></ColumnDefinition> <ColumnDefinition Width="1*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1*"></RowDefinition> <RowDefinition Height="1*"></RowDefinition> </Grid.RowDefinitions> <Grid.Resources> <Style TargetType="Button" > <Setter Property="MaxHeight" Value="300"></Setter> <Setter Property="MaxWidth" Value="300"></Setter> </Style> <Style TargetType="TextBlock" > <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style> <Style TargetType="StackPanel" > <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style> </Grid.Resources> <StackPanel Grid.Column="0" Grid.Row="0" > <Button HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding NavigateTo }" > <Image Source="Assets/84bbb25a-7a8a-4132-ad5b-adcf4691df54.png" ></Image> </Button> <TextBlock x:Uid="Characters" ></TextBlock> </StackPanel> <StackPanel Grid.Column="1" Grid.Row="0" > <Button HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding NavigateTo }" > <Image Source="Assets/84bbb25a-7a8a-4132-ad5b-adcf4691df54.png" ></Image> </Button> <TextBlock x:Uid="Simulator"></TextBlock> </StackPanel> <StackPanel Grid.Column="0" Grid.Row="1" > <Button HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding NavigateTo }" > <Image Source="Assets/84bbb25a-7a8a-4132-ad5b-adcf4691df54.png" ></Image> </Button> <TextBlock x:Uid="Simulator" ></TextBlock> </StackPanel> <StackPanel Grid.Column="1" Grid.Row="1" > <Button HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding NavigateTo }" > <Image Source="Assets/84bbb25a-7a8a-4132-ad5b-adcf4691df54.png" ></Image> </Button> <TextBlock x:Uid="Parameters" ></TextBlock> </StackPanel> </Grid> </Page>
Comment gère-t-on ça avec MVVM light ?
Merci pour vos réponses.
Partager