In my Windows 10 universal application I've Got a AppBarButton in a page:
<AppBarButton x:Name="PinBtn" Icon="Pin" Click="PinBtn_Click"/>
I want to check if Icon="Pin"
then UnPin it and vice versa.
So I tried to code like this:
private void PinBtn_Click(object sender,RoutedEventArgs e)
{
if(PinBtn.Icon == new SymbolIcon((Symbol.Pin)))
{
PinBtn.Icon = new SymbolIcon(Symbol.UnPin);
}
else {
PinBtn.Icon = new SymbolIcon(Symbol.Pin);
}
}
Problem : (PinBtn.Icon == new SymbolIcon((Symbol.UnPin)))
always return FALSE
What's the problem and fix for it?
It's not going to work 'cause Icon
is a reference type. It will never be equal to a new instance of SymbolIcon
.
You should be using a AppBarToggleButton
in this case instead and subscribe to the Checked
and Unchecked
events.
<AppBarToggleButton x:Name="PinToggle" Icon="Pin" Checked="PinToggle_Checked" Unchecked="PinToggle_Unchecked" />
private void PinToggle_Checked(object sender, RoutedEventArgs e)
{
PinToggle.Icon = new SymbolIcon(Symbol.UnPin);
}
private void PinToggle_Unchecked(object sender, RoutedEventArgs e)
{
PinToggle.Icon = new SymbolIcon(Symbol.Pin);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With