How to work with webClient.OpenReadCompleted event handler? - silverlight

I'm using the following code and it works nice
private void somethingButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
webClient.OpenReadAsync(new Uri(myUri));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if(e.Error != null)
{
messageTextBlock.Text = e.Error.Message;
return;
}
using (Stream s=e.Result)
{
XDocument document = XDocument.Load(s);
var q1 = from c in document.Descendants("result")
select new IndeedResult
{
Title =((string)c.Element("title")).Trim(),
ResultUri = ((string)c.Element("url")).Trim(),
Date = ((string)c.Element("date")).Trim(),
};
myDataGrid.ItemsSource = q1;
}
But I want to add another WebClient, webClient2 that will do exactly the same but has different uri and different structure so I will have webClient2_OpenReadCompleted...
The problem is that finally I need to merge (or do some logic before merge) var q1 from webClient_OpenReadCompleted and var q2 from webClient2_OpenReadCompleted and then
var mergedQs = q1.Union(q2).ToList();
myDataGrid.ItemsSource = mergedQs
Is there a simple way how to do it? I don't know how to do it using those event handlers.

Good question! Did it like this
void webClient_OpenReadCompleted( object sender, OpenReadCompletedEventArgs e )
{
Stream stream = (Stream)e.Result;
BinaryReader reader = new BinaryReader( stream );
byte[] buffer = reader.ReadBytes( (int)stream.Length );
Uri uri = (Uri)e.UserState;
streams.Add( uri.AbsoluteUri, new MemoryStream( buffer ) );
}
Note the usage of UserState in order to provide a unqiue key into my streams Dictionary. Works perfectly :-) This way you can work with as many files / images / binary data as you like!
See this to see just how powerful this can be....http://www.alansimes.com/warp3dsilverlighttestpage.html

Related

How do I change the sound file for my windows form application using combo box?

I am developing a Windows Form Application. Currently I am working on the Settings aspect for my windows form app. On the settings form, I would be able to toggle the alert tone for my application. The default sound code is as follows
public String defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\applause-2.wav";
As for the settings, I have included 2 default tones for the user to select through a combo box. The code for the combo box are as follows,
private void comboBoxSound_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxSound.SelectedIndex == 0)
{
ReportStatus("Alert tone changed to 'Beep(1)'!");
backgroundFormObject.getSetting().defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-1.wav";
}
else
{
ReportStatus("Alert tone changed to 'Beep(2)'!");
backgroundFormObject.getSetting().defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-2.wav";
}
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Stream stream = File.Open(appPath + "\\setting.sd", FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, backgroundFormObject.getSetting());
stream.Close();
}
Why is it that whenever I select another tone, and I play the sound, the effect is still the same as the original alert tone which is applause. Do I have to wait for the file to load finish before I play?
managed to solve it myself using the following code
private void comboBoxSound_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxSound.SelectedIndex == 0)
{
ReportStatus("Alert tone changed to 'Beep(1)'!");
settingObject.defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-1.wav";
}
else
{
ReportStatus("Alert tone changed to 'Beep(2)'!");
settingObject.defaultAlertTone = Path.GetDirectoryName(Application.ExecutablePath) + "\\Sounds\\beep-2.wav";
}
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Stream stream = File.Open(appPath + "\\setting.sd", FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, settingObject);
stream.Close();
}

WP7 Audio Streaming Help

So I've downloaded the samples from http://archive.msdn.microsoft.com/ManagedMediaHelpers.
I've got my code working using MP3MediaStreamSource. However, I don't fully understand the code would like some explanation.
public partial class MainPage : PhoneApplicationPage
{
private static string mediaFileLocation = "http://file-here.mp3";
private static HttpWebRequest request = null;
private static Mp3MediaStreamSource mss = null;
public MainPage()
{
InitializeComponent();
}
private void RequestCallback(IAsyncResult asyncResult)
{
HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse;
Stream s = response.GetResponseStream();
mss = new Mp3MediaStreamSource(s, response.ContentLength);
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
this.wp7AudioElement.Volume = 100;
this.wp7AudioElement.SetSource(mss);
});
}
private void Button_Click(object sender, RoutedEventArgs e)
{
request = WebRequest.CreateHttp(MainPage.mediaFileLocation);
// NOTICE
// Makes this demo code easier but I wouldn't do this on a live phone as it will cause the whole
// file to download into memory at once.
//
// Instead, use the asynchronous methods and read the stream in the backgound and dispatch its
// data as needed to the ReportGetSampleCompleted call on the UIThread.
request.AllowReadStreamBuffering = true;
IAsyncResult result = request.BeginGetResponse(new AsyncCallback(this.RequestCallback), null);
}
}
It's really just the last method I need explained, I don't understand the Notice as to why it's a bad idea and how to do it differently?
Basically, it is trying to tell you that you are downloading 1 file COMPLETELY before it plays. It is not a good idea, since if the file is 10 MB, it may take a while before it completely downloads.
A better idea would be to chunk the file using Encoders, and read it in on need basis.

A simple WPF Authentication

