How to change Fontawesome.wpf Icon in c# code - c#

I'm fairly new to programming in wpf, so this might be pretty basic, but I have no Idea how to change a FontAwesome Icon of a button dynamically (in c# code). I use the nuget package as described in this post here. However the last part:
And finally in your C# code behind:
using FontAwesome.WPF; // on the top of the code
btnButton.Content = FontAwesomeIcon.LongArrowRight;
is not working for me, as it only shows the text "LongArrowRight" after clicking the button.
This is my code:
<Window
...
xmlns:fa="http://schemas.fontawesome.io/icons/">
<Grid>
<Button x:Name="btnPlay" Click="btnPlay_Click">
<Button.Content>
<fa:ImageAwesome Icon="Play"/>
</Button.Content>
</Button>
</Grid>
</Window>
using FontAwesome.WPF; // at the top
private bool running = false;
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
if (running)
{
running = false;
btnPlay.Content = FontAwesomeIcon.Play;
}
else
{
running = true;
btnPlay.Content = FontAwesomeIcon.Stop;
}
}
As already stated when the button is clicked it does not show the Icon but only the text "Stop" and when pressed again "Play".

FontAwesomeIcon is enum. When you assign enum value to Button.Content it will be displayed as text.
You need to assign ImageAwesome element to Content:
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.LongArrowRight };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Play };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Stop };

Related

MahApps adding controlzex:ToolTipAssist.AutoMove programatically

I'm adding buttons to a Grid programatically like the code below in a MahApps Metro program. This works fine and the buttons are visible and clickable.
var tooltip = new ToolTip()
{
Content = "ToolTip Text"
};
var button = new Button()
{
Name = "Button1",
Focusable = false,
ToolTip = tooltip,
Content = "ClickMe"
};
button.Click += Button1_Click;
Grid.SetRow(button, 2);
Grid.SetColumn(button, 5);
MainGrid.Children.Add(button);
But I want to use the ToolTipAssist.AutoMove. In the designer you add it like this
<Button Name="Button1" Content="ClickMe">
<Button.ToolTip>
<ToolTip controlzex:ToolTipAssist.AutoMove="True" Content="ToolTip Text" />
</Button.ToolTip>
</Button>
But I haven't been able to add it in code. using ControlzEx; is present in the code. I've tried:
tooltip.controlzex //controlzex does not exist
tooltip.ToolTipAssist //ToolTipAssist does not exist
tooltip.AutoMove //AutoMove does not exist
So I'm thinking it has to be an event that has to be attached somehow, but which one?
The AutoMove property is an attached property, that exposes these methods:
public static bool GetAutoMove(ToolTip element) { //... }
public static void SetAutoMove(ToolTip element, bool value) { //... }
You set AutoMove like this to the tooltip in your code:
ToolTipAssist.SetAutoMove(tooltip, true);

Why is the TitleBar title gone after adding acrylic effect?

I used the following code in MainPage.xaml.cs to add acrylic effect on the TitleBar
public void AcrylicTitleBar()
{
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
var title = ApplicationView.GetForCurrentView();
title.TitleBar.ButtonBackgroundColor = Colors.Transparent;
title.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
title.TitleBar.ButtonForegroundColor = (Color)Resources["SystemBaseHighColor"];
}
but I don't know why the TitleBar title is gone.
If I remove the above code the TitleBar title comes back. can someone please help me?
Your code is not applying any "Acrylic" effect to your app Title bar .
It is simply setting the button background color to "Transparent" which is no where close to the acrylic.
In order to have an acrylic title bar you need to do the following :
In your MainPage.xaml.cs , add code for hiding the default title bar and update its layout so that it still works like the default toolbar :
public MainPage()
{
// Hide default title bar.
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;
UpdateTitleBarLayout(coreTitleBar);
}
private void UpdateTitleBarLayout(CoreApplicationViewTitleBar coreTitleBar)
{
// Get the size of the caption controls area and back button
// (returned in logical pixels), and move your content around as necessary.
LeftPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset);
RightPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayRightInset);
TitleBarButton.Margin = new Thickness(0, 0, coreTitleBar.SystemOverlayRightInset, 0);
// Update title bar control size as needed to account for system size changes.
AppTitleBar.Height = coreTitleBar.Height;
}
private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
UpdateTitleBarLayout(sender);
}
Next , in your MainPage.xaml add AcrylicBrush as the main or parent grid background (Note : that this grid should wrap all the contents in the page ):
<Grid>
<Grid.Background>
<AcrylicBrush BackgroundSource="HostBackdrop"
TintColor="{ThemeResource SystemAltHighColor}"
FallbackColor="{ThemeResource SystemAltHighColor}"
TintOpacity="0.5">
</AcrylicBrush>
</Grid.Background>
</Grid>
Hope this helps!

