MS - Design time URLS

  • Thread starter Justin Weinberg
  • Start date
J

Justin Weinberg

I'm resposting because I think I stated my queston poorly and having
answered questions myself, realize previously answered ones go
unnoticed.

The question is - How does the Microsoft Image Control get the "actual
path" to the image to render it at design time. The control takes a
relative URL and "like magic" renders a design time preview of the
relative specified path.

I've seen solutions that use ENVDTE - is this how how this control
works internally?

The Site property and getService look real promising, but I can't seem
to find what I'm looking for.

The question has been asked a lot (I google grouped for it for the
better part of yesterday), but never adequately anwered.

Thanks,

Justin Weinberg
 
A

Alessandro Zifiglio

hi Justin,
Yeah, your first post to which i responded werent all that clear. Well for
a proper example of how microsoft is doing it you might want to look at
IEWebControls. They are supplying the source code along with it and they
have written two small class that do just that for the set of IEWebControls.
So why not rip that and use it, surely they wont mind ;) and surely nobody
could do it better than how they are doing it :) They have created a custom
Editor. Its a set of two classes -- ObjectUrlEditor.cs and
ObjectImageUrlEditor.cs. The code is self explanatory and commented so you
shouldnt have a problem following and integrating it into your own control
;)
//////////////////////////////////////////////////////////////////////Object
UrlEditor.cs//////////////////////////////////Start/////////////////////////
//////////////////////////////////////
namespace YourNameSpace
{
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Web.UI.Design;
using Microsoft.Web.UI.WebControls;

/// <summary>
/// Provides an editor for visually picking an URL.
/// </summary>
public class ObjectUrlEditor : UITypeEditor
{
/// <summary>
/// Gets the caption for the URL.
/// </summary>
protected virtual string Caption
{
get { return DesignUtil.GetStringResource("UrlCaption"); }
}

/// <summary>
/// Gets the filter to use.
/// </summary>
protected virtual string Filter
{
get { return DesignUtil.GetStringResource("UrlFilter"); }
}

/// <summary>
/// Gets the options for the URL picker.
/// </summary>
protected virtual UrlBuilderOptions Options
{
get { return UrlBuilderOptions.None; }
}

/// <summary>
/// Gets the editting style of the Edit method.
/// </summary>
/// <param name="context">The context</param>
/// <returns>The style</returns>
public override UITypeEditorEditStyle
GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}

/// <summary>
/// Edits the specified object value using the editor style provided
by GetEditorStyle.
/// </summary>
/// <param name="context">Context</param>
/// <param name="provider">Provider</param>
/// <param name="value">The value to edit</param>
/// <returns>The editted value</returns>
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
if (provider != null)
{
IWindowsFormsEditorService edSvc =
(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorSe
rvice));

if (edSvc != null)
{
object node = null;

if (context.Instance is BaseChildNode)
{
node = context.Instance;
}
else if (context.Instance is object[])
{
object[] components = (object[])context.Instance;
if (components[0] is BaseChildNode)
{
node = components[0];
}
}

while ((node != null) && (node is BaseChildNode))
{
node = ((BaseChildNode)node).Parent;
}

if ((node != null) && (node is IComponent))
{
string url = UrlBuilder.BuildUrl((IComponent)node,
null, (string)value, Caption, Filter, Options);
if (url != null)
{
value = url;
}
}
}
}

return value;
}
}
}