How to create a simple WPF Authentication for WPF application?
For example:
First time a user should registry then login.
Users login and password should be saved as txt file(encrypted).
If process of authentication is successful,then it should redirect to another existed window.
I'm a beginner in WPF.
I've searched about this question,but didn't find what I need.
I need a simple,step by step explanation of how to do it.
Thanks in advance! :)
I am also learning so in order to exercise a bit i have created a very simple example for you. It is probably unprofessional and unsafe but i think (hope) it is possible to extend it somehow :).
Firstly you need to create simple WPF windows (use txt/btn+Name naming convention):
For both windows add
using System.IO;
Then you need to add events for buttons and modify code for both windows:
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
// This is really bad/weak encryption method
String WeakDecryptMethod(String textIn)
{
Char[] temp = textIn.ToArray<Char>();
for (int i = 0; i < textIn.Length; i++)
{
temp[i] = (char)((int)temp[i] - 3);
}
return new String(temp);
}
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
RegisterWindow newWindow = new RegisterWindow();
newWindow.ShowDialog();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
// If file exist and login and password are "correct"
if (File.Exists("Users.txt")
&& txtLogin.Text.Length >= 4
&& txtPass.Text.Length >= 4)
{
using (StreamReader streamReader = new StreamReader("Users.txt"))
{
// While there is something in streamReader read it
while (streamReader.Peek() >= 0)
{
String decryptedLogin = WeakDecryptMethod(streamReader.ReadLine());
String decryptedPass = WeakDecryptMethod(streamReader.ReadLine());
if (decryptedLogin == txtLogin.Text && decryptedPass == txtPass.Text)
{
ProtectedWindow protectedWindow = new ProtectedWindow();
this.Close();
protectedWindow.Show();
break;
}
}
}
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
And code for Register window:
public partial class RegisterWindow : Window
{
public RegisterWindow()
{
InitializeComponent();
}
// This is really bad/weak method to encrypt files
String WeakEncryptMethod(String textIn)
{
Char[] temp = textIn.ToArray<Char>();
for (int i = 0; i < textIn.Length; i++)
{
temp[i] = (char)((int)temp[i] + 3);
}
return new String(temp);
}
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
// If file exist and login and password are "correct"
if (File.Exists("Users.txt")
&& txtLogin.Text.Length >= 4
&& txtPass.Text.Length >= 4
&& txtPass.Text == txtPassCheck.Text)
{
StringBuilder stringBuilder = new StringBuilder();
using (StreamReader streamReader = new StreamReader("Users.txt"))
{
stringBuilder.Append(streamReader.ReadToEnd());
}
using (StreamWriter streamWriter = new StreamWriter("Users.txt"))
{
streamWriter.Write(stringBuilder.ToString());
streamWriter.WriteLine(WeakEncryptMethod(txtLogin.Text));
streamWriter.WriteLine(WeakEncryptMethod(txtPass.Text));
}
this.Close();
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
In order to work application need to have access to file "Users.txt" which needs to be placed in the same folder.
Notes:
It will be better if you will use some proper encryption functions and probably create separate class for it. Additionally i am almost sure that it will not work properly with login and password which contains the last 3 characters from the end of ASCII tables.
In my opinion it is a bad idea to store login and password data in *.txt file :).
As far i know C# code is very easily reverse engineered so probably it will be better to hide encryption/decryption part somehow. I do not know much about it, but u will be able to read more [here] 2 and probably uncle Google will be able to help.
Code is very simple and there is probably a lot of possibilities to extend it (more file handling stuff, TextBox validation for proper input and password strength calculations)

How to load a UserControl in WPF with reflection?

private void Window_Loaded(object sender, RoutedEventArgs e)
{
var assm = Assembly.LoadFrom("wpflib.dll");
foreach (var t in assm.GetTypes())
{
var i = t.GetInterface("test.ILib");
if (i != null)
{
var tmp = Activator.CreateInstance(typeof(UserControl)) as UserControl;
this.stackPanel1.Children.Add(tmp);
}
}
}
tmp(UserControl1 in wpflib.dll) only contains a label and a textbox.
Windows1 (test.exe) reference ILib.dll, and only contains a stackPanel1.
But, why there is nothong in Windows1(stackPanel1)?
You are not instantiating the type from the DLL at all. Instead of:
var tmp = Activator.CreateInstance(typeof(UserControl)) as UserControl;
write:
var tmp = Activator.CreateInstance(t) as UserControl;
Furthermore, I would recommend that you actually write
var tmp = (UserControl) Activator.CreateInstance(t);
instead. Otherwise, if you have a bug, you will get a null-reference exception later on, which is not very informative and hard to debug. This way you get a more meaningful type-cast exception in the right place where the bug actually happens.

Stream a WAV File From The Web In Silverlight 3

I've managed to discover Gilles Khouzam's playback implementation for WAV files in Silverlight 3 and while that would be the majority of the battle, I'm stuck on a final detail: how do I pull a wav file from the web some place and then feed it into his WaveMediaStreamSource for playback?
Here's the closest I've come:
public MainControl()
{
// Required to initialize variables
InitializeComponent();
PlayButton.Click += PlayButtonClicked;
}
private void PlayButtonClicked(object sender, RoutedEventArgs e)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(#"soundfile.wav");
request.BeginGetResponse(ReadCallback, request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
WaveMediaStreamSource wavMss = new WaveMediaStreamSource(response.GetResponseStream());
MediaPlayer.SetSource(wavMss);
}
Edit:
It turns out the problem had to do with HttpWebRequest. Changing the code to:
public MainPage()
{
InitializeComponent();
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(#"http://www.russellmyers.com/somefile.wav", UriKind.Absolute));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
WaveMediaStreamSource wavMss = new WaveMediaStreamSource(e.Result);
Debug.WriteLine("Setting source...");
Media.SetSource(wavMss);
}
Works fine. This makes sense too after reading Shawn Wildermuth's article on the differences. I would like to get HttpWebRequest working though because it won't be done on the UI thread.

Resources