How to read image source or image name in xamarin.forms?

I am working on Xamarin forms, where I have used TapGestureRecognizer inside image now on tap of that image I need to get the source of the image in c#. How do I get that?. Why I am doing this is, radio buttons are not available in Xamarin forms, On tap of the image I will check the source of the image if the source is checked image then I need to change the source to unchecked and vice versa.
Here is my XAML code
<Image Scale="0.7" HorizontalOptions="Start" x:Name="radioButton" Source="unchecked.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="radioButton_Clicked">
</TapGestureRecognizer>
</Image.GestureRecognizers>
</Image>
Here is my C# code
private void radioButton_Clicked(object sender, EventArgs e)
{
var imageSource = "";//get image source here
if (imageSource == "Checked.png")
{
radioButton.Source = "Unchecked.png";
}
else
{
radioButton.Source = "Checked.png";
}
}
you can achieve like this on radioButton_Clicked action
private void radioButton_Clicked(object sender, EventArgs e)
{
var imageSource = (Image)sender;//get image source here
var selectedImage = imageSource.Source as FileImageSource;
if (selectedImage.File == "Checked.png")
{
radioButton.Source = "Unchecked.png";
}
else
{
radioButton.Source = "Checked.png";
}
}
or you can use the custom plugin for support checked box refer this
https://github.com/XLabs/Xamarin-Forms-Labs
you can cast sender to image control
var imageSender = (Image)sender;
gestures

Change language for dialog after overriding language?

My code in MainPage.cs
ApplicationLanguages.PrimaryLanguageOverride = "ja-jp";
XAML
<Button content="Click" Click="Button_Click" />
after this i've opened dialog with my code
private async void Button_Click(object sender, RoutedEventArgs e)
{
{
testDialog dialog = new testDialog();
await dialog.ShowAsync();
}
}
My testDialog's XAML code
<TextBlock x:Uid="TestTextBlock" />
i've define the language's text in Resources.resw file, it worked fine if i put textblock in current MainPage but when i put it in dialog the textblock's text doesn't change, It only change after i reset the application. Any ideas how can i fix this guys ?'
Setting new language:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "ja-jp";
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
Reload Current page:
private bool Reload(object param = null)
{
var type = Frame.CurrentSourcePageType;
try
{
return Frame.Navigate(type, param);
}
finally
{
Frame.BackStack.Remove(Frame.BackStack.Last());
}
}
Also you can use Frame.Navigate(this.GetType()); for refreshing current page UI.
please take a look at this post for more information: Dynamically change the language of a universal app

AppBar Button Changing icon dynamically

I have a button on appBar. When user clicks it, it should change the icon. It will either show the Map or List so it needs to toggle between those 2.
If I use the code below with icons Symbol.Play & Symbol.Stop it toggles perfectly.
But when I use Symbol.Map & Symbol.List it does not toggle properly. (On simulator at least.) It changes sometimes, but mostly stays the same icon.
Code:
private void MapToggle_Click(object sender, RoutedEventArgs e)
{
if (JobMap.Visibility != Visibility.Visible)
{
MapToggle.Icon = new SymbolIcon(Symbol.List);
MapToggle.Label = "List";
JobMap.Visibility = Visibility.Visible;
}
else
{
MapToggle.Icon = new SymbolIcon(Symbol.Map);
MapToggle.Label = "Map";
JobMap.Visibility = Visibility.Collapsed;
}
}
Binded to XAML
AppBarButton Name="MapToggle" Icon="Map" Click="MapToggle_Click" Label="Map"

Categories

Resources