On 06/01/2005 5:10 AM, in article
83b31e2a.0501052110.529cb1aa@posting.google.com, "MailInterface"
[quoted text, click to view] <IMailInterface@gmail.com> wrote:
> Iam creating Application implementing IDesigner host and facing
> problem when embedding image in the form.After embedding the image in
> the form when i compile i get an Error "resource not found".I tried
> this on the MSDN provided Sample Designer host and it is also giving
> the same error.Any clues solution this problem
>
The MSDN sample doesn't write out the resources except in its XML format, if
you change the code as follows (apologies if it gets mangled by line
breaks):
SampleResourceService.cs
using System;
using System.Resources;
using System.ComponentModel.Design;
using System.IO;
using System.Collections;
namespace SampleDesignerHost
{
/// Resource service using .resx files
public class SampleResourceService : IResourceService, IDisposable
{
private Hashtable writers;
private Hashtable readers;
private IDesignerHost host;
private MemoryStream ms;
// Track whether Dispose has been called.
private Boolean disposed = false;
public SampleResourceService(IDesignerHost host)
{
this.host = host;
writers = new Hashtable();
readers = new Hashtable();
}
~SampleResourceService()
{
Dispose(false);
}
private void SerializationComplete()
{
foreach (IResourceWriter writer in writers.Values)
{
if (writer != null)
{
writer.Close();
writer.Dispose();
}
}
writers.Clear();
foreach (IResourceReader reader in readers.Values)
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
readers.Clear();
}
public byte[] Resources
{
get
{
SerializationComplete();
return ms.GetBuffer();
}
}
#region Implementation of IResourceService
public System.Resources.IResourceReader
GetResourceReader(System.Globalization.CultureInfo info)
{
ResXResourceReader reader = (ResXResourceReader) readers[info];
if (reader == null)
{
if (ms == null)
{
ms = new MemoryStream();
}
reader = new ResXResourceReader(ms);
readers.Add(info, reader);
}
return reader;
}
public System.Resources.IResourceWriter
GetResourceWriter(System.Globalization.CultureInfo info)
{
ResXResourceWriter writer = (ResXResourceWriter) writers[info];
if (writer == null)
{
if (ms == null)
{
ms = new MemoryStream();
}
writer = new ResXResourceWriter(ms);
writers.Add(info, writer);
}
return writer;
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(Boolean disposing)
{
// Check to see if Dispose has already been called.
if(!disposed)
{
SerializationComplete();
}
disposed = true;
}
#endregion
}
}
SampleDesignerLoader.cs - Save method
/// Save the current state of the loader. If the user loaded the
file
/// or saved once before, then he doesn't need to select a file
again.
/// Unless this is being called as a result of "Save As..." being
clicked,
/// in which case forceFilePrompt will be true.
internal void Save(bool forceFilePrompt)
{
try
{
if (dirty)
{
// Flush any changes to the buffer.
Flush();
}
// If the buffer has no name or this is a "Save As...",
// prompt the user for a file name. The user can save
// either the C#, VB, or XML (though only the XML can be
loaded).
//
int filterIndex = 3;
if ((fileName == null) || forceFilePrompt)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = "xml";
dlg.Filter = "C# Files|*.cs|Visual Basic Files|*.vb|XML
Files|*.xml";
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
filterIndex = dlg.FilterIndex;
}
}
if (fileName != null)
{
switch (filterIndex)
{
case 1:
{
// Generate C# code from our codeCompileUnit and
save it.
CodeGeneratorOptions o = new
CodeGeneratorOptions();
o.BlankLinesBetweenMembers = true;
o.BracingStyle = "C";
o.ElseOnClosing = false;
o.IndentString = " ";
StreamWriter sw = new StreamWriter(fileName);
CSharpCodeProvider cs = new
CSharpCodeProvider();
cs.CreateGenerator().GenerateCodeFromCompileUnit(codeCompileUnit, sw, o);
sw.Close();
} break;
case 2:
{
// Generate VB code from our codeCompileUnit and
save it.
CodeGeneratorOptions o = new
CodeGeneratorOptions();
o.BlankLinesBetweenMembers = true;
o.BracingStyle = "C";
o.ElseOnClosing = false;
o.IndentString = " ";
StreamWriter sw = new StreamWriter(fileName);
VBCodeProvider vb = new VBCodeProvider();
vb.CreateGenerator().GenerateCodeFromCompileUnit(codeCompileUnit, sw, o);
sw.Close();
} break;
case 3:
{