How to set an Icon on a Window - c#

I am new here, I want to set an Icon on the left side of my program(WPF).
I copied it in an Image folder.!
Code:
<Window x:Class="Hotelverwaltung.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Icon="..\Fachlogik\Images\icon.png" Title="Hotelverwaltung"
Height="350" Width="525" Loaded="Window_Loaded">

After you posted the image, I see the image is in a different project. That's important to know.
Make sure the Build Action for you image is set to "Resource".
Make sure the Hotelverwaltung project references Fachlogik.
Then try setting the location like this:
Icon="pack://application:,,,/Fachlogik;component/Images/icon.png"
You can't set a relative path like that to go between projects.

You used the right property. Just make sure that the image is included in the project (right click on the image in the Solution tree and choose 'Include in project'). Then set the Icon property using the properties window in VS. That should do it.

Try setting the Image path like this
Icon="pack://application:,,,/Hotelverwaltung;component/Fachlogik/Images/icon.png"
Check this link for more details about XamlParseException XAML Parse Exception - xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Related

WPF can't display image properly

Yesterday I create a project in my disk D partition and I found the images I put into project can't be displayed well. XAML code below is very simple:
<Window x:Class="WpfImageTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfImageTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image Width="300" Height="300" Source="/Resources/Images/Chen.png"/>
</Grid>
</Window>
You can see I write a relative path for Image control's Source property and when I run the application it shows nothing on the interface.But magically I copy all the files into the root of my C partition or D partition of my physical disk and rebuild the project, the application runs properly. This proplem troubles me almost all day , can anyone explain why this thing happens?
The original location of my project is D:\Programming\C#\Practice\WpfImageTest and I have already set all images build property to Copy all the time and Content.
Select your image file in your Solution Browser and set its Build Action to Resource as on image below.

How could I access wpf window resoures

I have this xaml
<mui:ModernWindow x:Uid="mui:ModernWindow_1" x:Class="App1.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ContentSource="/Window1.xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized" MenuLinkGroups="{Binding menuLinkGroups}">
<mui:ModernWindow.Resources>
<sys:String x:Key="ApplicationName">Bla Bla</sys:String>
</mui:ModernWindow.Resources>
<Grid Style="{StaticResource ContentRoot}" Name="mainGrid">
</Grid>
</mui:ModernWindow>
I need to reference current window resources, so I used this:
object obj = this.Resources["ApplicationName"];
But this.Resources doesn't have any resource! so obj is always null. How could I reference this window resources?
Assume that this is a FrameworkElement, like a Window, a Grid, a Button or something like that.
object obj = this.TryFindResource("ApplicationName");
Assuming this is a control...
var parent = Window.GetWindow(this)
Will get the window the control is currently on, you should then be able to access the resources like you already did
parent.Resources["ApplicationName"];
you can use below mentioned code
var MainGrid=Application.Current.FindResource("strApp")
or
this.FindResource("ApplicationName")
Thanks all, I find the solution (I forget to update the localized dll).
I cleaned and rebuilt solution, used locbaml.exe again to generate new localized dll.
You should bind in the XAML and not in the code behind.
"res" is the namespace where the resources file is located.
In your example the namespace alias is "local":
xmlns:local="clr-namespace:Project.Resources"
So your code should look like:
<Page Title ="{x:Static local:localStrings.step1Description}" />
Where:
"local" is the namespace alias where the resources file is located.
"localStrings" is the name of the resource file.
"step1Description" is an entry in the resource file.

embed project icon in WPF

