Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Saturday, April 28, 2012

Custom Themes In Silverlight

Silverlight Custom theme making is easy way using app.xml. Below code for change the theme in silverlight application dynamically. Also We can make it save into local machine for next time.
Preview:


Below the code for change style of button dynamically.

AddHandler element.MouseEnter, Sub(i, j)
                                                 i.Style = Application.Current.Resources("MouseHoverStyle")
                                             End Sub

AddHandler element.MouseLeave, Sub(i, j)                                               
                                                     i.Style = Application.Current.Resources("MouseLeaveStyle")   
                                             End Sub


We can change the APP.xaml Resources too. here the Steps below:
1) Prepare the styles called as Red,blue,green..etc
2) Then Change App Styles Dynamically here the code below.
VB Code:

  Public Sub LoadResourceDictionary(xamlTheme As String)
        xamlTheme = "/app-path;component/Assets/" & xamlTheme & ".xaml"
        If String.IsNullOrEmpty(xamlTheme) Then
            Return
        End If
        Try
            Application.Current.Resources.MergedDictionaries.Clear()
            Dim resource As New ResourceDictionary
            resource.Source = New Uri(xamlTheme, UriKind.Relative)
            Application.Current.Resources.MergedDictionaries.Add(resource)

            '/* Master Page elements is not Affected Dynamically*/
            Dim Page As MainPage = DirectCast(App.Current.RootVisual, MainPage)
            Page.MainBlock.Style = App.Current.Resources("LayoutRootGridStyle")
            Page.SliderMenu.Style = App.Current.Resources("Slidermenubackcolor")
            Page.ContentBorder.Style = App.Current.Resources("MainContentBackground")
            Page.MenuBlock.Style = App.Current.Resources("Loginmenubackcolor")
            Page.loginBlock.Style = App.Current.Resources("Loginmenubackcolor")
            Page.pathleft.Template = App.Current.Resources("ButtonArrowLeft")
            Page.pathright.Template = App.Current.Resources("ButtonArrowRight")
            Slidermenubackcolor1.Style = App.Current.Resources("Slidermenubackcolor")
            Slidermenubackcolor.Style = App.Current.Resources("Slidermenubackcolor")
            ContentSubBorder.Style = App.Current.Resources("ContentBorder")

            '/* Theme save in local path */
            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

            If Not Directory.Exists(path & "\app-path") Then
                Directory.CreateDirectory(path & "\app-path")
            End If
            If File.Exists(path & "\app-path\Setting.txt") Then
                File.Delete(path & "\app-path\Setting.txt")
            End If

            Dim sr As StreamWriter = New StreamWriter(File.Open(path & "\app-path\Setting.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            sr.WriteLine(xamlTheme)
            sr.Close()
            sr = Nothing
        Catch ex As Exception
            MessageBox.Show("Failed to load the theme " & vbLf + ex.Message)
        End Try
    End Sub



3) Prepare some simple border and Call while mouse event
    Private Sub rectred_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles red.MouseLeftButtonUp
        LoadResourceDictionary("Red")
    End Sub

    Private Sub rectblue_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Blue.MouseLeftButtonUp
        LoadResourceDictionary("Blue")
    End Sub



Saturday, September 17, 2011

Change App Style Resources Dynamically in Silverlight

Mostly this is the same ways to changing the style dynamically.
Like :

AddHandler element.MouseEnter, Sub(i, j)
                                                 i.Style = Application.Current.Resources("MouseHoverStyle")
                                             End Sub

AddHandler element.MouseLeave, Sub(i, j)                                               
                                                     i.Style = Application.Current.Resources("MouseLeaveStyle")   
                                             End Sub


We can change the APP.xaml Resources too. here the Steps below:
1) Prepare the styles called as Red,blue,green..etc
2) Then Change App Styles Dynamically here the code below.
VB Code:

  Public Sub LoadResourceDictionary(xamlTheme As String)
        xamlTheme = "/app-path;component/Assets/" & xamlTheme & ".xaml"
        If String.IsNullOrEmpty(xamlTheme) Then
            Return
        End If
        Try
            Application.Current.Resources.MergedDictionaries.Clear()
            Dim resource As New ResourceDictionary
            resource.Source = New Uri(xamlTheme, UriKind.Relative)
            Application.Current.Resources.MergedDictionaries.Add(resource)

            '/* Master Page elements is not Affected Dynamically*/
            Dim Page As MainPage = DirectCast(App.Current.RootVisual, MainPage)
            Page.MainBlock.Style = App.Current.Resources("LayoutRootGridStyle")
            Page.SliderMenu.Style = App.Current.Resources("Slidermenubackcolor")
            Page.ContentBorder.Style = App.Current.Resources("MainContentBackground")
            Page.MenuBlock.Style = App.Current.Resources("Loginmenubackcolor")
            Page.loginBlock.Style = App.Current.Resources("Loginmenubackcolor")
            Page.pathleft.Template = App.Current.Resources("ButtonArrowLeft")
            Page.pathright.Template = App.Current.Resources("ButtonArrowRight")
            Slidermenubackcolor1.Style = App.Current.Resources("Slidermenubackcolor")
            Slidermenubackcolor.Style = App.Current.Resources("Slidermenubackcolor")
            ContentSubBorder.Style = App.Current.Resources("ContentBorder")

            '/* Theme save in local path */
            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

            If Not Directory.Exists(path & "\app-path") Then
                Directory.CreateDirectory(path & "\app-path")
            End If
            If File.Exists(path & "\app-path\Setting.txt") Then
                File.Delete(path & "\app-path\Setting.txt")
            End If

            Dim sr As StreamWriter = New StreamWriter(File.Open(path & "\app-path\Setting.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            sr.WriteLine(xamlTheme)
            sr.Close()
            sr = Nothing
        Catch ex As Exception
            MessageBox.Show("Failed to load the theme " & vbLf + ex.Message)
        End Try
    End Sub



3) Prepare some simple border and Call while mouse event
    Private Sub rectred_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles red.MouseLeftButtonUp
        LoadResourceDictionary("Red")
    End Sub

    Private Sub rectblue_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Blue.MouseLeftButtonUp
        LoadResourceDictionary("Blue")
    End Sub


4) Finally got change colors like that:

Friday, September 16, 2011

Add Weather Option in your Silverlight App

Preparing weather report in silverlight 4

1) Get the weather update from google.
2) Prepare WebClient For Fetch the XML (using WCF or Silverlight) here i am doing using WCF Data contract Class:

<DataContract()>
Public Class WeatherReport
    <DataMember()>
    Public ForecastInformation As New ForecastInformation
    <DataMember()>
    Public CurrentWeather As New CurrentWeather
    <DataMember()>
    Public WeatherFullReport As New List(Of WeatherFullReport)
    <DataMember()>
    Public Status As String
End Class


<DataContract()>
Public Class ForecastInformation
    <DataMember()>
    Public City As String
    <DataMember()>
    Public PostalCode As String
    <DataMember()>
    Public ForecastDate As String
    <DataMember()>
    Public CurrentDateTime As String
End Class

<DataContract()>
Public Class CurrentWeather
    <DataMember()>
    Public Condition As String
    <DataMember()>
    Public Temp_Fahrenheit As String
    <DataMember()>
    Public Temp_Celsius As String
    <DataMember()>
    Public Humidity As String
    <DataMember()>
    Public icon As String
    <DataMember()>
    Public WindCondition As String
End Class

<DataContract()>
Public Class WeatherFullReport
    <DataMember()>
    Public DayOfWeek As String
    <DataMember()>
    Public Low As String
    <DataMember()>
    Public High As String
    <DataMember()>
    Public icon As String
    <DataMember()>
    Public WindCondition As String
End Class


WCf Function Class:

Public Function GET_WEATHERREPORT(ByVal Location As String) As WeatherReport Implements IService.GET_WEATHERREPORT
        Dim ReturnObj As New WeatherReport
        Dim webClient As New WebClient()
        webClient.Headers.Add("Content-Type", "application/xml")
        webClient.Encoding = Encoding.UTF8
        webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)")
        Try
            Dim Site As String = "http://www.google.com"
            Dim Query As String = Site & "/ig/api?weather=" & Location
            'Dim result As String = webClient.DownloadString(Query)
            Dim reader As New XmlTextReader(Query)
            ' While reader.Read()
            reader.Read()
            reader.Read()
            reader.Read()
            reader.Read()
            If reader.Name = "forecast_information" Then
                reader.Read()
                If reader.Name = "city" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.ForecastInformation.City = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "postal_code" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.ForecastInformation.PostalCode = attribute
                    End If
                End If
                reader.Read()
                reader.Read()
                reader.Read()
                reader.Read()
                If reader.Name = "current_date_time" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.ForecastInformation.CurrentDateTime = attribute
                    End If
                End If
                reader.Read()
            End If
            reader.Read()
            reader.Read()
            If reader.Name = "current_conditions" Then
                reader.Read()
                If reader.Name = "condition" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Condition = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "temp_f" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Temp_Fahrenheit = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "temp_c" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Temp_Celsius = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "humidity" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Humidity = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "icon" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.icon = Site & attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "wind_condition" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.WindCondition = attribute
                    End If
                End If
            End If
            reader.Read()
            While reader.Read()
                If reader.Name = "forecast_conditions" Then
                    Dim forecast_conditions As New WeatherFullReport
                    reader.Read()
                    If reader.Name = "day_of_week" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.DayOfWeek = attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "low" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.Low = attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "high" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.High = attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "icon" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.icon = Site & attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "condition" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.WindCondition = attribute
                        End If
                    End If
                    ReturnObj.WeatherFullReport.Add(forecast_conditions)
                End If
                reader.Read()
            End While
            reader.Read()
            If reader.Name = "problem_cause" Then
                ReturnObj.Status = "No Wearther Report Found For Location: " + Location
            End If
            'End While
        Catch ex As Exception
            ReturnObj.Status = "Weather Report is currently Unavilable"
        End Try
        Return ReturnObj
    End Function


3) Call WCF In silverlight
mobjproxy.GET_WEATHERREPORTAsync("Chennai")
4) Prepare static design and fill the content in WCF Completed

