I am trying to get the Icon of a NotifyIcon in WPF.
So I have added a .ico file to my solution in a Resources folder and set the build action to Resource.
I am trying to grab this resource in code behind like so:
var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")
This doesn't work.
In addition to this: Application.Current.Resources.Count returns 0.
EDIT
var i = new Icon(Application.GetResourceStream(new Uri("/systemtrayicon.ico", UriKind.Relative)).Stream);
With the icon in the root and the build action set to Resource.
Still not working.
EDIT AGAIN:
I needed to Clean the solution and rebuild as per: WPF throws "Cannot locate resource" exception when loading the image
This will works 100%
ni.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,<Image Location From root>")).Stream);
Example:
notify.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/images/favicon.ico")).Stream);
You have to pass resourceName as a parameter to the FindResource method, not the path for the Resource. Sample code would look like:
var icon = (Icon) Application.Current.FindResource("myImage")
Please note in the above sample code "myImage" is the resource name.
Refer to Application.FindResource Method on MSDN.
You say, Application.Current.Resources.Count is Zero, that means you do not have any Resource defined in your App.xaml file.
You can add resources to App.xaml like this:
<Application.Resources>
<Image x:Key="myImage" Source="img.png" />
</Application.Resources>
It appears that your icon is an embedded resource. FindResource cannot work with embedded resources. Set BuildAction of your icon to Resource.
Refer to this MSDN page for more reading on WPF Resources.
UPDATE
Code for accessing Embedded Resources
Assembly.GetExecutingAssembly().GetManifestResourceStream("myImg.png");
However, if you had added this image to the Resources.Resx and you should simply be able to use Resources.ResourceName.
UPDATE 2
Adding resources to App.xaml or any ResourceDictionary is better, so that you can use them as Static/Dynamic resources via StaticResource or DynamicResource markup extensions.
If you do not want to add it to App.xaml resources and still want to access it, one option as I mentioned above is to add it to the Resources.Resx and use Resources.ResourceName to refer the icon/image
Another way is to create System.Drawing.Icon by yourself, sample code:
new System.Drawing.Icon(Application.GetResourceStream(new Uri("/Resources/icon.ico")));
Personally, I would go with XAML resources and add them to App.xaml or a ResourceDictionary.
Related
We are switching a huge WPF Appl. to .NET 6.0. At least one CustomControl which worked on 4.8 does not get it's template applied which is referenced via Generic.xaml. I am not sure if Generic.xaml is not loaded or something else needs to be considered.
The Style can be added manually to Application.Resources.MergedDictionaries via a simple "Add" call with the Source set to it. I can see the CustomControl Style afterwards with the Key being the correct Type. It is still not applied, as there is no visual representation and no call is made to the overriden OnApplyTemplate method.
If all else fails, can I apply a template manually if I have the given style, like just apply the Style manually to a newly created instance?
Also: the Projects are now SDK-Style, AssemblyInfo.cs was taken over and "generate assembly info" is set to false. It contains the standard ThemeInfo entry.
Just for clarification following the code which successfully finds the Generic.xaml. But before 6.0 Generic.xaml was loaded without doing anything.
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Contracts;component/Themes/Generic.xaml", UriKind.Relative)
});
Thank you all for your help!
I had the same Problem.
maybe u need to add the Assembly.cs at TopLevel of ur CustomControlLibrary
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
I have the following activity which specifies the theme as an attribute
[Activity(Label = "PermissionsActivity", Theme = "#android:style/Theme.Translucent.NoTitleBar")]
public class PermissionsActivity: Activity
This works well, but how can I apply the same at runtime? Maybe by calling SetTheme in OnCreate. I can see SetTheme accepts a resource id integer. I'm having a hard time finding the corresponding Xamarin.Android constant for the aforementioned theme. Please help
Add your theme in style.xml file under Resource folder than access it from resource as int
<style name="MyTheme" parent="Theme.Translucent.NoTitleBar">
</style>
Setting in activity
this.SetTheme(Resource.Style.MyTheme);
Add this code in your onCreate Method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
For reference check this Link
When you add some theme in any folder under the resources of an android project what Visual studio does is it creates a corresponding int value inside the ResourceDesigner.cs file under the resources folder.
Now in Runtime when you need to add these to your code they are available as follows:
If the resource is a style then it is available in Resource.Style.YourResourceName
If the resource is a dimension then it is available in Resource.Dimen.YourResourceName
If the resource is a string then it is available in Resource.String.YourResourceName
If the resource is an image under the drawable folder then it is available in Resource.Drawable.YourResourceName
If the resource is an image under the mipmap folder then it is available in Resource.Mipmap.YourResourceName, And so on and so forth.
Note: These properties are always an integer.
In your case since it is a theme(which is basically a style)
Hence you can get it like this in an Activity:
this.SetTheme(Resource.Style.MyTheme);
And in a Fragment something like this :
this.Activity.SetTheme(Resource.Style.MyTheme);
Hope this helps,
Revert in case of queries.
As far as I know "Cursor" don't exits in UWP.
I can change cursor with this code:
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
How do I create a custom cursor with CoreCursorType.Custom?
CoreCursorType.Custom
You should create your own cursor *.res resource file and contains the custom cursor to your project by add it to your package.manifest file. After that, you can use CoreCursorType.Custom enumeration option, and specify the Id of the custom cursor to use the custom cursor. There also have a thread in which #Azat Tazayan has introduced the detailed steps to implement it and you can refer to it:
https://social.msdn.microsoft.com/Forums/en-US/14001796-bcd5-4b9d-9d7e-13bc6ba59d2d/uwp-how-to-set-a-pointer-cursor-as-a-circle-instead-of-a-window-default-cursor?forum=wpdevelop
Here is the answer
https://learn.microsoft.com/en-us/uwp/api/windows.ui.core.corecursor?view=winrt-20348
To use a custom cursor, use the CoreCursorType.Custom enumeration
option, and specify the Id of the custom cursor. You can add a .res
resource file that contains the custom cursor to your project and
include it in your assembly with the /win32res compiler option. The Id
is the Cursor Resource ID specified in the .res file.
Personally I have used this working manual
https://social.msdn.microsoft.com/Forums/en-US/14001796-bcd5-4b9d-9d7e-13bc6ba59d2d/uwp-how-to-set-a-pointer-cursor-as-a-circle-instead-of-a-window-default-cursor?forum=wpdevelop
We need just proper .res file
For that we should do following
Create any C++ project. For example Dll, Add new C++ Dll project name it for example as ResourcesComponent.
Add a resource file to the project. [right-mouse] the project name "ResourcesComponent" => Add New Item => Visual C++ => Resource File (.rc)
Name the resource file for example Resources.rc
Add a cursor resource. Double click on Resources.rc the it will open in Resource View tab then Right click on "Resources.rc" => Add Resource => Cursor => New (Here you can modify cursor appearance by pen)
Go back to solution explorer and now you will see resource.h file where you can find its id
#define IDC_CURSOR1 101
Build dll in Release mode
In Release sub folder you can find Resource.res file
Now we have proper .res file, We should add it to our UWP project root folder.
Copy Resource.res file to UWP project root directory. (You can delete DLL project as it is no longer needed )
Unload UWP project in VS.
Rigth click on unloaded project and select edit .csproj
in first or second PropertyGroup (where is Appname, TargetPlatform, etc ) add
<PropertyGroup>
.....
<Win32Resource>Resource.res</Win32Resource>
</ PropertyGroup>
Reload project
Use following code for set cursor in your image PointerEnter event handler
Window.Current.CoreWindow.PointerCursor =
new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Custom, 101);
If you need to create a colorized *.cur file use any free tools like I did here. And just replace the source *.cur with a new one.
And if you need a small *.cur file just remember that by default its size is 32 x 32 pixels and you have draw your small image like I did.
Enjoy!
I want to add my icon as tay icon.
But this error is shown.
xaml.cs:
private System.Windows.Forms.NotifyIcon _notifyIcon;
This icon already added on resources but not working.
When I writing codes then resources file name not be shown.
See:
How can I solve this problem?
If you want to use it that way - don't add the image to the .resx file. Right click on the project -> Properties -> Resources -> Add Resource -> Add existing file...
Add the image there. If you do this you should be able to get it in the code through Properties.Resources.MyIcon
You can use Stream
Stream iconStream = Application.GetResourceStream(
new Uri( "pack://application:,,,/YourReferencedAssembly;component/YourPossibleSubFolder/YourResourceFile.ico" )).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
You just need to have an existing file, open the context menu on your folder , and then choose Add => Existing item...like this,name the folder Images
You need to change your image's property "Build Action" to Content and change the Copy to "If Newer" or "Always"
It will create a folder in bin\debug every time you build your application.
Then give your path like this #"Images\"
I have a lot of <Style> in <Window.Resources>.
How can I separate <Style> from <Window.Resources>?
Do I have to make a new Window file and use <Application.Resources>?
I also have a lot of those.
in my <Window.Resources>. Is it also possible to separate those?
You add those styles in ResourceDictionary and use it where ever required. You can add application level or Window Level.
http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx
http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF
You can create a ResourceDictionary file. It's just a xaml where you can put styles, control templates etc. See here for more info: http://msdn.microsoft.com/en-gb/library/cc903952(v=vs.95).aspx
You can then attach this dictionary to your project in app.xaml.