This problem is getting me crazy, I really have no clue what's going on - why it's not working as I think it should.
I have a WPF C# project. I've added a directory named "ExtFiles" to my solution. This directory contains a ICO file, a PNG image and TTF font.
All three files have "build action" set to "resource", and "copy to output dir" set to "Do not copy".
I call these 3 files in the XAML via:
For the Icon:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="Swiftech_Imperium.MainWindow"
Title="Imp" Height="435" Width="510" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Icon="pack://application:,,,/ExtFiles/imp.ico"
For the Image:
<Image HorizontalAlignment="Left" Width="278" Height="30" Margin="9,401,0,0" VerticalAlignment="Top" Source="pack://application:,,,/ExtFiles/Logo.png"/>
And for the font:
<Label x:Name="LabelTemp1" BorderBrush="#FFFFA421" BorderThickness="1" FontFamily="pack://application:,,,/ExtFiles/#DS-Digital" Height="30"
Everything works, no error. The image is displaying, the icon too, the font is working.
But, when I publish, it does generate the ExtFile directory with the icon in it. Why is that? My goal is to have all 3 files embedded in the EXE. My goal is deploy the program using the single EXE file and nothing else, as the users's requirement.
All these 3 files are called only in the XAML and nowhere else, I don't understand why it's working fine for the image and font but not for the icon...
Any idea?
Thanks
Steve
Edit: I tried this: Embed multiple icons in WPF EXE (last answer) but it doesn't work.
Icon="/Resources/Imp.ico"
Source="/Resources/Logo.png"
FontFamily="/Resources/#DS-Digital"
I get this error (only for the icon, If I can the Icon=".." out it starts but the image and the font are not applied...
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '5' and line position '9'.
I added the 3 files as resources (Resource, add file) and set their build action to "resource".
edit:
OK... I moved forward a tiny bit.
I added both DLL's (yeah there two!) to the resources.
Moved the image/icon/font to a separate folder (that probably wasn't needed).
but I was still getting the same error (parsing).
turns out it is my XAML that is calling the second DLL (which I originally forgot to add to the resources).
Now with both DLL's as resources, if I go into References and set both of these DLL's Copy Local to False, I get an error. I can put the first dll (WPFToolkit.dll) to false, but I can't do for system.windows.controls.datavisualization.toolkit.dll :(
reason why is because the XAML is calling it:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="Imperium.MainWindow"
Title="Imperium" Height="435" Width="510" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Icon="pack://application:,,,/ExtFiles/Imp.ico" Closing="Window_Closing" MouseDown="Grid_MouseDown" WindowStyle="None" BorderThickness="1" BorderBrush="Black" Foreground="Black">
what can I do?
Your pack URN was wrong for the icon. It should be:
pack://application:,,,/(qualified assembly name i.e. My.Project.Executable);component/ExtFiles/imp.ico
When you add the file to the VS project folder you should set it as Resource with Do Not Copy.
The same applies for the image and the font - tho' I would also say that I've not loaded font using a direct reference like this and I'm not sure it works like this.
Try This:
Doing with Resources would be a different approach to achieve the same task. I would like to provide you a link to read certain msdn articles.
Adding and Editing Resources (Visual C#)

WPF - Trying to Add an Icon to my Window but getting a strange error

XamlParseException occured
Provide value on 'System.Windows.BAML.2006.TypeConverterMarkupExtension' threw an exception
I looked up examples of how to add an icon and it seems simple enough. How can I fix this?
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="DTV_Red_Zone_Channel.MainWindow"
Title="DirecTV Red Zone Channel" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize"
Initialized="WindowInitialized" SizeToContent="WidthAndHeight"
Icon="RCS_Generic.ico"> <---- Where it's Erroring
ah... it says it cannot locate resource 'rcs_generic.ico' but i put it in the main directory AND where the executable folder is... so where would i put it then?
You need to include the RSC_Generic.ico in your project (at the root, given your XAML), and make sure that it's properties are set to use a Build Action of Resource.

Visual Studio refactor-rename causes inexplicable global replace of attributes in XAML file

I feel like I must be going crazy, but I just changed the name of a property in a view model (a C# file) from Width to Size using Visual Studio's refactor-rename feature. When I was done, this error appeared in the Error window:
Error 2: The property 'Size' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
The error was referring to a XAML UserControl file. When I checked the file to see what was up, I realized that every attribute named Width had been changed to Size.
Example:
<UserControl x:Class="ApbSymbolGenerator.Views.Symbol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Size="{Binding Size}"
Height="{Binding Size}">
This is the first time I've ever had refactor-rename cause a change to a XAML file (besides the x:class value). (Note: I did not do a global find/replace, I did refactor-rename. I undid everything and performed the rename again, and it did the same thing.)
Strangely, it only affected one of several XAML files in my app that has a Width property.
Any explanation what could be going on here?
Looks like this is a bug (that won't be fixed) - Refactor Rename Bug

Categories