Daily deeds:Windows Phone dev issues, changing common Control Properties in codebehind. :)

আজকের টা বাংলায়! Smile
প্রায়শই যথন উইন্ডোজ ফোনটা নিয়ে গুতোগুতি চলে তখন প্রথমেই কোন কিছু যখন করতে যাই সর্বপ্রথম যে ব্যাপারটা নিয়ে চিন্তা ভাবনা চলে সেটা হচ্ছে XAML. কিন্তু গ্যাঞ্জামটা পাকে তখন যখন আমার মাথায় ডাইন্যামিক কিছু করার ভুত চাপে এবং UI কে C# থেকে Control করবার প্রয়োজন হয়। তখন XAML এর Attribute গুলো কিভাবে এখানে Assign করবো বের করতে বেশ ঝামেলা পোহাতে হয়। যদিও MSDN আছে তাও ভাবলাম, লিখে রাখি, দোষ কি? এখানে XAML এবং C# একসাথে নোট করা আছে, দেখা লাগলে দেখে নিতে পারেন। Smile

সমস্যা: TextBlock এর TextWrapping property
XAML:

<TextBlock x:Name="textBlockWrapped" TextWrapping="Wrap" Text="This should be wrapped."/>

C#:

this.textBlockWrapped.TextWrapping = TextWrapping.Wrap;

সমস্যা: Horizontal Alignment/Vertical Alignment ঠিক করা
XAML:

<Button x:Name="button" Content="Shift" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="63" />

C#:


button.HorizontalAlignment=(HorizontalAlignment)AlignmentX.Left;
button.VerticalAlignment=(VerticalAlignment)AlignmentY.Top

সমস্যা: ScrollBar Visibility ঠিক করা
XAML:


<ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Visible">
<!--some code here...-->
</ScrollViewer>

C#:


this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

সমস্যা: Font ঠিক করা
XAML:


<TextBlock Text="TextBlock" FontWeight="Bold" FontSize="30" FontFamily="Arial" x:Name="textBlock"/>

C#:


this.textBlock.FontSize = 30;
this.textBlock.FontFamily = new FontFamily("Arial");
this.textBlock.FontWeight = FontWeights.Bold;

যদি Font আগেই embedded হয় তো?

XAML:


<TextBlock Text="TextBlock" FontWeight="Bold" FontSize="30" FontFamily="YourProjectName;component/Fonts/Fonts.zip#Siyam Rupali" x:Name="textBlock"/>

C#:


this.textBlock.FontFamily = new FontFamily("YourProjectName;component/Fonts/Fonts.zip#Siyam Rupali");

সমস্যা: Button অথবা অন্য কোন Control কোন Grid বা Container এ যোগ করা

C#:


Button btn = new Button();
//ContentPanel is the main Grid in MainPage.xaml
this.ContentPanel.Children.Add(btn);

 

সমস্যা: Background পরিবর্তন করা
XAML:


<Button x:Name="button" Content="Change Color" Background="Green"/>

C#:


this.button.Background = new SolidColorBrush(Colors.Green);
this.button.Background = new SolidColorBrush(Color.FromArgb(10,15,100,124));

যদি Backgroundimage দিতে চাই তো?
XAML:

<Button x:Name="button" Content="Image" Width="100" Height="100">
<Button.Background>
<ImageBrush ImageSource="logo.png"/>
</Button.Background>
</Button>

C#:


ImageBrush imageBrush = new ImageBrush();
Uri uri = new Uri("logo.png", UriKind.RelativeOrAbsolute);
imageBrush.ImageSource = new BitmapImage(uri);
this.button.Background = imageBrush;

সমস্যা: Thickness পরিবর্তন করা

XAML:


<Border BorderBrush="Red" x:Name="border" Height="200" Width="200" BorderThickness="5,4,3,2">
<!--Some code here...—>
</Border>

C#:


this.border.BorderThickness = new Thickness(5, 4, 3, 2);
this.border.BorderThickness = new Thickness(5);
Advertisements

Daily Deeds: Windows Phone Data Encryption

Well, its nothing new to be said. I was sticking my head on a encrypted sms sender app and was definitely looking for some encryption system over the internet. The thing is I knew a way around on C# as I used it way before. So, I looked it u[ and suddenly it hit me that may be that solution might not work on the silverlight core on Windows Phone 7.5. Just as it turned out I was right. So, I hooked up again on MSDN, found a blog of Rob Tiffany describing a system and I used to make my app! Smile

As I said, daily deed is a daily feed of my lookups, so don’t expect much without codes. Smile

Here’s how I encrypted the data


AesManaged aes = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{

Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

//Create AES algorithm
aes = new AesManaged();
aes.Key = rfc2898.GetBytes(32);
//IV derived from byte array with 16 pseudo-random key bytes
aes.IV = rfc2898.GetBytes(16);

//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

//Encrypt Data
byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();

//Return Base 64 String
return Convert.ToBase64String(memoryStream.ToArray());
}
finally
{
if (cryptoStream != null)
cryptoStream.Close();

if (memoryStream != null)
memoryStream.Close();

if (aes != null)
aes.Clear();
}
}

Points to be remembered here:

    • AES is used here to encrypt and decrypt a message.
    • Salt is needed with the password to get things done and it needs at least 8 characters
    • Anybody can set the iteration at any value, minimum 1000 is preferred.
    • Use same password for encryption and decryption.
    • Code sample is taken from the respective blog. Smile

And the decryption is as followed:


public string Decrypt(string dataToDecrypt, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;

try
{
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

aes = new AesManaged();

aes.Key = rfc2898.GetBytes(32);
aes.IV = rfc2898.GetBytes(16);

//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

//Decrypt Data
byte[] data = Convert.FromBase64String(dataToDecrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();

//Return Decrypted String
byte[] decryptBytes = memoryStream.ToArray();

//Dispose
if (cryptoStream != null)
cryptoStream.Dispose();

//Retval
return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
}
finally
{
if (memoryStream != null)
memoryStream.Dispose();

if (aes != null)
aes.Clear();
}
}

And Voila! With a SmsComposerTask, my simple app looks like:
onetwothree

This is meant to be a share of my daily experience feed. So, take it easy! Hope it helps! Ciao!