我只是C#中的一個新手,我在MSDN博客上閱讀INotifyPropertyChanged Event Handler
並在這裏搜索「stackoverflow」。但我真的不明白如何在代碼中實現它,以及如何將事件和屬性綁定在一起。
不能理解INotifyPropertyChanged的實現
我已經與INotifyPropertyChanged
取得了BindingClass
,代碼爲:
namespace Testing.Pages
{
class BindingClass : INotifyPropertyChanged
{
private string _setting;
public event PropertyChangedEventHandler PropertyChanged;
public BindingClass()
{
}
public BindingClass(string value)
{
_setting = value;
}
public string SettingProperty
{
get { return _setting; }
set
{
_setting = value;
// calling OnPropertyChanged whenever the property gets updated
}
}
protected void OnPropertyChanged([CallerMemberName] string _setting = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(_setting));
}
}
}
SettingsPage.xaml
<TextBlock x:Name="PopupText"
Grid.Row="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,0,0,20"
Text="Your theme will be updated next time you start the app."
TextWrapping="Wrap"
Visibility="Collapsed">
<TextBlock.Resources>
<Storyboard x:Name="popup_animate">
<DoubleAnimation Duration="0:0:2"
Storyboard.TargetName="PopupText"
AutoReverse="True"
From="0.0"
To="1.0"
BeginTime="{x:Bind }"
Storyboard.TargetProperty="(TextBlock.Opacity)"
></DoubleAnimation>
</Storyboard>
</TextBlock.Resources>
</TextBlock>
<TextBlock Text="Change Theme?"
Margin="10,10,0,0"></TextBlock>
<RadioButton x:Name="DarkTheme_btn"
Click="ChangeTheme_btn_Click"
Content="Dark Theme"
Margin="10,0,0,0"
GroupName="theme"></RadioButton>
<RadioButton x:Name="LightTheme_btn"
Click="ChangeTheme_btn_Click"
Content="Light Theme"
Margin="10,0,0,0"
GroupName="theme"></RadioButton>
和代碼隱藏文件SettingsPage.xaml.cs
是:
namespace Testing.Pages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class SettingsPage : Page
{
BindingClass notifyProperty = new BindingClass();
public SettingsPage()
{
this.InitializeComponent();
NavigationCacheMode = NavigationCacheMode.Enabled;
}
private void ChangeTheme_btn_Click(object sender, RoutedEventArgs e)
{
DataContext = notifyProperty;
int notifySettings = 0;
if ((bool)DarkTheme_btn.IsChecked)
{
notifySettings = 2;
AppSettings.saveThemeSettings(notifySettings);
PopupText.Visibility = Visibility.Visible;
popup_animate.Begin();
}
else if ((bool)LightTheme_btn.IsChecked)
{
notifySettings = 1;
AppSettings.saveThemeSettings(notifySettings);
PopupText.Visibility = Visibility.Visible;
popup_animate.Begin();
}
}
}
}
我已經使用了int notifySettings
改變LocalSettingsFolder
應用程序的設置和應用程序每次重新啓動在App.xaml
加載設置。每當我更改設置時,我都會調用另一個class
中的功能,並且它會更改設置,當我單擊SettingsPage.xaml
中的這兩個radiobuttons
中的一個時,將播放animation
。這是舊的方法。
現在我想將這些事件綁定在一起,這樣我就不必使用int notifySettings
和PopupText
動畫,因爲它在每次更新Theme Settings
時都會播放。這是我如何也可以瞭解INotifyPropertyChanged Event
。
int notifySettings
通過一個int
值來相應地更改設置。 1 = LightTheme和2 = DarkTheme。
這裏是Settings Class
:
namespace Testing.Pages
{
class AppSettings
{
public static void saveThemeSettings(int value)
{
ApplicationDataContainer themeSettings = ApplicationData.Current.LocalSettings;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
themeSettings.Values["AppThemeSetting"] = value.ToString();
}
public static string readThemeSettings()
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
string appSettingsString = "error, nothing found";
if (localSettings.Values.ContainsKey("AppThemeSetting"))
{
appSettingsString = localSettings.Values["AppThemeSetting"]?.ToString();
}
return appSettingsString;
}
public static void removeLocalSettings(string settingValue)
{
ApplicationData.Current.LocalSettings.Values.Remove(settingValue);
}
}
}
如果仍然有任何含糊之處,請讓我知道,我可以嘗試進一步解釋。我希望有人能幫助我。
更新:
我根據答案由丹尼Bogers但需要Animation
不啓動,我認爲這是因爲該功能甚至不被稱爲修改了自己的項目。我做了一些更改並嘗試自行完成,但並沒有真正實現,所以我將使用自己的方法來進行更改,直到其他人提出解決方案。
您發表評論'/ /每當屬性得到更新時調用OnPropertyChanged',您需要真正顯示代碼,或者您的真實代碼是否只有該評論? –
我實際上從微軟博客獲得了代碼,我編輯了一下,它寫在那裏,所以我保留了它。但我需要知道如何一起實現這些功能。所以我可以瞭解它。 – Ahmar