XAML
 <Canvas Height="322" Width="860">
                    <Canvas Height="33" Name="Canvas2" Width="458" Canvas.Left="251" Canvas.Top="20">
                        <sdk:Label Canvas.Left="0" Canvas.Top="0" Content="Location" Foreground="White" Height="28" Name="Label4" Width="49" />
                        <sdk:AutoCompleteBox Canvas.Left="58" Canvas.Top="2" IsTextCompletionEnabled="True" Name="SearchArea" Width="320" Text="Chennai, Tamil Nadu" />
                        <Button Canvas.Left="380" Canvas.Top="1" Content="Search" Height="27" Name="Find" TabIndex="3" Template="{StaticResource SearchButton}" Width="74" />
                    </Canvas>
                    <sdk:Label Content="Weather" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" Name="Label3" Width="63" Canvas.Left="61" Canvas.Top="20">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                    <Image Name="Image3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" Canvas.Left="8" Canvas.Top="10" Height="59" Width="50" />
                    <Grid Height="254" Name="WeatherGrid" Width="848" Canvas.Left="11" Canvas.Top="59" Visibility="Visible">
                        <sdk:Label Content="Weather for Chennai, Tamil Nadu" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="33,0,579,204" Name="lblocation" Width="236" VerticalAlignment="Bottom">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <Image Margin="33,69,681,30" Name="CurrentWeatherIcon" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <Rectangle Margin="357,16,488,18" Style="{StaticResource DividerStyle}" />
                        <Image Margin="393,69,378,98" Name="WeatherDayIcon1" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="393,40,378,191" Name="WeatherDay1" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" Foreground="#FFFFCC01" Height="28" Margin="404,162,421,64" Name="WeatherDayHigh1" FontWeight="Bold" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="438,162,389,64" Name="WeatherDayLow1" />
                        <Image Margin="510,69,261,98" Name="WeatherDayIcon2" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="510,40,261,191" Name="WeatherDay2" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="521,162,304,64" Name="WeatherDayHigh2" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="555,162,272,64" Name="WeatherDayLow2" />
                        <Image Margin="621,69,150,98" Name="WeatherDayIcon3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="621,40,150,191" Name="WeatherDay3" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="632,162,193,64" Name="WeatherDayHigh3" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="666,162,161,64" Name="WeatherDayLow3" />
                        <Image Margin="738,69,33,98" Name="WeatherDayIcon4" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="738,40,33,191" Name="WeatherDay4" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="749,162,76,64" Name="WeatherDayHigh4" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="783,162,44,64" Name="WeatherDayLow4" />
                        <sdk:Label Content="36 C" FontWeight="Bold" Foreground="#FFFFCC01" Margin="190,69,0,134" Name="CurrentWeatherC" FontSize="16" HorizontalAlignment="Left" Width="40" />
                        <sdk:Label Content="36 F" FontSize="16" FontWeight="Bold" Foreground="White" Margin="267,69,541,134" Name="CurrentWeatherF" />
                        <Rectangle Margin="248,82,599,142" Style="{StaticResource DividerStyle}" Height="30" />
                        <TextBlock Height="98" HorizontalAlignment="Left" Margin="190,126,0,0" Name="CurrentWeather" Text="TextBlock" VerticalAlignment="Top" Width="154" Foreground="White" FontWeight="Bold" FontSize="14" FontFamily="Arial">
                                Scattered Clouds<LineBreak /><LineBreak />
                                Wind: SW at 13 km/h<LineBreak /><LineBreak />
                                Humidity: 62%<LineBreak /><LineBreak /></TextBlock>
                    </Grid>
                    <sdk:Label Canvas.Left="324" Canvas.Top="173" Content="Loading ..." FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="#FFFFB100" Height="23" Name="lberror" Width="236">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                </Canvas>

 <Canvas Height="322" Width="860">
                    <Canvas Height="33" Name="Canvas2" Width="458" Canvas.Left="251" Canvas.Top="20">
                        <sdk:Label Canvas.Left="0" Canvas.Top="0" Content="Location" Foreground="White" Height="28" Name="Label4" Width="49" />
                        <sdk:AutoCompleteBox Canvas.Left="58" Canvas.Top="2" IsTextCompletionEnabled="True" Name="SearchArea" Width="320" Text="Chennai, Tamil Nadu" />
                        <Button Canvas.Left="380" Canvas.Top="1" Content="Search" Height="27" Name="Find" TabIndex="3" Template="{StaticResource SearchButton}" Width="74" />
                    </Canvas>
                    <sdk:Label Content="Weather" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" Name="Label3" Width="63" Canvas.Left="61" Canvas.Top="20">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                    <Image Name="Image3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" Canvas.Left="8" Canvas.Top="10" Height="59" Width="50" />
                    <Grid Height="254" Name="WeatherGrid" Width="848" Canvas.Left="11" Canvas.Top="59" Visibility="Visible">
                        <sdk:Label Content="Weather for Chennai, Tamil Nadu" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="33,0,579,204" Name="lblocation" Width="236" VerticalAlignment="Bottom">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <Image Margin="33,69,681,30" Name="CurrentWeatherIcon" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <Rectangle Margin="357,16,488,18" Style="{StaticResource DividerStyle}" />
                        <Image Margin="393,69,378,98" Name="WeatherDayIcon1" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="393,40,378,191" Name="WeatherDay1" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" Foreground="#FFFFCC01" Height="28" Margin="404,162,421,64" Name="WeatherDayHigh1" FontWeight="Bold" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="438,162,389,64" Name="WeatherDayLow1" />
                        <Image Margin="510,69,261,98" Name="WeatherDayIcon2" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="510,40,261,191" Name="WeatherDay2" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="521,162,304,64" Name="WeatherDayHigh2" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="555,162,272,64" Name="WeatherDayLow2" />
                        <Image Margin="621,69,150,98" Name="WeatherDayIcon3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="621,40,150,191" Name="WeatherDay3" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="632,162,193,64" Name="WeatherDayHigh3" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="666,162,161,64" Name="WeatherDayLow3" />
                        <Image Margin="738,69,33,98" Name="WeatherDayIcon4" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="738,40,33,191" Name="WeatherDay4" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="749,162,76,64" Name="WeatherDayHigh4" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="783,162,44,64" Name="WeatherDayLow4" />
                        <sdk:Label Content="36 C" FontWeight="Bold" Foreground="#FFFFCC01" Margin="190,69,0,134" Name="CurrentWeatherC" FontSize="16" HorizontalAlignment="Left" Width="40" />
                        <sdk:Label Content="36 F" FontSize="16" FontWeight="Bold" Foreground="White" Margin="267,69,541,134" Name="CurrentWeatherF" />
                        <Rectangle Margin="248,82,599,142" Style="{StaticResource DividerStyle}" Height="30" />
                        <TextBlock Height="98" HorizontalAlignment="Left" Margin="190,126,0,0" Name="CurrentWeather" Text="TextBlock" VerticalAlignment="Top" Width="154" Foreground="White" FontWeight="Bold" FontSize="14" FontFamily="Arial">
                                Scattered Clouds<LineBreak /><LineBreak />
                                Wind: SW at 13 km/h<LineBreak /><LineBreak />
                                Humidity: 62%<LineBreak /><LineBreak /></TextBlock>
                    </Grid>
                    <sdk:Label Canvas.Left="324" Canvas.Top="173" Content="Loading ..." FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="#FFFFB100" Height="23" Name="lberror" Width="236">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                </Canvas>

 <Canvas Height="322" Width="860">
                    <Canvas Height="33" Name="Canvas2" Width="458" Canvas.Left="251" Canvas.Top="20">
                        <sdk:Label Canvas.Left="0" Canvas.Top="0" Content="Location" Foreground="White" Height="28" Name="Label4" Width="49" />
                        <sdk:AutoCompleteBox Canvas.Left="58" Canvas.Top="2" IsTextCompletionEnabled="True" Name="SearchArea" Width="320" Text="Chennai, Tamil Nadu" />
                        <Button Canvas.Left="380" Canvas.Top="1" Content="Search" Height="27" Name="Find" TabIndex="3" Template="{StaticResource SearchButton}" Width="74" />
                    </Canvas>
                    <sdk:Label Content="Weather" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" Name="Label3" Width="63" Canvas.Left="61" Canvas.Top="20">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                    <Image Name="Image3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" Canvas.Left="8" Canvas.Top="10" Height="59" Width="50" />
                    <Grid Height="254" Name="WeatherGrid" Width="848" Canvas.Left="11" Canvas.Top="59" Visibility="Visible">
                        <sdk:Label Content="Weather for Chennai, Tamil Nadu" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="33,0,579,204" Name="lblocation" Width="236" VerticalAlignment="Bottom">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <Image Margin="33,69,681,30" Name="CurrentWeatherIcon" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <Rectangle Margin="357,16,488,18" Style="{StaticResource DividerStyle}" />
                        <Image Margin="393,69,378,98" Name="WeatherDayIcon1" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="393,40,378,191" Name="WeatherDay1" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" Foreground="#FFFFCC01" Height="28" Margin="404,162,421,64" Name="WeatherDayHigh1" FontWeight="Bold" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="438,162,389,64" Name="WeatherDayLow1" />
                        <Image Margin="510,69,261,98" Name="WeatherDayIcon2" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="510,40,261,191" Name="WeatherDay2" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="521,162,304,64" Name="WeatherDayHigh2" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="555,162,272,64" Name="WeatherDayLow2" />
                        <Image Margin="621,69,150,98" Name="WeatherDayIcon3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="621,40,150,191" Name="WeatherDay3" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="632,162,193,64" Name="WeatherDayHigh3" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="666,162,161,64" Name="WeatherDayLow3" />
                        <Image Margin="738,69,33,98" Name="WeatherDayIcon4" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="738,40,33,191" Name="WeatherDay4" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="749,162,76,64" Name="WeatherDayHigh4" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="783,162,44,64" Name="WeatherDayLow4" />
                        <sdk:Label Content="36 C" FontWeight="Bold" Foreground="#FFFFCC01" Margin="190,69,0,134" Name="CurrentWeatherC" FontSize="16" HorizontalAlignment="Left" Width="40" />
                        <sdk:Label Content="36 F" FontSize="16" FontWeight="Bold" Foreground="White" Margin="267,69,541,134" Name="CurrentWeatherF" />
                        <Rectangle Margin="248,82,599,142" Style="{StaticResource DividerStyle}" Height="30" />
                        <TextBlock Height="98" HorizontalAlignment="Left" Margin="190,126,0,0" Name="CurrentWeather" Text="TextBlock" VerticalAlignment="Top" Width="154" Foreground="White" FontWeight="Bold" FontSize="14" FontFamily="Arial">
                                Scattered Clouds<LineBreak /><LineBreak />
                                Wind: SW at 13 km/h<LineBreak /><LineBreak />
                                Humidity: 62%<LineBreak /><LineBreak /></TextBlock>
                    </Grid>
                    <sdk:Label Canvas.Left="324" Canvas.Top="173" Content="Loading ..." FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="#FFFFB100" Height="23" Name="lberror" Width="236">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                </Canvas> </canvas>

