From Xaml-String to Wpf-Window - c#

I keep having problems with creating a Wpf-Window from a Xaml-String.
What works:
I have a Server running which can receive Strings from remote Clients. (That is not complicated and works perfectly)
What doesn’t work:
Sometimes the Server receives a String with the content of a Xaml-File. Know I want to create a window dynamically with all the content specified in the Xaml-String.
Is there an easy way to do that?

var window = XamlReader.Parse(SomeXamlString) as Window;

Related

End page background working, WP8, C#

I dont know if it is even possible, but is there some way how to "end" page in Windows Phone 8 app?
My problem is that i am using one delegate (to know when is my xml downloaded) on multiple pages. It works fine, but when i open one page, she initialize herself, i go on other page (trough back button) and new page initialize herself too. Everything is fine, but the previous page is still listening to the delegate and it is really big problem. So i need to get the previous page (that closed) into a same state like she was not ever opened.
I will be thankful for any advice (maybe i am thinking in wrong way now, i dont know, maybe the page just have to be de-initialize).
PS: If its necessary i will post the code, but i think it is not. :)
Okey here is some code:
In class whis is downloading XML i have delegate like this:
public delegate void delDownloadCompleted();
public static event delDownloadCompleted eventDownloadCompleted;
This class is downloading few different xml files depends of constructor in run(int number) method.
After is download complete and all information from xml are saved in my local list i call delegateCompled. if (eventDownloadCompleted != null)
{
eventDownloadCompleted();
}
Then i have few different pages. All pages are used for display specific data from downloaded xml. So on this specific page I have method that is fired when "downloadClass" says it is complet.
XML_DynamicDataChat.delDownloadCompleted delegMetoda = new XML_DynamicDataChat.delDownloadCompleted(inicialiyaceListu);
XML_DynamicDataChat.eventDownloadCompleted += delegMetoda;
This is that "inicializaceListu" method:
private void inicialiyaceListu()
{
Dispatcher.BeginInvoke(() =>
{
model = new datka();
// object model is just model where i am saving all specific list of informations that i got from xml files.
chatList9 = model.getChat(1);
gui_listNovinky.ItemsSource = chatList9;
gui_loadingGrid.Visibility = Visibility.Collapsed;
});
}
All of these works fine, but when i go back (with back button) and open other specific page with other specific information from other downloaded xml, previous page is still listening for the delegate and inicialiyaceListu() method is still fired everytime i complete download of xml.
So i need to say previous page something like: "hey page, you are now closed! Can you shut the **** up and stop work?!?"
I think that specific delegate for each pages could solve this, but it is not correct programing way.
I solved it nice and easy. It is really simple solution. I just created bool variable and set it false when i go back. In inicializaceListu() i have condition if it is true. If it is true do that stuffs when false do nothing.

Get selected text from another app into own C# app

I've a C# app that responds correctly to a global hotkey. I want to get the selected text from another app which has the focus if the global hotkey is pressed.
I tested native Win32 Api, then SendKeys (CTRL + C, Clipboard,...) and now Microsoft UI Automation! The problem is, this only works in Notepad, but not in Internet Explorer or Word or other apps.
I think there must be a better solution than the code I have. I read that sending CTRL + C should work fine, but that only works in Notepad too.
Here's the method I call when the global hotkey is fired:
public String GetSelectedTextFromApp()
{
String output = "";
AutomationElement focused = AutomationElement.FocusedElement;
object pattern;
TextPatternRange[] trs;
if (focused.TryGetCurrentPattern(TextPattern.Pattern, out pattern))
{
TextPattern tp = (TextPattern)pattern;
trs = tp.GetSelection();
output = trs[0].GetText(-1);
}
return output;
}
The control you're trying to automate might not be implementing TextPattern. It might only implement ValuePattern. Also, it's possible that the control you're focused on isn't the control which actually contains the text, but rather a wrapping control under which you'll find an AutomationElement implementing the above mentioned patterns. Another possibility is, like Andrii said, is that the control doesn't support UIA at all (though I find it hard to believe in case of MS Word).
In such cases, it's worth working with UISpy.exe which will help you see the visual tree of the application you're trying to automate. You'll also be able to see the supported Patterns for each AutomationElement. You can also call GetSupportedPatterns() on every AutomationElement, to see which Patterns are currently supported.

