Silverlight XAML - shell.xaml上的背景属性 - 更新网格背景

时间:2012-02-05 15:23:48

标签: silverlight xaml mvvm

更新

我在这里的小组的帮助下纠正了这个问题,这段代码是任何可能希望将来使用它的代码。只需在shellviewmodel中发布代码并更新ThemeChange子项以反映背景所需的颜色。此背景显示垂直LinearGradient。

要使用EventSystem,请参阅此帖子:http://rachel53461.wordpress.com/2011/06/05/communication-between-viewmodels-with-mvvm/

我正在尝试更新整个应用的背景。我正在使用Silverlight 5 + Prism + MEF。

这是shell.xaml:

<UserControl x:Class="Insight.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:prism="clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism" 
mc:Ignorable="d"
d:DesignHeight="1024" d:DesignWidth="768">

    <Grid x:Name="LayoutRoot" 
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Height="Auto" 
    Width="Auto"
    MouseRightButtonDown="LayoutRoot_MouseRightButtonDown"
    Background="{Binding BackGroundBrush}">
    </Grid>

这是shellViewModel:

Imports System.Text
Imports System.Threading
Imports System.Windows.Data
Imports System.Globalization
Imports System.ComponentModel
Imports System.Windows.Interop
Imports System.Collections.ObjectModel
Imports System.ComponentModel.Composition
Imports Microsoft.VisualBasic
Imports Microsoft.Practices.Prism
Imports Microsoft.Practices.Prism.Regions
Imports Microsoft.Practices.Prism.Commands
Imports PrismFramework.Abstractions.Bases
Imports PrismFramework.Abstractions.Interfaces
Imports PrismFramework.Implementors.Commanding
Imports PrismFramework.Implementors.Primitives
Imports PrismFramework.Abstractions.Globalization
Imports PrismFramework.Implementors.Interaction.Request
Imports Insight.ModuleUser.Services
Imports Insight.DataServices.Services
Imports Insight.ModuleUser.Interfaces
Imports Insight.DataServices.Primitives
Imports Insight.Controls.ModalDialogViewModels
Imports System.Windows.Controls.Theming

<Export()> _
<PartCreationPolicy(CreationPolicy.NonShared)> _
Public Class ShellViewModel
Inherits ViewModelBase
Implements IPartImportsSatisfiedNotification

#Region "Attributes"
Private backgroundDefaultPrimaryColor As Color = New Color With {.A = 128, .R = 128, .G = 128, .B = 128}
Private backgroundDefaultSecondaryColor As Color = New Color With {.A = 222, .R = 222, .G = 222, .B = 222}

Private backgroundPrimaryColor As Color
Private backgroundSecondaryColor As Color

Private backgroundGradientStop1 As GradientStop = New GradientStop() With {.Color = BackgroundDefaultColor, .Offset = "0"}
Private backgroundGradientStop2 As GradientStop = New GradientStop() With {.Color = backgroundPrimaryColor, .Offset = "0.185"}
Private backgroundGradientStop3 As GradientStop = New GradientStop() With {.Color = backgroundSecondaryColor, .Offset = "0.8"}
Private backgroundGradientStop4 As GradientStop = New GradientStop() With {.Color = BackgroundDefaultColor, .Offset = "1"}

Private backgroundGradientStops As GradientStopCollection = New GradientStopCollection From {backgroundGradientStop1, backgroundGradientStop2, backgroundGradientStop3, backgroundGradientStop4}

#End Region
#Region "Instantiators"
<ImportingConstructor()> _
Public Sub New()
    backgroundPrimaryColor = backgroundDefaultPrimaryColor
    backgroundSecondaryColor = backgroundDefaultSecondaryColor
    BackGroundBrush = New LinearGradientBrush(backgroundGradientStops, 90)
End Sub
#End Region
#Region "IPartImportsSatisfiedNotification"
Public Sub OnImportsSatisfied() Implements IPartImportsSatisfiedNotification.OnImportsSatisfied
    ' Subscribe to any MenuClick events
    EventSystem.Subscribe(Of Theme)(AddressOf ThemeChange)
End Sub
#End Region
#Region "ThemeChange"
Public Sub ThemeChange(theme As Theme)
    'Parse out themeName
    Dim sThemeName() As String = theme.ToString.Split(".")
    Select Case sThemeName(UBound(sThemeName))
        Case "BubbleCremeTheme"
            backgroundPrimaryColor = Color.FromArgb(123, 235, 111, 234)
            backgroundSecondaryColor = Color.FromArgb(12, 23, 11, 23)
            Exit Select
        Case "ExpressionDarkTheme"
            backgroundPrimaryColor = New Color With {.A = 222, .R = 222, .G = 222, .B = 222}
            backgroundSecondaryColor = New Color With {.A = 245, .R = 245, .G = 245, .B = 245}
            Exit Select
        Case "ExpressionLightTheme"

            Exit Select
        Case "No Theme"
            Exit Select
        Case "Rainier Orange"

            Exit Select
        Case "Rainier Purple"

            Exit Select
        Case "Shiny Blue"

            Exit Select
        Case "Shiny Red"

            Exit Select
        Case "Twilight Blue"

            Exit Select
        Case Else
            backgroundPrimaryColor = backgroundDefaultPrimaryColor
            backgroundSecondaryColor = backgroundDefaultSecondaryColor
    End Select
    'Setup the gradient stops that changed
    backgroundGradientStop2 = New GradientStop() With {.Color = backgroundPrimaryColor, .Offset = "0.185"}
    backgroundGradientStop3 = New GradientStop() With {.Color = backgroundSecondaryColor, .Offset = "0.8"}
    'Now setup the gradientstop collection and create the brush
    backgroundGradientStops.Clear()
    backgroundGradientStops = New GradientStopCollection From {backgroundGradientStop1, backgroundGradientStop2, backgroundGradientStop3, backgroundGradientStop4}
    BackGroundBrush = New LinearGradientBrush(backgroundGradientStops, 90)
End Sub
#End Region
#Region "BackgroundDefaultColor"
Private _backgroundDefaultColor As Color = New Color() With {.A = 255, .R = 255, .G = 255, .B = 255}
Public Property BackgroundDefaultColor() As Color
    Get
        Return Me._backgroundDefaultColor
    End Get
    Set(value As Color)
        If Me._backgroundDefaultColor <> value Then
            Me._backgroundDefaultColor = value
            OnPropertyChanged("BackgroundDefaultColor")
        End If
    End Set
End Property
#End Region
#Region "BackgroundBrush"
Dim _backgroundBrush As Brush
Public Property BackGroundBrush() As Brush
    Get
        Return Me._backgroundBrush
    End Get
    Set(value As Brush)
        If Me._backgroundBrush IsNot value Then
            Me._backgroundBrush = value
            OnPropertyChanged("BackGroundBrush")
        End If
    End Set
End Property
#End Region
End Class

正如您在constuctor中看到的,我为应用程序设置了默认值,然后更新了BackgroundBrush属性。代码点到了这一点,并且调用了Get属性,但由于某种原因,后台画笔不会在GUI上更新。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你有一个属性Back_ G _roundBrush,它在构造函数中设置并且绑定有效。然后你触发OnPropertyChanged(“Back * g * roundBrush”)并且由于属性的名称不对,绑定机制没有接收到它。绑定会查找大写字母'G',然后使用小写'g'进行搜索。