Silverlight XAML - Свойство фона на shell.xaml - Обновление фона сетки - PullRequest
0 голосов
/ 05 февраля 2012

UPDATE

Я исправил проблему с помощью группы здесь, и этот код является исправленным кодом для любого, кто может захотеть использовать его в будущем. Просто опубликуйте код в вашей shellviewmodel и обновите сабвуфер ThemeChange, чтобы отразить цвета, которые вы хотите для своего фона. На этом фоне показан вертикальный линейный градиент.

Чтобы использовать 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, но по какой-то причине фоновая кисть не обновляется в графическом интерфейсе. Есть идеи?

1 Ответ

0 голосов
/ 06 февраля 2012

У вас есть свойство Back_ G _roundBrush, которое устанавливается в конструкторе, и привязка работает.Затем вы запускаете OnPropertyChanged («Назад * g * roundBrush»), и это не определяется механизмом привязки, поскольку имя свойства не является правильным.Привязка ищет заглавную букву «G», а вы стреляете строчной буквой «g».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...