Fill Content in WCF Completed
1) Image is provided by google also but its GIF
2) Get Images from http://www.gettyicons.com/free-icon/126/weather-icon-set/free-sun-little-cloud-flash-rain-icon-png/

 Private Sub GET_WEATHERREPORTCompleted(sender As Object, e As ClientProxy.GET_WEATHERREPORTCompletedEventArgs)
        Dim WeatherReport = e.Result
        Dim Dictionary As New Dictionary(Of String, String)
        Dictionary.Add("http://www.google.com/ig/images/weather/sunny.gif", "sunny.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/mostly_sunny.gif", "mostly_sunny.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/partly_cloudy.gif", "partly_cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/mostly_cloudy.gif", "cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_storm.gif", "chance_of_storm.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/rain.gif", "rain.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_rain.gif", "chance_of_rain.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_snow.gif", "chance_of_snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/cloudy.gif", "cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/mist.gif", "mist.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/storm.gif", "chance_of_storm.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/thunderstorm.gif", "mist.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_tstorm.gif", "chance_of_storm.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/sleet.gif", "snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/snow.gif", "snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/flurries.gif", "chance_of_snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/icy.gif", "chance_of_snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/dust.gif", "haze.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/fog.gif", "cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/smoke.gif", "haze.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/haze.gif", "haze.png")


        If Not e.Result.ForecastInformation.City Is Nothing Then
            WeatherGrid.Visibility = Windows.Visibility.Visible
            lblocation.Content = "Weather for " & e.Result.ForecastInformation.City
            CurrentWeatherIcon.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.CurrentWeather.icon), UriKind.RelativeOrAbsolute))
            CurrentWeatherC.Content = e.Result.CurrentWeather.Temp_Celsius & ChrW(176) & "C"
            CurrentWeatherF.Content = e.Result.CurrentWeather.Temp_Fahrenheit & ChrW(176) & "F"

            CurrentWeather.Text = e.Result.CurrentWeather.Condition & ChrW(13) & ChrW(13) & _
                                  e.Result.CurrentWeather.WindCondition & ChrW(13) & ChrW(13) & _
                                  e.Result.CurrentWeather.Humidity


            WeatherDay1.Content = e.Result.WeatherFullReport(0).DayOfWeek
            WeatherDayHigh1.Content = e.Result.WeatherFullReport(0).High & ChrW(176) & "F"
            WeatherDayLow1.Content = e.Result.WeatherFullReport(0).Low & ChrW(176) & "F"
            WeatherDayIcon1.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(0).icon), UriKind.RelativeOrAbsolute))

            WeatherDay2.Content = e.Result.WeatherFullReport(1).DayOfWeek
            WeatherDayHigh2.Content = e.Result.WeatherFullReport(1).High & ChrW(176) & "F"
            WeatherDayLow2.Content = e.Result.WeatherFullReport(1).Low & ChrW(176) & "F"
            WeatherDayIcon2.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(1).icon), UriKind.RelativeOrAbsolute))

            WeatherDay3.Content = e.Result.WeatherFullReport(2).DayOfWeek
            WeatherDayHigh3.Content = e.Result.WeatherFullReport(2).High & ChrW(176) & "F"
            WeatherDayLow3.Content = e.Result.WeatherFullReport(2).Low & ChrW(176) & "F"
            WeatherDayIcon3.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(2).icon), UriKind.RelativeOrAbsolute))

            WeatherDay4.Content = e.Result.WeatherFullReport(3).DayOfWeek
            WeatherDayHigh4.Content = e.Result.WeatherFullReport(3).High & ChrW(176) & "F"
            WeatherDayLow4.Content = e.Result.WeatherFullReport(3).Low & ChrW(176) & "F"
            WeatherDayIcon4.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(3).icon), UriKind.RelativeOrAbsolute))
            lberror.Content = e.Result.Status
        Else
            WeatherGrid.Visibility = Windows.Visibility.Collapsed
            lberror.Content = "Invalid location!"
        End If
    End Sub

