Showing posts with label VS2010. Show all posts
Showing posts with label VS2010. Show all posts

Wednesday, 18 April 2012

The Resource ‘X’ Could not be resolved : WP7

A while back, I took one of my WP7 programs, and saved it as a Template, so it was easy to create a new WP7 app with all my framework code already built into the app.

What I didn’t notice at the time though, was that my Merged resource dictionary was not being used to render in the VS2010 xaml designer, though they were working correctly in the actual program, and in Blend.

After spending a few hours searching on the web, and trying “voodoo” solutions, I found the answer (or thought I did, until I began writing this up, which wasted another hour or two)

My original solution was built with this ResourceDictionary located in my Framework folder:

<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/AppDetails.xaml" />
<ResourceDictionary Source="/Framework/Resources/FrameworkResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>


Both xaml files above were included in the build as Content


This is the code I needed instead:


<!--Application Resources-->
pplication.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyApp;component/Framework/Resources/FrameworkResources.xaml" />
<ResourceDictionary Source="/MyApp;component/Resources/AppDetails.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>


And to get this to work, the xaml files needed to be changed to Page


You must also make sure your App.xaml file is marked with a build action of ApplicationDefinition and your App.xaml appears to have to be in the root of your project.


I had some cases where it appeared to work with various combinitions of all of the above, but after a restart of visual studio, occasionally it would again lose the resources in the designer, so the only apparent way to make this work is to follow the exact steps above.

Friday, 16 March 2012

My splash screen closed my message box

Today, while trying to debug on a very slow virtual machine, I decided to add a splash screen to my WPF application.  After all, my phone applications have one, so how hard could it be?

First I found these instructions on MSDN for adding a splash screen

Wow.  That’s easy!  However to my surprise, I found that in some circumstances, my application caused a message box to show before the main window was displayed, as my MessageBox was shown by the constructor.

And to my surprise, the splash screen closed that message box.  Now I’m not the first to find this problem.  There are various discussions and links here, but I have to admit, I didn’t really like any of the three options presented there.

So, I thought, there must be a better way.  Well, I think I’ve found one!

First, construct your SplashScreen object as shown in this constructor:

MainWindow()
{
SplashScreen ss = new SplashScreen("Images/splash.png");

// show the splash screen
ss.Show(false);

// do the rest of the constructor work.
// ...
//

ss.Close(new Timespan.FromSeconds(0.1));
}

 

When I first wrote the above, I had my splash screen flicker, as my main application appeared on top of the splash screen.  To solve that I came up with a very confusing BeginInvoke, which is only necessary to solve a different problem.

 

Turns out, that because I followed the initial instructions, I had added my splash screen with a build action of “SplashScreen”.  That’s wrong.  The build action should be resource if you want to do it manually, otherwise, it appears that you end up with two splash screens!


Wednesday, 12 October 2011

Connecting to SQL CE 3.5 databases from C#

I’ve been scratching my head for a long time about how I should be connecting to my .sdf files from c#.  I’ve had a couple of data based projects on the go for some time now, and continually find myself bumping up against some problem as the project progresses.

I tried creating the data with the wizard and an Entity Data Model/Entity Framework.

This leads to pain, as your tables cannot have an server generated identity field.

I tried creating the database using EDM and code first

But for the life of me, I couldn’t work out how to bind it to tables in the database.

I tried using OLE DB

And then had to struggle with code to fetch the identity like this:

_lastIndexCmd = _sqlConn.CreateCommand();
_lastIndexCmd.CommandText = "SELECT @@IDENTITY";


and had to be very careful about inserting only one record at a time.

 

This spring, I went to TechDays in London, and Andy Wigley gave a talk http://blogs.msdn.com/b/mikeormond/archive/2011/07/12/tech-days-live-video-sql-server-compact-and-user-data-access-in-mango.aspx
on using SQL CE in mango.  I asked a bunch of questions in the talk (yes, that’s me in the orange jumper at the front) about server generated keys.


The reason I asked, was because I had misunderstood the comment earlier in the talk that mentioned EDM 4.1, and didn’t realize that we were actually talking about LINQ to SQL, not EF.  In other projects I had problems with inserting multiple server generated keys as described in the OLE DB section above.


Today, I watched the talks again, very carefully, and realized that I should have been using Linq to SQL, and that that will let me work on the phone too.


To create your objects from an already existing database file, run the following command:


sqlmetal /dbml:mydb.dbml mydb.sdf /pluralize


Afterwards, simply add the mydb.dbml file to your project.


Don’t forget the pluralize switch if you’re trying to replace your EDM implementation, or you may be in for a lot of member renaming.


The other gotchas?  AddObject in EF is replaced with InsertOnSubmit on the tables, and  SaveChanges on the DataContext becomes SubmitChanges.


After these changes, I was pleasantly surprised that my query time dropped from 95msec for 11520 records to 78, and improvement of  18%.


I hope I’ve got this right, but if not, hope ErikEJ over at http://erikej.blogspot.com/ will put me right.

Friday, 2 September 2011

VS2010/WiX Porting issues

Upgrading some old VS2005/Wix 3.5 projects to the new compiler raised some issues:

WiX gave some build errors described here:

http://sourceforge.net/tracker/index.php?func=detail&aid=3293788&group_id=105970&atid=642714

this was also set incorrectly in my wixproj file:

<WixToolPath>$(ProgramFiles)\Windows Installer XML v3.5\bin\</WixToolPath>