Bonjour
Je voudrais savoir comment faire pour que le changement d'une propriété public (ma_propri) dans une classe public (ma_classe) déclenche un événement sur une page (Mainpage) ?
Merci.
Version imprimable
Bonjour
Je voudrais savoir comment faire pour que le changement d'une propriété public (ma_propri) dans une classe public (ma_classe) déclenche un événement sur une page (Mainpage) ?
Merci.
La classe va déclencher un évènement tout court, pas un évènement "sur une page". C'est le principe du pattern Observateur : l'objet observé n'a pas à connaître à l'avance tous les observateurs possibles.
Donc il faut juste que Mainpage s'abonne à l'évènement, et ta classe ma_classe n'a pas à se préoccuper de Mainpage : elle déclenche juste l'évènement, ce qui va notifier tous ceux qui sont abonnés.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 class MaClasse : INotifyPropertyChanged { private int _maPropriete; public int MaPropriete { get { return _maPropriete; } set { _maPropriete = value; OnPropertyChanged("MaPropriete"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } }
j'ai fais ça mais l'application bugggggg
voici ma classe static
Code:
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 namespace test_passage_parametre { public class passage : System.ComponentModel.INotifyPropertyChanged { private string _xxx; public string xxx { get { return _xxx; } set { _xxx = value; OnPropertyChanged("xxx"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs("xxx")); } private static passage _Instance; public static passage Instance { get { if (_Instance == null) _Instance = new passage(); return _Instance; } } } }
ma page MainPage (le constructeur)
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public MainPage() { InitializeComponent(); this.loginContainer.Child = new LoginStatus(); passage mypassage = passage.Instance; var tt = mypassage.xxx.ToString(); if (tt == "mehdi") { hyperlinkButton1.Visibility = System.Windows.Visibility.Visible; } }
ma page Home (un bouton dans la page home)
Code:
1
2
3
4
5
6
7
8
9
10
11
12 private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { passage mypassage = passage.Instance; mypassage.xxx = "mehdi"; }
le sénario c dans la page home quand je clik le bouton un bouton sur Mainpage soit visible mais on utilisant la classe passage.cs
L'interface INotifyPropertyChanged est utilisée quand on fait du binding. Toi tu testes simplement la valeur de passage.xxx dans le code, tu ne prends pas en compte l'évènement.
Dans ton cas l'utilisation d'un binding est sans doute la solution la plus simple, par contre il faut créer un converter qui va renvoyer Visible ou Hidden en fonction de la valeur.
Par exemple :
Et tu l'utilises comme ça :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 [ValueConversion(typeof(string), typeof(Visibility))] public class NameToVisibilityConverter : IValueConverter { public string Name { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string s = (string)value; if (value == Name) return Visibility.Visible; return Visibility.Hidden; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } }
Code:
1
2
3
4
5
6
7 ... <Page.Resources> <my:NameToVisibilityConverter x:Key="nameToVisi" Name="mehdi" /> </Page.Resources> ... <HyperlinkButton Visibility="{Binding xxx, Source={x:Static passage.Instance}, Converter={StaticResource nameToVis}}" />
il me déclare une erreur pour "ValueConversion" ???
Code:
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 namespace test_passage_paramaitre2 { using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using test_passage_paramaitre2.LoginUI; using System.Windows.Data; using System.Globalization; using System; /// <summary> /// <see cref="UserControl"/> class providing the main UI for the application. /// </summary> public partial class MainPage : UserControl { /// <summary> /// Creates a new <see cref="MainPage"/> instance. /// </summary> public MainPage() { InitializeComponent(); this.loginContainer.Child = new LoginStatus(); passage mypassage = passage.Instance; var tt = mypassage.xxx.ToString(); //if (tt == "mehdi") //{ // Link2.Visibility = System.Windows.Visibility.Visible; //} } /// <summary> /// After the Frame navigates, ensure the <see cref="HyperlinkButton"/> representing the current page is selected /// </summary> private void ContentFrame_Navigated(object sender, NavigationEventArgs e) { foreach (UIElement child in LinksStackPanel.Children) { HyperlinkButton hb = child as HyperlinkButton; if (hb != null && hb.NavigateUri != null) { if (hb.NavigateUri.ToString().Equals(e.Uri.ToString())) { VisualStateManager.GoToState(hb, "ActiveLink", true); } else { VisualStateManager.GoToState(hb, "InactiveLink", true); } } } } /// <summary> /// If an error occurs during navigation, show an error window /// </summary> private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { e.Handled = true; ErrorWindow.CreateNew(e.Exception); } } [ValueConversion(typeof(string), typeof(Visibility))] public class NameToVisibilityConverter : IValueConverter { public string Name { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string s = (string)value; if (value == Name) return Visibility.Visible; return Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } }
Il te manque un using:
Code:using System.Windows.Data;
j'ai fais
mais il signale toujour une erreur sur [ValueConversion(typeof(string), typeof(Visibility))] malgré que il ya using System.Windows.Data; ???????
Code:
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 using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using test_passage_paramaitre.LoginUI; using System.Windows.Data; using System.Globalization; using System; [ValueConversion(typeof(string), typeof(Visibility))] public class NameToVisibilityConverter : IValueConverter { public string Name { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string s = (string)value; if (value == Name) return Visibility.Visible; return Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } }
Par contre il y a une erreur dans mon code... il faut remplacer cette ligne :
Par ça :Code:if (value == Name)
Code:if (s == Name)