silverlight...hyperlinkbutton to file using relative path - silverlight

I am trying to use a hyperlinkbutton in silverlight to enable the user to download a word document. I don't care if a file save as box appears or if the word doc opens in a new browser. I get the error "cannot navigate to locations relative to a page." I've seen it posted that you can do this with the absolute path (www.domain.com/filename.doc) but there's got to be a way to make this relative (/docs/filename.doc). Anyone know how?

Slightly easier:
Uri myAbsoluteUri = new Uri(HtmlPage.Document.DocumentUri, myRelativePath);

The HyperlinkButton only works with absolute URLS, so you should fixup your URLs at runtime:
uriCurrent = System.Windows.Browser.HtmlPage.Document.DocumentUri;
string current = uriCurrent.OriginalString;
int iLastSlash = current.LastIndexOf('/') + 1;
current = current.Remove(iLastSlash, current.Length - iLastSlash);
from Silverlight.net forums.

Related

WPF webrowser control - flash working only first time

I have a very strange problem on my wpf application.
I use a webbrowser control to display a third-party web page that uses flash to provide some functionality.
On internet explorer 9 and 10 i have a particular behaviour : the first time I open the webbrowser control everything works fine, and the application puts a .SWF file inside my temporary internet files folder. When I close and reopen the webcontrol, the following error arise :
"object does not support property or method 'UpdateTimeLeft'".
If i manually delete the .SWF file inside the temporary folder, close ad reopen the application, everything works fine again.
This is the code that arise the exception :
var swfUrl = "//" + serverName + "/js/tv/FlashTurboLotto3.swf";
var expressSwfUrl = "//" + serverName + "/js/tv/expressInstall.swf";
var flashvars = {};
var params = {wmode:"transparent", allowscriptaccess:"always" };
var attributes = {id:"FlashTurboLotto3", name:"FlashTurboLotto3", align:"top"};
swfobject.embedSWF(
swfUrl,
"flashcontent",
"100%",
"197",
"9.0.0",
expressSwfUrl,
flashvars,
params,
attributes
);
function updateTimeleft(drawID, timeleft)
{
var swf = document.getElementById("FlashTurboLotto3");
swf.updateTimeleft(parseInt(drawID), timeleft);
}
does anyone have an idea of why this appens and how to solve the issue?
Thank in advance.
I found the solution - It is a very particular bug of Flash. Sometimes happens that when the flash application runs in a non-native browser (like, in my case, a webbrowser control in WPF) the javascript fails at retrieving the .swf file from the cache, so the Flash object will not get initialized.
I solved, for now, by forcing to not use the browser cache, simply adding a random value to the .Swf download Url.

WPF Dispay image from relative location

I would like to display an image on my dialog which could be located in a directory relative to the one the .exe is located in, e.g.
project
- data
-- logo //<-- that's where the image is located
-bin //<-- that's where the .exe is in
A default image should be included in the .exe but on displaying the dialog, the \data\logo directory should be checked first and if an image with the given filename could be found there that one should be used for display instead of the one that is inside the .exe.
Any ideas on how to do this?
Thanks,
tabina
Use the pack URI
pack://application:,,,/ReferencedAssembly;component/data/logo/image.png
As he asks for a folder relative to the exe, i suggest he means the logo should be located inside the filesystem (e.g. c:\program files\MyApp\data\logo). So I would check if the file exists with
File.Exists("/data/logo/logo.png")
If it returns false, you could load your embedded resource.
//Edit
If you use this snippet in your Visual Studio IDE the path is located at
<Project directory>/bin/debug/data/logo
what I am doing now is
// set the logo
string path = Environment.CurrentDirectory + "\\data\\logo\\logo.gif";
var uri = new Uri(path, UriKind.Absolute);
try
{
this.myImg.Source = new BitmapImage(uri);
}
catch (Exception e)
{
this.myImg.Source = new BitmapImage(new Uri(#"pack://application:,,,/myAssemblyName;component/Images/logo.gif"));
}
This works pretty well, but what I don't like about this is that it is all written in the code-behind. I would rather have it more re-usable.
Is there a way to put it all into a template and apply it to viewboxes/images?
Thanks,
tabina

Can i open directory from jump list

Im writing WPF application and want to add ability to call jump list and open program configuration, app.config or log directory from it. Is it possible(cant find the way to do that..just JumpTasks with application path and JumpPath with path to file, and not just path to be opened via explorer)?
Found answer here. Seems that JumpList wasnt designed for opening anything but files or applications, associated with current program. So that when we see directories in explorer tasklist -it actually means: use explorer with parameters. By the way ill try to use it.
Update
made it with such code:
string explorerPath = #"%windir%\explorer.exe";
JumpTask path = new JumpTask
{
CustomCategory = "Paths",
Title = "Open program directory",
IconResourcePath = explorerPath,
ApplicationPath = explorerPath,
Arguments = #"/root," + AppDomain.CurrentDomain.BaseDirectory,
Description = AppDomain.CurrentDomain.BaseDirectory
};
Im leaving this answer here, because someone can have similar incomprehension.

How to create Uri pointing to an local image in Silverlight?

I was wondering if it is allowed to read local images within a Silverlight client. (it is just to test a control)
var image = new BitmapImage { UriSource = new Uri("../Images/2.jpg", UriKind.Relative) };
Would this Uri be allowed and the image created? For some reason the picture doesn't show, despite getting no compiler errors that the Uri might have been wrong...
Thanks for advice,
kave
Assuming your project looks like this
- Project
- Images
- 2.jpg
- Another_Folder
- *your code file*
, that should be fine.
Remember that
../Images/2.jpg
is navigating to parent folder, then looking for an Images folder and a 2.jpg file.

FileSaveDialog changing my current working directory path

I am new to winforms and facing one problem in my application. I am trying to show the user to save a file using SaveFileDialog control on my form. But as soon as the user chooses his directory and saves his file, my next code which uses Directory.getCurrentDirectory() fails to point to my working directory. It seems that SaveFileDialog is causing problem here.
For eg:- I have these lines of code -
MessageBox.Show( Directory.GetCurrentDirectory( ) ); // output: C:\TestSamples\TestApp\Bin\Debug
fdSave.ShowDialog( ); // fdSave is SaveFileDialog control which will show to save file
MessageBox.Show( Directory.GetCurrentDirectory( ) ); //output: C:\Program files\outputDir (This is the path chosen by user to save file in previous saveDialog.)
Is there any solution for this so that my current working directory does not get changed or should I keep a variable to store my current working directory before saveDialog is used?
I found a possible answer in the comments on Raymond Chen's excellent blog:
in the class System.Windows.Forms.FileDialog, there is a property 'RestoreDirectory'
Well you could use the Application.StartupPath if you always wanted it to point to the bin directory

Resources