You can embed items into your WinForm app. So how to do you figure out what you have and the correct path to retreive the items? Here’s a method to show you what’s an embedded resource in your application.
Code:
using System.Diagnostics;
using System.Reflection;
private void ExposeResources()
{
Assembly assembly = Assembly.GetExecutingAssembly();
foreach ( string resource in assembly.GetManifestResourceNames() )
Debug.WriteLine(resource);
}
Usage:
ExposeResources();
Reference:
http://msdn.microsoft.com/en-us/library/aa287526(VS.71).aspx
I’ve embedded images into my app. Now I need to retrieve those images. Here’s code to do it.
Code:
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
...
Image workingImage;
private void LoadImages()
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream file;
if ( workingImage == null )
{
file = assembly.GetManifestResourceStream(".WorkingAni_32x32.gif");
if (file != null)
{
workingImage = Image.FromStream(file);
}
}
}
Usage:
Change the <AssemblyName> to match your environment. If you can't find the embedded resouce, use my "List Embedded Resources" snippet to find the correct path to your image.
The build action for the image properties must be set to 'Embedded Resource'.
If you are adding a new image, the project MUST BE REBUILT before the image is embedded. Rebuilding the entire solution is an easy way to ensure this gets done.
Who am I?
Where am I?
Have you ever asked that question while examining your code? Before I learned about the Call Stack in Visual Studio 2010, I faced that problem often. I’d have a million of these little debugging statements scattered throughout my code.
string callingAssemblyName = System.Reflection.Assembly.GetCallingAssembly().FullName;
string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
string className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();
Usage:
using System.Diagnostics;
Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "() called.");
or
string methodName = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;
Debug.WriteLine(methodName + "(" + <method args> + ") called.");
...
Debug.WriteLine(methodName + "() ending.");
I am using the M-V-VM pattern on a WPF app. The rules that we are following on my team state that the View only knows about the ViewModel via the DataContext. The ViewModel has no explicit knowledge of the View.
BTW: I created the project using the WPF M-V-VM template available on CodePlex (I think?). It already contains the DelegateCommand classes.
I have an “OK” button.
In WPF, a button does nothing. If you want to do do “something” you have to wire up a command that resides in the ViewModel.
View:
<Button Command="{Binding UserClickedOkButtonCommand}" Content="OK" />
ViewModel:
public ICommand UserClickedOkButtonCommand { get { return new DelegateCommand( UserClickedOkButton); } }
public void UserClickedOkButton()
{
// do something
}
If you put a break point on the UserClickedOkButton, you will see the event is correctly mapped to the delegate method. So how do I close the View?
It takes a couple extra steps to get all of your ducks in a row.
First, add an x:Name=”ThisWindowName” to your View header.
Second, add a command parameter that refers back to your new window name.
Third, update the Command in the ViewModel to use a Window as it’s argument.
Fourth, use the input window argument to close the window.
Here’s the code.
View:
<Window
...
x:Name="ThisWindowName"
>
...
<Button Command="{Binding UserClickedOkButtonCommand}" CommandParameter="{Binding ElementName=ThisWindowName, Path=.}" Content="OK" />
ViewModel:
public ICommand UserClickedOkButtonCommand { get { return new DelegateCommand( UserClickedOkButton); } }
public void UserClickedOkButton(Window window)
{
// Do Something
window.Close();
}
The window is closed. Concerns are separated. MVVM rules are followed.
Works on my machine.
“Only the mediocre are always at their best.”
Jean Giraudoux
There are so many ways to take that one. “Sarcastically” is leading the mental calculus. Go figure.
I wrote about this a couple days ago. I have now confirmed my fix.
ENV:
Win7 Ent x64, VS 2008 SP1
Problem:
API restriction: The assembly ‘file:///c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll’ has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.
Solution:
Make sure you don’t have any extra references in your non-test projects. It happened to one guy on our team. The reference showed up during the check-in code review.
Remove “processorArchitecture=MSIL” from the UnitTestFramework reference in ALL of the test project files.
<reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
becomes…
<reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
Restart VS. Reloading the project isn’t enough to get around the issue once it has popped up. I had to restart Visual Studio.
The issue went away after I double checked all of my project files and cleaned up the references in my test projects. (There are 12 projects in my current solution including 5 test projects.) I tested the fix by:
- Deleting the UnitTestFramework reference from one of my test projects.
- Re-add the reference via the UI by right-clicking on the references folder
- Add reference
- .Net tab
- Select Microsoft.VisualStudio.QualityTools.UnitTestFramework
- Click ok
- Save all. At this point the processorArchitecture reference appeared in my project file.
- Compile. Failure.
- Manually edit the project file with Notepad2.
- Save.
- Reload project when VS notices the change outside of the IDE.
- Compile. Failed.
- Restart Visual Studio.
- Load solution.
- Compile solution. No errors.
Works on my machine…
I have been having problems with the internal testing library in Visual Studio. I get an API error. “The assembly has already been loaded and cannot be loaded into the appDomain.”
Really? <sarcastic response>
Everyone on the team has had intermittent issues for a couple months. One solution seemed to be to clean the solution, close VS, reload VS, open your solution, load the Test View (View -> Tool Bars -> Testing, find the “Test View” icon and click on it). Once the Test View loaded up the assemblies the issue might not appear. Obviously that isn’t an ideal solution.
I tried going through each project, removing any references to the lib, clean, rebuild solution (obviously a failure), then re-add the reference for each missing assembly. That worked once and only once.
My latest effort was to go through each project file and manually edit the reference. I removed the processorArchitecture=MSIL attribute from each entry. I loaded up my solution in VS and went through the usual Test View steps. So far, it works.
Maybe this problem is worse on x64 systems. Our team lead was running Win2008 x64 for months and always struggled with this issue. When I was on WinXP the issue was a minor annoyance that would pop up once a week or so. I switched to Win7 x64 a couple weeks ago and the issue has been a constant irritant ever since.
Hmm…
Sitting on the couch. MotoGP final round in HiDef. Netbook running an ASP.Net MVC site that I have been working on.
Lorenzo has a huge moment and Rossi passes. He is nearly chucked off the bike.
“I don’t know how he stays on when that happens…”
“Faith?”
“So is that what it feels like when you lean over?”
“I suppose. Read more…
Am I allowed to lust for another bike yet? Probably not.
Italian Sex Appeal: Aprillia RSV4+R in black of course… My latest Cycle World rated it as Bike Of The Year, just like my SD.
I’ve been looking at the new BMW superbike, the S 1000 RR. It looks very interesting and the price is very good. Huge amounts of technology, under $15K.
But hey, if you are going to lust… Which would you rather have a racy Italian or a racy German. A racy German?!? That almost sounds oxymoronic. I think I’d even trade the SuperDuke for the Aprilia. I’ve got the FJR for road trips… Hmm… But what about my commutes? I guess I need all three!
One thing I do know that I don’t need the Factory version. I know the standard is way better than my skills can handle. So, let’s stick with the “cheap one.”
Just don’t say anything to the woman! I promised to not lust after another bike until the KTM is paid off.
The woman asked what I knew about the shootings in Texas. My reply was basically, “Just another whacko that couldn’t handle what they asked for. If you join the Army, odds are you are going to war. When you’re a lifer, it should be a given. He didn’t want to be deployed, couldn’t get out of it, freaked out and lost it. Simple. Disappointing. Let him rot.”
So she started looking it up online. I looked over her shoulder and saw a BIG ad for hotels at Fort Hood. 75% off room rates. Being me I had to comment.
“Hey look at that! Boy look at that deal. I’m sure they are hurting for customers, now that the population has dropped.”
“You know, you are a truly horrible person sometimes…”
“I thought I was a retched human being?”
“That too”
Read more…