I have several image files I want to share between projects(common icons) I have them in an assembly that would be in every solution I create...I have the files in a folder called Icon and I have the build as content copy always. I have verified that a folder is created with these icons...however my other assemblies are not able to find them...
<r:RibbonGroup Header="Users">
<r:RibbonButton >
<r:RibbonButton.LargeImageSource>
<BitmapImage UriSource="..\Icons\UserIcon.png" />
</r:RibbonButton.LargeImageSource>
</r:RibbonButton>
</r:RibbonGroup>
i have tried formatting the uri several ways...but it never succeeds. If the icons are in the actual assembly though they work...
Try using an absolute Uri. Build action must be set to Resource for UserIcon.png
<BitmapImage UriSource="pack://application:,,,/NameOfImageAssembly;component/Icons/UserIcon.png"/>
Relative Uri should also work
<BitmapImage UriSource="/NameOfImageAssembly;component/Icons/UserIcon.png"/>
Related
Sorry for bad describe in the title. I have a image in "C:\Users\aUser\Desktop\Program\Image\Image.png"
But my program is in the same folder with the Image. I can set the Directory manually <Image Source="C:\Users\aUser\Desktop\Program\Image.png" But when the parent directory is moved, the code will no longer work. So how can I set the Source of the image that in the child folder without use of the code behind
Image.Source = new BitmapSource(new Uri(AppDomain.CurrentDomain.BaseDirectory + #"\Image\Image.png")
You will need to be more specific what XAML 'version' you are using (e.g. WPF, UWP or Xamarin.Forms). Anyways, here goes:
UWP
You should read through these docs. Your XAML code could look like this:
<Image Source="ms-appx:///Assets/Image.png"/>
Where Assets/Image.png is a path to your image
Xamarin.Forms
Have a look at these docs. The solution depends on the platform
WPF
Have a look at these docs. Your XAML code could look like this:
<Image>
<Image.Source>
<BitmapImage UriSource="/Images/image.png" />
</Image.Source>
</Image>
I am new in WPF. I want to use Font-awesome Icon in textbox and button. but the icon is not bind with my textbox
I install Font-awesome resource to my application.
Let me know the way how can I use it
Thank You,
I really need it please help me..
Example
Step 1 : Download Font-Awesome
Tools -> Library Package Manager -> Package Manager Console
Install
PM > Install-Package FontAwesome.WPF
Step 2 : Add Resource
<Application> xmlns:fa="http://schemas.fontawesome.io/icons/" </Application>
Step 3 : Put App.xaml
<Application.Resources>
<Style x:Key="FontAwesome">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/fonts/#FontAwesome" />
</Style>
</Application.Resources>
Step 4 : Use it in Demo.xaml
<TextBlock Style="{StaticResource FontAwesome}"
FontSize="75"
Text="" />
Step 5 :- Output
First, download Font Awesome, extract the ZIP file and copy fonts/fontawesome-webfont.ttf into a Fonts folder in your solution. Set the Build Action in the properties to Resource if it isn’t already
Next, add a Style to the Resources in App.xaml. Don’t forget the # at the front of the font name and remember to use the internal name of the font, not the name of the file. To check the name of the font, just double click on the font file and it will open in the Windows Font Viewer. The font name will be at the top.
<Application.resources>
<FontFamily x:Key="FontAwesome">/Fonts/fontawesome-webfont.ttf#FontAwesome</FontFamily>
</Application.resources>
Open MainWindow.xaml and replace the grid with below snippet:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="I" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock x:Name="tbFontAwesome" Text="" FontFamily="{StaticResource FontAwesome}" Foreground="Red" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="Font Awesome" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Grid>
Notice "Text" property of "tbFontAwesome" textblock, its the Unicode for Heart.
Cheat Sheet
To extend the accepted answer because it's somewhat out of date and missing information, here's what I did:
Download FontAwesome
Unzip the archive
Inside the unzipped folder, under the use-on-desktop folder, locate the version you want. N.B. Solid has the most icons free; some icons require a Pro payment for Regular and Light versions.
For me, this was Font Awesome 5 Free-Solid-900.otf.
Following the accepted answer and most tutorials, first create a sub-folder in your C# project named Fonts. Paste the fonts file inside this folder.
I renamed this file FontAwesome.otf for brevity
Set the properties of this file:
Build Action: Resource
Copy to Output Directory: Copy if newer/Copy always
In your App.xaml <Application.Resources> or other <ResourceDictionary>, insert:
<FontFamily x:Key="FontAwesome">/YOUR_PROJECT_NAME;component/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid</FontFamily>
Replace YOUR_PROJECT_NAME with your project name. I include this because it is needed should you use MergedDictionaries across projects.
If you did not place the file in a project sub-folder Fonts, rename or omit this part of the path.
Check that the filename matches: replace FontAwesome.otf with the filename (or rename the file itself).
Check the internal font name. You can do this by following the accepted answer. (Open the .otf or .tff file from explorer to start Windows Font Viewer, copy the font name).
Replace the Font Awesome 5 Free Solid with the font name (after the #).
Do not install the font otherwise you cannot verify that you have followed these steps correctly and the font may not work across computers that do not have the font installed.
I know this is an old question, and this option may not have been available to you at the time, so I thought I would provide an updated method for anyone who stumbles across this. I recommend you install the NuGet package 'FontAwesome5' from here:
FontAwesome5 on NuGet.org
or search "fontawesome5" in Visual Studio's built-in NuGet Package Manager window:
Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
-it is usually the top result when using this exact search term.
Then simply add the following namespace to your XAML document:
xmlns:fa5="http://schemas.fontawesome.com/icons/"
and then you should find relative ease in adding icons to your project. For example:
<fa5:ImageAwesome Icon="Solid_ExclamationTriangle" Foreground="#FFFF7400"/>
or
<fa5:FontAwesome Icon="Solid_ExclamationTriangle" Foreground="#FFFF7400"/>
And the beauty of this is that IntelliSense will show you a list of all available icons, saving you the hassle of going to the website to search them up.
You could as well manually select FontAwesome from the FontFamily property of the TextBlock.That will solve the problem.
If FontAwesome is not among the list of fonts then you probably need to import the font file just like the first answer suggested.
I have a button on a custom control and I'm trying to display an image on it which is defined as a resource in my App.xaml file like so:
<Application.Resources>
<BitmapImage x:Key="PlusSymbol" UriSource="Resources/PlusSymbol.png" />
</Application.Resources>
For some reason I can't use this as a static resource within my custom control's template defined in the Themes\Generic.xaml file, it crashes my application during runtime saying that it cannot find the requested resource. Here's the xaml code I'm using to bind to the resource:
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
It DOES work during runtime if I define the resource the exact same way but within the Generic.xaml file, however it gives me a pre-compiler warning that it can't find the file since it's now looking for it in Themes/Resources/ rather than just in /Resources/. So I get a warning during design time but it works fine in runtime.
So why can't it find my resource when it's defined in App.xaml? I do this the exact same way in a regular WPF project and it works flawlessly, but in this custom control library it is giving me headaches. Any help is much appreciated!
This should work if you switch your StaticResource to DynamicResource so that the resource will be evaluated dynamically at runtime rather than statically. If you switch to DynamicResource.
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{DynamicResource PlusSymbol}"/>
</Button>
I believe this is because of how theme-based styles and templates are handled, as opposed to standard resources. This answer and this answer speak specifically to Generic.xaml and how it is different from other ResourceDictionaries.
So the way I ended up getting this to work was by defining the BitmapImage in the Generic.xaml file and using a Pack URI to get to the file (here's the MSDN article about Pack URIs, which frankly just confused me). This way it's using a relative path to the file, and specifying the assembly that it's coming from (the file is located at \Resources\PlusSymbol.png in the MyCustomControlLibrary project and has a build action of Resource):
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/MyCustomControlLibrary;component/Resources/PlusSymbol.png" />
Then in the control template in Generic.xaml I use the resource like so:
<Button Grid.Row="0" Grid.Column="1" Margin="3" Height="25" Width="25"
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
Note that I got fooled thinking that I could use the shorter version of the Pack URI like so:
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/Resources/PlusSymbol.png" />
However this was still causing the program to crash at runtime. I think the fact that this is a custom control library, and that the end consumer of the image is my UI project, makes the longer version which specifies the assembly that actually contains the image necessary.
Hope that helps anyone else who is having similar problems.
Note I don't think this technically answers my original question which specified using app-level resources (aka defined in the custom control library's App.xaml file) from Generic.xaml. Defining the PlusSymbol resource there still crashes the program. However I didn't actually care about doing it in App.xaml, I was just trying to get it to work right at both design time and run time. This solution does the trick for me, is fairly simple, and from what I can tell from my research is the best practice.
I'm, a WPF & Xaml-noob, for console applications it has worked for me to simply give the relative path to the executable to access a file.
It doesn't seem to work in xaml for me. (code at the bottom)
Absolute path works perfectly.
Is it possible in XAML in a WPF-application to access a file by simply using a relative path to the directory of your executable as a valid UriSource? If yes how and if not why not?
I found the following question, where they were talking about adding the file via Visual Studio's "Add existing item", so it seems like a different issue.
How can I set the WPF BitmapImage UriSource property to a relative path?
<Window.Icon>
<!--absolute path works:-->
<BitmapImage UriSource="C:\LongPath\SolutionFolder\ProjectFolder\bin\Debug\path4.ico" />
<!--none of the following relative paths worked:-->
<!--AppDomain.CurrentDomain.BaseDirectory returns the Debug-folder-->
<!--<BitmapImage UriSource="path4.ico" />-->
<!--<BitmapImage UriSource="../path4.ico" />-->
<!--<BitmapImage UriSource="Debug/path4.ico" />-->
<!--<BitmapImage UriSource="bin/Debug/path4.ico" />-->
<!--<BitmapImage UriSource="../bin/Debug/path4.ico" />-->
<!--<BitmapImage UriSource="../../bin/Debug/path4.ico" />-->
<!--<BitmapImage UriSource="../Debug/path4.ico" />-->
</Window.Icon>
URIs can be confusing, as they can refer to both files on disk and resources in the application. So a relative path could be to a resource in the app, when you intend it to be on disk.
You can use the siteoforigin authority to force relative file URIs. This blog explains this more, but here an example:
pack://siteoforigin:,,,/path4.ico
Relative paths without qualifications look up the referenced file in the application resources, if a path should be relative to the executable you can use pack://siteoforigin:,,,/path4.ico, see Pack URIs on MSDN.
I'm loading an image in WPF by using the BitmapImage class. My code works perfectly when I give an absolute path for the UriSource but not when I try and use a relative path.
My XAML is:
<Image Width="50" Name="MemberImage" HorizontalAlignment="Left">
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="questionmark.jpg" />
</Image.Source>
</Image>
The questionmark.jpg is added to the project with a Build Action of Resource and Copy to Output Directory set to Copy always. The error I get is "The file [file name] is not part of the project or its 'Build Action' property is not set to 'Resource'". This works when I use an absolute path for the UriSource but that obviously won't do.
How should I be defining my UriSource in the XAML?
I don't think you need to copy the image to output directory once it's in resource.
Correct way to specify is
<BitmapImage
x:Key = "Logo"
UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png"
/>
Just replace
ApplicationNamespace with your application namespace
and
Images/App/image.png with your image path in the project
Image files with the following options in properties
Build Action=Content
Copy to Output Directory=Copy if newer
<BitmapImage x:Key="QuestionMark" UriSource="pack://siteoforigin:,,,/questionmark.png"/>
Reference:
Xaml - Bitmap UriSource - absolute path works, relative path does not, why?
I cannot reproduce the problem on my computer:
I add the jpg by choosing Add existing item in the Project menu
I then set its Build Actio to Resource and Copy to Output directory to always.
My XAML is the same.
Works perfectly here, even after moving the .exe and renaming the jpg. Something else must be biting you!