////////////////////////////////////////////////////////////////////////////
/////////ObjectUrlEditor.cs//////////////////////////////////End////////////
////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
////ObjectImageUrlEditor.cs///////////////////////////////////Start/////////
//////////////////////////////////////////
namespace YourNameSpace
{
/// <summary>
/// Provides an editor for visually picking an image URL.
/// </summary>
public class ObjectImageUrlEditor : ObjectUrlEditor
{
/// <summary>
/// Gets the caption for the URL.
/// </summary>
protected override string Caption
{
get { return DesignUtil.GetStringResource("ImageUrlCaption"); }
}

/// <summary>
/// Gets the filter to use.
/// </summary>
protected override string Filter
{
get { return DesignUtil.GetStringResource("ImageUrlFilter"); }
}
}
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////ObjectImageUrlEditor.cs/////////////////////
//////End//////////////////////////////////


//////////////////////////////////////////////////How to call the editor and
use it in your own
property////////////////////////////////////////////////////////////////////
/////////
////Now all you need to do is use the editor anywhere, when ever you need it
//////////////////////////////////////
/// <summary>
/// The URL of an image to display within the control.
/// </summary>
[
Category("Appearance"),
DefaultValue(""),
Editor(typeof(YourNameSpace.ObjectImageUrlEditor),
typeof(UITypeEditor)),
PersistenceMode(PersistenceMode.Attribute),
ResDescription("ItemImageUrl"),
]
public string ImageUrl
{
get
{
Object obj = ViewState["Image"];
return (obj == null) ? String.Empty : (string)obj;
}

set
{
ViewState["Image"] = value;
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////------//////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

Lemme know if it works or is what you want to do. I havent had the time to
test it or try it out yet :)
Alessandro
 
J

Justin Weinberg

Thanks for the respone jline. Looks good.

If you know, do I add any deployment requirements by referencing the DTE?


jline said:
The following code is a quick hack, but I believe it demonstrates a
"correct" solution to your problem. If you are going to use EnvDTE make
sure that you are retrieving a reference to it correctly (IE - don't make
system calls looking for the EnvDTE interface)... GetService is the way to
go. Remember to add a reference to EnvDTE to you project. Good Luck.


//This function finds the Active Project's Local Cache
//and uses it to create a working design-time path
private string GetTheDesignTimePath(string FilePath){

string ProjectBasePath = "";
string ActiveDocPath = "";

EnvDTE._DTE env = (_DTE)GetService(typeof(_DTE));
if(env != null)
{
EnvDTE.Project proj;

proj = env.ActiveDocument.ProjectItem.ContainingProject;
ActiveDocPath = env.ActiveDocument.Path;

int index = 1;
while(index <= proj.Properties.Count) {
if(proj.Properties.Item(index).Name == "ActiveFileSharePath") {
ProjectBasePath = proj.Properties.Item(index).Value.ToString();
index = proj.Properties.Count;
}
index++;
}
}

string ReturnPath;
if (FilePath.IndexOf("\\\\") >= 0 || FilePath.IndexOf(':') >= 0)
throw new Exception("Invalid Path");

FilePath = FilePath.Replace('/', '\\'); if(FilePath[0] != '\\') {
//RELATIVE PATH
ReturnPath=Path.Combine(ActiveDocPath.Substring(0,
ActiveDocPath.LastIndexOf('\\')), FilePath);
}
else
{
//ABSOLUTE PATH
ReturnPath=Path.Combine(ProjectBasePath.Substring(0,
ProjectBasePath.LastIndexOf('\\')), FilePath.Substring(1));
}

return ReturnPath;
}



I'm resposting because I think I stated my queston poorly and having
answered questions myself, realize previously answered ones go
unnoticed.

The question is - How does the Microsoft Image Control get the "actual
path" to the image to render it at design time. The control takes a
relative URL and "like magic" renders a design time preview of the
relative specified path.

I've seen solutions that use ENVDTE - is this how how this control works
internally?

The Site property and getService look real promising, but I can't seem
to find what I'm looking for.

The question has been asked a lot (I google grouped for it for the
better part of yesterday), but never adequately anwered.

Thanks,

Justin Weinberg
 
J

Justin Weinberg

Appreciate both of the responses. The IE Webcontrol code is exceptional and
really informative. After examining int closely, they never actually have a
full path to the URL, but depend on the writer to render it appropriately at
design time.

So for my purposes, I'll have to go with the ENVDTE related solutions. Many
thanks!

--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com
 
A

Arnaud PICHERY

Just read your question for the first time. I don't know if I correctly
understood it but if you have the relative url of an image, and you want the
full url for it, you just need the url of the current document.
It can retrieved using the "standard" service IWebFormsDocumentService

IServiceProvider serviceProvider = ... (here retrieve some service provider.
Depending on the context it can be a call to Component.Site, or any other
way)
IWebFormsDocumentService webFormsDocumentService =
serviceProvider.GetService(typeof(IWebFormsDocumentService)) as
IWebFormsDocumentService;
if (webFormsDocumentService == null)
throw new ConfigurationException("Unable to retrieve the Web Forms
Document Service within current context.");
string documentUrl = webFormsDocumentService.DocumentUrl;

there you got it.

Don't know if it will help you.
Arnaud :O)
 
A

Arnaud PICHERY

Hi !

I have a design-time problem that puzzle me...
I am building a WebControls library and one of the control inherits from
Chart which is located in the DundasWebChart WebControls library.
When I adds a legend to this charting control its serialized form is similar
to something like this:
<myCompany:Chart runat=server ...>
<Legend>
<dcwc:LegendText ... />
</Legend>
</myCompany>

"myCompany" is the TagPrefix of my WebControls library, and "dcwc" the
TagPrefix of DundasWebChart.
The problem is that when I drag'n'drop my control from the ToolBox, Visual
Studio .NET only adds one Register directive, the one
for my WebControls library.
<%@ Register TagPrefix="myCompany" Namespace="MyCompany.WebControls"
Assembly="MyCompany.WebControls" %>

But I definitely need the Register directive for the Dundas WebControls
library in order for my control to work (both at design-time
and runtime).
The question is: Is there a way for me to add the Register directive for the
Dundas WebControls library programmatically? Inside the
ControlDesigner.OnSetParent method for example?
I know how to retrieve the registered directives using
IWebFormReferenceManager.GetDirectives() but can't find a way to add a
directive.

In the same way, is there a way to access and modify to the content of the
aspx file at design-time?..To add a Register Directive for example...:O)

Thanks in advance,
Arnaud :O)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top