How can you programmatically detect if javascript is enabled/disabled in a Windows Desktop Application? (WebBrowser Control)

I have an application which writes HTML to a WebBrowser control in a .NET winforms application.
I want to detect somehow programatically if the Internet Settings have Javascript (or Active Scripting rather) disabled.
I'm guessing you need to use something like WMI to query the IE Security Settings.
EDIT #1: It's important I know if javascript is enabled prior to displaying the HTML so solutions which modify the DOM to display a warning or that use tags are not applicable in my particular case. In my case, if javascript isn't available i'll display content in a native RichTextBox instead and I also want to report whether JS is enabled back to the server application so I can tell the admin who sends alerts that 5% or 75% of users have JS enabled or not.
Thanks to #Kickaha's suggestion. Here's a simple method which checks the registry to see if it's set. Probably some cases where this could throw an exception so be sure to handle them.
const string DWORD_FOR_ACTIVE_SCRIPTING = "1400";
const string VALUE_FOR_DISABLED = "3";
const string VALUE_FOR_ENABLED = "0";
public static bool IsJavascriptEnabled( )
{
bool retVal = true;
//get the registry key for Zone 3(Internet Zone)
Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3", true);
if (key != null)
{
Object value = key.GetValue(DWORD_FOR_ACTIVE_SCRIPTING, VALUE_FOR_ENABLED);
if( value.ToString().Equals(VALUE_FOR_DISABLED) )
{
retVal = false;
}
}
return retVal;
}
Note: in the interest of keep this code sample short (and because I only cared about the Internet Zone) - this method only checks the internet zone. You can modify the 3 at end of OpenSubKey line to change the zone.
If you are having troubles with popups popping up, i've included a solution for you, and if you want to disable/enable javascript on th client machine (or even just read/query if it is enabled/disabled) ive included that answer for you as well, here we go:
Which popup message do you want to disable? If it's the alert message, try this, obviously resolving the window or frame object to your particular needs, I’ve just assumed top-level document, but if you need an iframe you can access it using window.frames(0). for the first frame and so on... (re the JavaScript part)... here is some code, assuming WB is your webbrowser control...
WB.Document.parentWindow.execScript "window.alert = function () { };", "JScript"
You must run the above code only after the entire page is done loading, i understand this is very difficult to do (and a full-proof version hasn't been published yet) however I have been doing it (full proof) for some time now, and you can gather hints on how to do this accurately if you read some of my previous answers labelled "webbrowser" and "webbrowser-control", but getting back to the question at hand, if you want to cancel the .confirm JavaScript message, just replace window.alert with window.confirm (of course, qualifying your window. object with the correct object to reach the document hierarchy you are working with). You can also disable the .print method with the above technique and the new IE9 .prompt method as well.
If you want to disable JavaScript entirely, you can use the registry to do this, and you must make the registry change before the webbrowser control loads into memory, and every time you change it (on & off) you must reload the webbrowser control out and into memory (or just restart your application).
The registry key is \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ - the keyname is 1400 and the value to disable it is 3, and to enable it is 0.
Of course, because there are 5 zones under the Zones key, you need to either change it for the active zone or for all zones to be sure. However, you really don't need to do this if all you want to do is supress js dialog popup messages.
Let me know how you go, and if I can help further.
Here is a suggestion - Encode the warning into your webpage as default. Create a javascript that runs on page load which removes that element. The warning will be there when ever javascript is not allowed to run.
It's a long while since I coded client side HTML javascript to interact with the DOM so I may be a little out of date. i.e. you will need to fix details, but I hope I get the general idea across.
<script>
document.getElemntByID("noJavascriptWarning").innerHTML="";
</script>
and in your HTML body
<DIV id="noJavascriptWarning" name="noJavaScriptWarning">Please enable Javascript</DIV>

CodedUI Test: Keyboard.SendKeys not working

I'm trying to use the CodedUI Test feature of Visual Studio 2010.
I've got a problem while replaying the various actions for one of my html component. The Keyboard.SendKeys generated do not work (like if there was no input).
The code generated is :
// Type '{F4}{F4}{F2}titre{Enter}' in 'SaisieSD_DS' custom control
Keyboard.SendKeys(uISaisieSD_DSCustom, this.Params.UISaisieSD_DSCustomSendKeys, ModifierKeys.None);
If I replace the call to Keyboard.SendKeys by a call to System.Windows.Forms.SendKeys.SendWait, it does work.
I was thinking about a problem due to a loss of focus. However, if i do something like uISaisieSD_DSCustom.SetFocus(), it doesn't change the behavior.
Do you have any idea ?
thx.
Have you tried
uISaisieSD_DSCustom.WaitForReady()
Or one of the other waitfors?
Is it failing on this line? Or is it failing afterward due to this not working correctly?
You can also use the following to wait for all threads to complete before proceeding:
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
Keyboard.SendKeys(uISaisieSD_DSCustom, this.Params.UISaisieSD_DSCustomSendKeys, ModifierKeys.None);
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
Just make sure you include the last line to turn it back to UIThreadOnly, or it will slow everything way down.
Visual Studio CodedUI Test searches for a control and sends those keys to it. In your case the control is 'uISaisieSD_DSCustom'.
You can try using:
Keyboard.SendKeys(this.Params.UISaisieSD_DSCustomSendKeys);
OR
Keyboard.SendKeys("{F4}{F4}{F2}titre{Enter}");
After typing the URL if we want to send the enter key then the below code works in Coded UI
Keyboard.SendKeys("{Enter}");

Silverlight 2 - Download file - IE Blocks file download

I'm having a problem with IE only in my Silverlight application. Since Silverlight 2 doesn't include a mechanism for file downloads I was left to my own devices to come up with a solution. The way I did it was as follows:
HtmlPage.Window.Navigate(new Uri(sb.ToString(), UriKind.Relative));
My StringBuilder contains the relative url with query string to a *.ashx handler on the server that reads the query string, gets some data from the database, and returns an Excel file.
When I do this I get a blocked file download bar in IE only. I understand that this is a new security "feature" in IE and that it is being blocked because it believes that the download wasn't triggered by the user interaction with the web page. The users can choose to allow the download and that setting seems to be remembered for the rest of the session. However next time they open the page it happens again. Even if the site is in the Trusted zone and even if the popup blocker is configured to allow popups for the site.
Does anyone know how to make IE know that the user did in fact request this file?
I had exactly the same problem. The solution for me was to not use HtmlPage.Window.Navigate, but to instead use a HyperlinkButton and dynamically set the NavigateUri property.
Saving and restoring the app state as suggested above didn't work. Well, it did, but it was impossible to determine when it needed to be done and when it didn't. So, ultimately, it didn't really work.
See this discussion on codeplex....
http://slideshow2.codeplex.com/Thread/View.aspx?ThreadId=60242
Try HtmlPage.PopupWindow instead of HtmlPage.Window.Navigate. This has helped me get around IE's "Automatic prompting for file downloads" setting being disabled by default for Internet zone sites.
This is my code solution to open URL for download and override Automatic prompting for file downloads option issue in IE 8.
It also use HyperlinkButton, but all is called from code:
public class BrowserHelper
{
private sealed class HyperlinkButtonCaller : HyperlinkButton
{
public static void OpenUrl(Uri url)
{
var button = new HyperlinkButtonCaller()
{
NavigateUri = url
};
button.OnClick();
}
}
public static void OpenUrl(Uri url)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
HyperlinkButtonCaller.OpenUrl(url);
}
}
BrowserHelper.OpenUrl(new Uri(ClientGlobalInfo.Current.ApplicationUrl, "myhandler.ashx"));
In my case only happended the first time (using IE 7), maybe that happens on your local dev instance?
I think there's not much you can do, even MSDN pages tells you that "a message will appear on top of...", things that could mitigate this:
Warn the user that the message will be shown, then once he clicks the app is reset (Store the current app state in the isolated storage, when you receive the reset reload the app with the settings).
Open a popup, and in the popup include and standard HTML button to download.
HTH
Braulio

Categories

Resources