我只是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