SplashScreen s = new SplashScreen(System.Reflection.Assembly.GetExecutingAssembly(),"splash.png");
s.Show(false);
when invoking s.Show() whether the parameter is false or true, it throws an IOException
with the message
Cannot locate resource 'splash.png'
even though that splash.png is added to resources and its Build Action is Resource.
I have noticed many problems when using images in WPF, also with Image control.
According to the principle "My Code doesn't work and I don't know why. My code Works and I don't know why", I have solved the problem.
First I have made the Build Action of the splash image as Resource. (Putting the image as Embedded Resource didn't solved the problem).
Second, My code is :
SplashScreen s = new SplashScreen("resources/splash.png");
s.Show(false);
/* do some things */
s.Close(Timespan.FromMilliseconds(300));
even though that splash.png is added to resources and its Build Action is Resource.
Wrong build action, make it Embedded Resource. If you still have trouble then use ildasm.exe to look at the assembly manifest for the .mresource directive to ensure that the resource got properly embedded with the expected name.
Or use a file, following these how-to steps. Generally the more sane approach since it doesn't make that much sense to have a large resource occupy address space for the life of the program when it is only needed for a short amount of time at the start of the program.
try with explicitly specifying the assembly name, even though the caller was in the same assembly as the resource.
Here's how it looks with a Pack URI syntax:
pack://application:,,,/MyAssemblyName;component/MyResourcesFolder/MyImage.png
http://msdn.microsoft.com/en-us/library/aa970069.aspx
Related
I have these files.
"Control.cs" [this has method activateabc()] and "abc.xaml" in an assembly (created as a Class Library) and I have referred it in my working project(Windows Phone 8.1 Runtime App). I have added
Frame.Navigate(typeof(abc)) within the activateabc() and when I call activateabc() from the working project it throws XAMLPARSEEXCEPTION.
Any idea how do I navigate to XAML page in another assembly? Also I have no dots or hypens or underscores in any of my assembly names.
Edit: NavigationService.Naivage()is available in Windows Phone 8 to do the job. However, Frame.Navigate() allows navigation to a type rather than to a URI as in WP8. So please tell me a way how to navigate to a XAML page in different assembly in Windows Phone 8.1 Runtime Apps
Can you provide more information please? I don't have an answer but I do have some additional information that might help.
Do you know if it's failing to parse the xaml markup or is it in truth failing to find the abc.xaml file? I bet this is the real problem. If it's failing to find the file you might need to modify the xaml file Uri to include the assembly name. I don't know how to override the default file load location for pages since that code gets generated but the Uri would look like this.
If your assembly is MyCustomControls.dll then the Uri would be new Uri("ms-appx:///MyCustomControls/abc.xaml").
If your xaml file is found but some markup is failing you might try to comment out all the markup until there is no load failure then comment it back in by bits until you isolate which bit of markup seems to be the problem. It might be some namespace resolution issue in the app's xamltypeinfo.g.cs. I doubt this is the issue though since you say you added the assembly as a reference.
I hope some of the above information is useful.
<Application.Resources>
<BitmapImage x:Key="MyImageSource" UriSource="C:\Users\nwrobinson\Pictures\skyskanlogo.jpg" />
</Application.Resources>
hello,
im having an issue with the way C# handles resources, i understand that the above code is a reference to a picture, but i assumed that when the program compiled, it would include the picture for when it runs on other computers. instead, it just crashes when the resource is not present in that location. this makes no sense to me, there has to be a way to include the picture in the program so it will run right?
the way it was explained to me, the application always has to have a photo to reference, so does that mean every time i give someone the application i have to send them the photo as well, or is there a way to have the photo wrapped into the application.
this is my first WPF so im not 100% sure what going on all the time, it might have just been something that was overlooked.
thanks for the help
What you want to do is to add the picture to your project and set its Build Action to Resource. Then you can change your UriSource to something like
UriSource="pack://application:,,,/YourProjectAssembly;component/DirectoryName/FolderName/ImageName"
When you look at the properties for the image in Visual Studio, you can select a Build Action. This can be one of many options, including copying the image to the build folder, embedding it as a resource, and others.
Depending on how you include the image in your project, you will need to use the Pack URI syntax to specify where the image can be found. It is not straightforward, but there are enough examples here (http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx) for when the resources are embedded etc. that you should be able figure it out. It typically involves specifying the assembly in which the resource is located and a relative path, with a very specific assortment of punctuation interspersed.
All, the question is a basic one. I have been loading all .png icon that I use in a complex UI as a resource. This means that the 'Resource' folder contains approx. 100-150 small .pngs. This works but should I be doing this and if not what should I be doing?
Thanks for your time.
well basically, if you think the images will change then store them outside a resource file. If they're going to be static over the lifetime of the application (or its version) then a resource file is fine
Please excuse the newbie question - my background is Unix and I am very green when it comes to WPF...
//Context//
I need to design a single page WPF-based UI, using Visual Studio, or Blend.
Additionally, I need this to be portable outside the microsoft generated solution... this is because I need to be able to define the business logic within a different c# environment, that is exposed by software called 'NinjaTrader' (for those who may use it).
I am restricted to .NET 3.5
//Additional info//
Right now, I am trying to get to proof-of-concept as quickly as possible. I have successfully implemented the UI using winforms. by simply designing the form within VS, and then copying the auto-generated designer code into my own abstract class definition (say 'myForm') within NinjaTrader. I then have to inherit myForm into a new class definition within a 'standard' NinjaTrader.Indicator class (this is necessary to be able to instantiate the myForm2 class), and by passing the outer class to the constructor of myForm2, and overriding the methods I need for the business logic, I can display a UI upon 'OnStartUp' of the NinjaTrader.Indicator class, and implement the behaviour that I need.
//My problem//
WPF is proving more difficult so far. I have tried copying the class definitions from 'WPFWindow.xaml.cs', and also what is necessary from 'WPFWindow.g.cs' into a single .cs file (due to limitations within NinjaTrader, I must use a single file for this), and then instantiating this class from another NinjaTrader.Indicator class.
Everything is in the same namespace, though I am repeatedly stumped when it comes to the relative Uri and it seems I get the following message, no matter what I do:
Cannot locate resource 'wpfwindow/wpfwindow.xaml'
Now - I am aware that the relative Uri will not be the same, when running from within a different application, and I have even tried copying the WPFWindow.xaml file to a new folder (within what seems to be the current working directory, and without any spaces) and calculating the relative :
string cpath = Directory.GetCurrentDirectory();
string installPath = #"C:\Program Files (x86)\NinjaTrader 7\bin64\WPFWindow\WPFWindow.xaml";
Uri cwUri = new Uri(#cpath);
Uri instUri = new Uri(#installPath);
string relPath = cwUri.MakeRelativeUri(instUri).ToString();
System.Uri resourceLocater = new System.Uri(relPath, System.UriKind.Relative);
System.Windows.Application.LoadComponent(this, resourceLocater);
However, I am still getting the same error.
I would be extremely grateful if anyone has any advice / suggestions as to how I might try to proceed.
Is there another way that I can load an XAML file (that is external to the current application / project), without using a relative Uri in order to display a single UI window? Any advice on what the bare minimum (less is definitely more reliable in this case) I would need to implement in order to do this and define the business logic, would be extremely helpful...
Thanks in advance for any advice
J
EDIT: (& Reponse to Brian S):
Thanks for the response, and also for the reccomendation on material. It is entirely possible that I have misunderstood the necessary architecture involved with WPF...
Therefore, please let me know it that is the case... My understanding is as follows:
The graphical composition, content, layout, etc is all specified within the XAML file. Behaviour, interaction, event handling etc is specified in the code-behind.
Once the XAML markup has been written, I had made the assumption that, in order to instantiate the window I had designed, all I needed to do was to load / parse it into a tree of objects within - lets say - a class that inherits System.Windows.Window, and is named as the same class as in x:class="myClass", within the XAML file.
Upon re-reading what I wrote previously, I realise that I was not all that clear about the situation. I can use multiple files for the solution, without a problem, the issue arises as follows:
With classes that are partial, across 2 files - lets say file1.cs and file2.cs, class methods will only be recognized within the same file that they are defined (unless I actually instantiate the class - after which I can call it's methods). So - for example - I could not specify InitializeComponent() in file1.cs, and then define a constructor in file2 that refers to InitializeComponent()... I hope that makes sense...
What I can do, which seems to work, is to avoid defining methods outside of the file that I need to reference them in. And thus, avoiding the use of partial classes as far as possible.
I can define a class in one file (say class1.cs), then instantiate it from another file (say class2.cs) no problem, by using:
private class1 mynewclass1 = new class1(args)
I can also use:
'System.Windows.Application.LoadComponent(args);'
Though I seem to be running into issues with the relative addressing, as no matter what I try it does not seem to find the .xaml file..
Is there a way that I can try to load the xaml file without using relative addressing? I have been looking at XamlReader, though I'm not sure it can do what I need....
Thanks again,
J
If I follow, you're trying to treat the WpfWindow.xaml file as a loose resource file located in the same directory. This is not how WPF works - the WpfWindow.xaml file is a partial class with the codebehind. This is possible with XAML Resource Dictionaries, but I don't believe it is possible with the WpfWindow.xaml file because they need to be compiled together.
If you want a single-file solution, it is a lot more work, but everything you do in XAML, you can do through code. So you can create a window, add controls, position them and connect behaviors all through the codebehind. Obviously, this is not the ideal approach, but it sounds like you've got some pretty significant limitations (I'm not familiar with NinjaTrader).
The MSDN Documentation will provide help on the code necessary to manually construct and layout a WPF window, or if you are looking for another resource, Applications = Code + Markup, by Charles Petzold goes through doing anything you need to do with WPF from codebehind.
I've a tiny application with several icons. I decided to put them in a separated DLL, so within my solution, I created a project and I've embedded those icons as resources.
Then I compiled this library (myImages.dll)
After, I added that dll as reference in the main program and I used the ResourceManager to get the reference of my custom library. Then I pointed to use GetObject method to retrieve the name of each file.
The following code works, but an error occurs when I try to retrieve an icon:
ResourceManager rm = new ResourceManager("myAssembly.MyNamespace", Assembly.LoadFile("Images.dll"));
Image myImage = ((System.Drawing.Image)(rm.GetObject("myIcon")));
Seems that the reference to my Images.dll doesn't work properly.
Any hints?
Usually when you get resources by name, you need to provide a fully qualified name. If you call GetManifestResourceNames, you can get a list of the names in your loaded resource manager to easily find out exactly what the names area that you must use.