2014-10-01 81 views
0

我正在處理一個Silverlight 5現有的應用程序,其中MVVM接近是遵循。如何在這種情況下使用子窗口

我在View文件夾中創建了自己的ErrorMessageBox.xaml(childwindow),我處於模型文件夾內的類中必須裝入此ErrorMessageBox的情況。

我發現ErrorMessageBox在Model中不可訪問(因爲它在View文件夾中)。所以最後我在Model中創建了一個ErrorMessageBox.xaml,以便它將用於Model文件夾中的所有 類。

而當我嘗試彈出這個子窗口(ErrorMessageBox.xaml),那麼它不會彈出。爲什麼會發生,以及如何在Model 文件夾中的類中的函數調用中彈出此ErrorMessageBox.xaml。

public static void ThisFunctionIsCalledIHaveVerifiedOnDebugging(string message) //it is inside a class in Model folder 
{ 
    ConfirmationWindow cfw = new ConfirmationWindow(); 
    cfw.SetMessage("Popup test"); 
    cfw.Show(); //it do not pop up it 
} 

而且ConfirmationWindow.xaml是:

<silvercontrols:ChildWindow x:Class="Model.MessageFolder.ConfirmationWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:silvercontrols="clr-namespace:Silverlight.Windows.Controls;assembly=Silverlight.Windows.Controls" 
      xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
      xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" 
      Title="Message" Width="Auto" Height="Auto" MouseRightButtonDown="ChildWindow_MouseRightButtonDown"> 

    <silvercontrols:ChildWindow.Style> 
     <StaticResource ResourceKey="MessageBoxStyle"/> 
    </silvercontrols:ChildWindow.Style> 

    <Grid x:Name="LayoutRoot" MinWidth="360"> 
     <StackPanel Orientation="Vertical"> 
      <TextBlock x:Name="MessageBox" Margin="10 15 0 0" Height="Auto" FontSize="12" Text="Text" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" /> 
      <StackPanel x:Name="ContentBox" Margin="10 15 0 0" Height="Auto" Orientation="Horizontal"></StackPanel> 
      <StackPanel Margin="0 0 0 10" Orientation="Horizontal" HorizontalAlignment="Center" Height="45"> 
       <Button x:Name="YesBtn" Content="Yes" Width="82" HorizontalAlignment="Left" VerticalAlignment="Bottom" Style="{StaticResource ButtonStyle_Blue}"/> 
       <Button x:Name="NoBtn" Content="No" Margin="60 0 0 0" Width="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Style="{StaticResource ButtonStyle_Blue}"/> 
      </StackPanel> 
     </StackPanel> 
    </Grid> 
</silvercontrols:ChildWindow> 

和ConfirmationWindow.xaml.cs是:

using System.Windows; 
namespace Model.MessageFolder 
{ 
    public partial class ConfirmationWindow : Silverlight.Windows.Controls.ChildWindow 
    { 
     private bool showBtnClose; 
     public ConfirmationWindow(bool showBtnClose = false) 
     { 
      InitializeComponent(); 
      HasCloseButton = showBtnClose; 
      this.showBtnClose = showBtnClose; 
      NoBtn.Click += Close; 
     } 

     #region METHODS 
     public void SetMessage(string message) 
     { 
      MessageBox.Text = message; 
     } 
     public void AddContent(UIElement elt) 
     { 
      ContentBox.Children.Add(elt); 
     } 
     #endregion 

     #region EVENT_HANDLER 
     public void Close(object sender, RoutedEventArgs e) 
     { 
      this.Close(); 
     } 
     #endregion 

     private void ChildWindow_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
     { 
      e.Handled = true; 
     } 
    } 
} 

爲什麼它不工作?如何使它工作?

+0

您是否嘗試過使用Dispatcher.BeginInvoke()並嘗試使用istopmost = true將CFW放在前面 – user2526236 2014-10-01 20:52:32

回答

1

首先,您不應該將模型文件夾中的子窗口類帶入模型文件夾,因爲它會打破MVVM模式。請將其保留在您的視圖文件夾中。 你應該做的是從模型視圖中顯示子窗口。 爲了實現這一點,您需要一種方法來告訴您的視圖何時顯示子窗口以及它應該顯示的消息。

首先,在你的模型創建一個屬性的ErrorMessage:

public class MyModel : INotifyPropertyChanged 

{ ...

private string _errorMessage; 
public string ErrorMessage 
    { 
     private set 
     { 
      _errorMessage = value; 
      OnPropertyChanged("ErrorMessage"); 
     } 
     get { return _errorMessage;; } 
    } 

... }

注:我這裏假設你的模型類實現INotifyPropertyChanged接口,但它可能是一個不同的實現。

然後在您的視圖的代碼中添加一個依賴項屬性並將其綁定到您的模型的ErrorMessage。 依賴項屬性的更改回調用於顯示子窗口。 這可能看起來如下:

public partial class MyView : UserControl 

你每次{ ...

public static readonly DependencyProperty ErrorMessageProperty = 
     DependencyProperty.Register("ErrorMessage", typeof (string), typeof (MyView), 
       new PropertyMetadata((o, args) => 
       { 
        // Display childwindow when message is changed 
        string message = args.NewValue as string; 
        if(message!=null) 
        { 
          ConfirmationWindow cfw = new ConfirmationWindow(); 
          cfw.SetMessage(message); 
          cfw.Show(); 
        } 
       })); 

    public string ErrorMessage 
    { 
     get { return (string)GetValue(ErrorMessageProperty); } 
     private set { SetValue(ErrorMessageProperty, value); } 
    } 

...

public MyModel ViewModel 
    { 
... 
     set 
     { 
      DataContext = value;     

      Binding binding = new Binding(); 
      binding.Source = value; 
      binding.Path = new PropertyPath("ErrorMessage"); 
      SetBinding(ErrorMessageProperty, binding); 
     } 
    ... 
    } 

... }

然後改變模型中ErrorMessage的值,它應該顯示chil dwindow。