Showing a build number or version on Silverlight app? - silverlight

I have a Silverlight app that I hand off to the IT people you are supposed to publish it. This does not always happen correctly, they might forget, put in the wrong loc, or get the wrong version.
So I want to add something to the app itself to easily tell the latest version. This is for SL 3 and 4 so context menu probably not an option. I don't think the users would want a splash screen so that also probably not a choice.
Has anyone done this? Is there a clean, unobtrusive way of doing this?
If you have done this do you just use the assembly version or do use some custom value?
thanks

Here is code to check the assembly version of a running application:
string GetAssemblyVersion()
{
var assemblyName = new AssemblyName(Application.Current.GetType().Assembly.FullName);
if (assemblyName == null)
{
return string.Empty;
}
Version v = assemblyName.Version;
if (v == null)
{
return string.Empty;
}
return v.ToString();
}

Related

Where have my config settings gone?

After some long and complicated stories, I came upon code very similar to this, and have been using it fine for months. Yesterday I changed a great many things, and now this code no longer works.
public void OpenConfig(string configDir)
{
if (string.IsNullOrWhiteSpace(configDir))
{
configDir = AppDomain.CurrentDomain.BaseDirectory;
}
var sharedConfigPath = Path.Combine(configDir, ConfigFileName);
var map = new ExeConfigurationFileMap { ExeConfigFilename = sharedConfigPath };
sharedConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
logger.Trace("'{0}' set 'SharedConfigPath' to '{1}'.", GetType().Name, sharedConfig.FilePath);
}
Then, I get config values like this:
ServicePollingInterval = GetIntegerAppSetting("ServicePollingInterval"),
where
private static int GetIntegerAppSetting(string settingName, bool throwOnMissing = false)
{
string setting = null;
if (settingTag != null)
var settingTag = sharedConfig.AppSettings.Settings[settingName];
{
setting = settingTag.Value;
}
if (setting == null)
{
if (throwOnMissing)
{
logger.Trace("App Setting '{0}' not configured. Throwing an exception.", settingName);
throw new EalsConfigurationException(settingName, "App Setting not configured.");
}
logger.Trace("App Setting '{0}' not configured. Defaulting to -1.", settingName);
return -1;
}
int i;
if (!int.TryParse(setting, out i))
{
logger.Trace("App Setting '{0}' not valid as Integer. Throwing an exception.", settingName);
throw new EalsConfigurationException(setting, "App Setting '{0}' not valid as Integer.");
}
return i;
}
But, now basically overnight, the call var settingTag = sharedConfig.AppSettings.Settings[settingName]; returns settingTag as null, because there are no appSettings items in that collection.
I have been working on this project for months, and I put this config code in way in the beginning because I had several executables running in the same folder, and I want them to all use the same config. I am really stumped (not surprised) that I made no memorable code changes to config.
Can anyone see what I might have screwed up, or guess at what external influences I may have changed, or at anything that could suddenly cause all the sections of this Configuration object to be empty.
One suspicion I have, but cannot trace, is a change in what user that app runs as. It's complicated: I have a WCF service hosted in a Windows Services, consumed by a WPF application.
Trial and error has brought to light some sort of answer. When coding and debugging with separate WCF library, host, and client, projects, you need the same config settings in all three projects. Sometimes VS2013 (I dunno about others) auto-hosts the WCF library, in some unknown process somewhere, and for this reason you need a copy of app.config in the library project, even though it is not an executable.

WPF+PRISM on windows 8 region.active crash

I'm working om project base on WPF .net 4 with Prism 4.0,
after I upgrade my machine from windows 7 to windows 8,
the project crash when reach the point of active view
object view = s.GetView(viewName);
if (view != null)
{
theRegion = (Region)s;
s.Activate(view);
}
The exception occur in the line s.Activate(view)
the exception details is"Value cannot be null.Parameter name: view"
This is strange because I check that the view isn't null.
on windows 7 it's working
any advice ?
Thanks
Yair
Hmmmm, that seems like a threading issue to me. Is there another piece of code that could be potentially running in parallel and also getting the view, and modifying it between the time the if statement and your Activate method is called?
If so, the double-check locking pattern would fix your problem.
if (view != null)
{
lock (myLock) // myLock is just a static object
{
if (view != null)
{
theRegion = (Region)s;
s.Activate(view);
}
}
}
Of course, you would also need to lock myLock in other places where you are modifying your view.

Embedding word 2010 editor in a wpf application