Saturday, August 27, 2011

Dynamic Image Source Changing in Silverlight OOB

Initialize Class:
xmlns:mycontrol="clr-namespace:yournamespacehere"

Implementing Static resources:

<UserControl.Resources >
<mycontrol:ImageUriConverter x:Key="converter"/ >
</UserControl.Resources >



Binding Image source: (it will automatically calling convertor class)

<Image Source="{Binding Path=ImagePath, Converter={StaticResource converter}}" Margin="5,5,6,5" Stretch="Fill"/ >


get Class for ImageUriConverter
http://code.google.com/p/photobucket-silverlight/source/browse/trunk/photobucketapi/ImageUriConverter.cs?r=46


Within Convert function you prepare dynamic path

string host = App.Current.Host.Source.ToString();
string imagePath = "";
if (host.IndexOf("/ClientBin") != -1) {
imagePath = host.Substring(0, host.IndexOf("/ClientBin"));
return new BitmapImage(new Uri(imagePath + "/" + value, UriKind.RelativeOrAbsolute));
}

//else no image path

return new BitmapImage(new Uri("/applicationname;component/Images/noimage.jpg", UriKind.RelativeOrAbsolute));



checkout also Sample:

http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/03d69c15-172b-4098-bb90-5119f9bdac24/silverlight-ivalueconverter-for-image-urls.aspx


Friday, July 15, 2011

How to get the Screen Resolution in Silverlight 4 OOB

In Silverlights OOB Application resize by resolution.


if (!Application.Current.HasElevatedPermissions)
{
throw new NotSupportedException("This method requires elevated permissions.");
}

var content = Application.Current.Host.Content;
var dispatcher = Application.Current.MainWindow.Dispatcher;

if (!content.IsFullScreen)
{
// do this asynchronously; that will also work
// in certain event handlers like FrameworkElement.Loaded
dispatcher.BeginInvoke(() =>
{
// switch to full screen
content.IsFullScreen = true;

if (!content.IsFullScreen)
{
// something went wrong
Application.Current.MainWindow.width = 0;
Application.Current.MainWindow.height= 0;
return;
}

int width = (int)Math.Round(content.ActualWidth);
int height = (int)Math.Round(content.ActualHeight);

// switch back to windowed mode
content.IsFullScreen = false;

Application.Current.MainWindow.width = width;
Application.Current.MainWindow.height= height;
});