【发布时间】:2012-02-05 15:23:47
【问题描述】:
更新
我在小组的帮助下更正了问题,此代码是更正后的代码,适用于将来可能想要使用它的任何人。只需在您的 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
正如您在构造函数中看到的,我为应用设置了默认值,然后更新了 BackgroundBrush 属性。代码达到了这一点,并调用了 Get 属性,但由于某种原因,背景画笔没有在 GUI 上更新。有什么想法吗?
【问题讨论】:
-
您似乎没有设置任何
DataContext,但尝试绑定它? -
通过 MEF 绑定到视图模型。我可以在 BackGroundBrush Setter 处设置断点,然后该属性会被命中并触发相应的 get 属性。
标签: silverlight xaml mvvm