How do I use the word editor in a WPF application? Is it possible using windows forms hosting in WPF only? Is there another way to accomplish that?
I found AvalonEdit but it does not have features that I need. So using this way, my problem may not be solved.
Also there is some stuffs out there to host a windows forms control in WPF, but it could not be my answer.
I want to understand that is there a way to use word editor in a native way in a wpf app?
Will all APIs be available in that solution?
Thanks in advance.
You can host MS Word (2007/2010 and probably other versions) from within a WebBrowser control, this works in WinForms and should work in WPF too. A .NET API is provided for automating Word, documented here. The required interop assemblies ship with Office 2010, so deployment is a lot simpler than previous Office versions.
See this Microsoft Support article for more details on hosting Word within a WebBrowser control. The Screenshot below shows Word embedded within a host Winforms application.
Note that this only works reliably for a single hosted instance of Word, so you can't show 2 Word documents side by side in the same application. Also, the Ribbon can sometimes go missing - but Word hasn't ever caused the application to crash.
Administrative rights are required to make the required registry updates as there are potential security issues. One easy method to make the registry updates is to write a script, but the following (revised/untested) code shows how this can be done in c# for Word, Excel and PowerPoint:
using System.Security.AccessControl;
private Dictionary<string,uint> OfficeBrowserRegKeys()
{
string[] officeRegKeyArray = new string[]
{
#"SOFTWARE\Classes\Word.Document.12",
#"SOFTWARE\Classes\Word.DocumentMacroEnabled.12",
#"SOFTWARE\Classes\Excel.Sheet.12",
#"SOFTWARE\Classes\Excel.SheetMacroEnabled.12",
#"SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12",
#"SOFTWARE\Classes\PowerPoint.Show.12",
#"SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12",
#"SOFTWARE\Classes\PowerPoint.SlideShow.12",
#"SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12"
};
Dictionary<string,uint> officeRegKeys = new Dictionary<string, uint>();
uint wrdVal = 0x80000024;
uint excelVal = 0x80000A00;
uint powerPtVal = 0x800000A0;
foreach(string keyName in officeRegKeyArray)
{
if (keyName.Contains("Word"))
{
officeRegKeys.Add(keyName, wrdVal);
}
else if (keyName.Contains("Excel"))
{
officeRegKeys.Add(keyName, excelVal);
}
else
{
officeRegKeys.Add(keyName, powerPtVal);
}
}
return officeRegKeys;
}
private void setNewOfficeKeys()
{
uint editFlag = 0x00010000;
Dictionary<string,uint> officeRegKeys = OfficeBrowserRegKeys();
foreach (KeyValuePair<string, uint> kvp in officeRegKeys)
{
try
{
RegistryKey rKey = Registry.LocalMachine.OpenSubKey(kvp.Key,
RegistryKeyPermissionCheck.ReadWriteSubTree,
System.Security.AccessControl.RegistryRights.SetValue);
rKey.SetValue("BrowserFlags", unchecked((int)kvp.Value),
RegistryValueKind.DWord);
rKey.SetValue("EditFlags", unchecked((int)editFlag),
RegistryValueKind.DWord);
}
catch (Exception e) { string msg = e.Message; }
}
}
Well, Word proper isn't technically designed to be hosted by another app, whether it's WPF, WINFORMS or anything else.
You CAN use api tricks (like SetParent) to move the Main Word window into a WPF hosted window. I've done it before, but it's pretty tricky business and it's very easy to miss things that cause GPFs (both in Word and your app).
Is there any reason why it needs to be "Word in your app"? Why not write a little word addin and then launch Word from your app when necessary. then the Addin can communicate with your app, or your DB or whatever as necessary from within Word.
Users may find that to be a more usable approach in any case.

Internet Explorer 9 RC stops my WinForms WebBrowser control to work in editing mode

Using the IHtmlDocument2.designMode property set to On to switch a WebBrowser control hosted on a Windows Forms form to editing mode suddenly stopped working after installing Microsoft Internet Explorer 9 RC.
Question:
Any chance to fix this?
I already tried to tweak with doctype or with the EmulateIE7 meta tag but without success.
(An example would be this project)
Update 2011-02-21:
As Eric Lawrence suggested, I adjusted the "Zeta" example to set the document text before setting the edit mode.
Unfortunately I did not manage to switch to design mode, either.
Update 2011-02-24:
Parts of the discussion also take place in Eric's blog.
Update 2011-02-26:
What I currently eperience is that the behaviour seems to be different for HTTP URLs and for content that was added via WebBrowser.DocumentText.
First tests seems to prove this assumption.
I'm now going to build a solution around this assumption and post updates and a proof-of-concept here.
Update 2011-02-26 (2):
I've now built a proof-of-concept with a built-in web server which I believe is also working well with IE 9. If anyone would like to download and test whether it is working and give me a short feedback, I can clean up and release the source code for this.
Update 2011-02-26 (3):
No feedback yet, I still updated the HTML Edit Control article and demo over at the Code Project.
Update 2011-03-16:
Since Internet Explorer 9 was released yesterday, we updated our major products to use the idea with the integrated web server as described in the HTML Edit Control article.
After nearly a month of testing, I think it works quite well.
If you do experience any issues in the future with this approach, please post your comments here and I can investigate and fix.
I had a similar problem and got around it by adding the following line to the DocumentCompleted event:
((HTMLBody)_doc.body).contentEditable = "true";
We just need an empty editable control. I did however step through debugger and add value to the control's InnerHtml and it displayed it fine, and I could edit it.
Small update, we were able to get the control editable using this line also:
browserControl.browser.Document.Body.SetAttribute("contentEditable", "true");
This allows us to avoid referencing mshtml, (don't have to include Microsoft.mshtml.dll)
This lets us avoid increasing our installation size by 8 megs.
What's your exact code?
If I set the following code:
private void cbDesign_CheckedChanged(object sender, EventArgs e){
var instance =
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(
wbView.ActiveXInstance,
null,
#"Document",
new object[0],
null,
null, null );
var objArray1 = new object[] { cbDesign.Checked ? #"On" : #"Off" };
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSetComplex(
instance,
null,
#"designMode",
objArray1,
null,
null,
false,
true );
The IE9 Web Browser instance enters designMode without any problems. If you change the "Zeta" example to not set the document text after entering design mode, it also works fine.
Just want to add that I am also unable to enter designmode (using a WebBrowser control in my case). This was not an issue in the beta. Definitely new with the RC.
Another Code Project user suggested to use the following code:
First, add event DocumentCompleted:
private void SetupEvents()
{
webBrowser1.Navigated += webBrowser1_Navigated;
webBrowser1.GotFocus += webBrowser1_GotFocus;
webBrowser1.DocumentCompleted += this.theBrowser_DocumentCompleted;
}
Then write the function:
private void theBrowser_DocumentCompleted(
object sender,
WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Write(webBrowser1.DocumentText);
doc.designMode = "On";
}
Although I did not test this, I want to document it here for completeness.
It's fixed if the property is set after the document is loaded
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
IHTMLDocument2 Doc = Document.DomDocument as IHTMLDocument2;
Doc.designMode = #"On";
}
Yesterday, Internet Explorer 9 RTM finally was released.
I did some more tiny adjustments to my control, but basically the idea with the intergrated, small web server seems to work rather well.
So the solution is in this Code Project article:
Zeta HTML Edit Control
A small wrapper class around the Windows Forms 2.0 WebBrowser control
This was the only solution that worked for me.
I hope it is OK to answer my own question and mark my answer as "answered", too?!?
I was also able to get this to work using the following inside the DocumentCompleted event:
IHTMLDocument2 Doc = browserControl.browser.Document.DomDocument as IHTMLDocument2;
if (Doc != null) Doc.designMode = #"On";
Thanks everyone!
I use HTML Editor Control, I solved this problem adding the DocumentComplete event
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
(((sender as WebBrowser).Document.DomDocument as IHTMLDocument2).body as HTMLBody).contentEditable = "true";
}

Version detection with Silverlight

How can I efficiently and effectively detect the version and, for that matter, any available information about the instance of Silverlight currently running on the browser?
The Silverlight control only has an IsVersionSupported function, which returns true / false when you give it a version number, e.g.:
if(slPlugin.isVersionSupported("2.0")) {
alert("I haz some flavour of Silverlight 2");
You can be as specific as you want when checking the build, since the version string can include all of the following:
major - the major number
minor - the minor number
build - the build number
revision - the revision number
So we can check for a specific build number as follows:
if(slPlugin.isVersionSupported("2.0.30523")) {
alert("I haz Silverlight 2.0.30523, but could be any revision.");
Silverlight 1.0 Beta included a control.settings.version property, which was replaced with the isVersionSupported() method. The idea is that you shouldn't be programming against specific versions of Silverlight. Rather, you should be checking if the client has at least verion 1.0, or 2.0, etc.
That being said, you can get the Silverlight version number in Firefox by checking the Silverlight plugin description:
alert(navigator.plugins["Silverlight Plug-In"].description);
Shows '2.0.30523.8' on my computer.
Note that it is possible to brute force it by iterating through all released version numbers. Presumably that's what BrowserHawk does - they'll report which version of Silverlight the client has installed.
I got this from http://forums.asp.net/p/1135746/1997617.aspx#1997617 which is the same link Stu gave you. I just included the code snippet.
Silverlight.isInstalled = function(d)
{
var c = false, a = null;
try
{
var b = null;
if(Silverlight.ua.Browser == "MSIE")
b = new ActiveXObject("AgControl.AgControl");
else
if(navigator.plugins["Silverlight Plug-In"])
{
a = document.createElement("div");
document.body.appendChild(a);
a.innerHTML = '<embed type="application/x-silverlight" />';
b = a.childNodes[0]
}
if(b.IsVersionSupported(d))
c = true;
b = null;
Silverlight.available = true
}
catch(e)
{
c=false
}
if(a)
document.body.removeChild(a);
return c
};
found this site that detects the full version of silverlight- silverlight version (aka silverlightversion.com)
As mentioned in the above comments there is currently no efficient direct way to get the installed Silverlight version number (that works cross browser platform).
I wrote a post on how to workaround this problem and detect the Silverlight major version number (including version 3) programmatically and more efficiently using JavaScript.
You can find the code and the post at:
http://www.apijunkie.com/APIJunkie/blog/post/2009/04/How-to-programmatically-detect-Silverlight-version.aspx
Good luck!
Environment.Version will do what you want! Supported since Silverlight 2.0
Look in silverlight.js:
http://forums.asp.net/p/1135746/1997617.aspx#1997617

Resources