2014-10-01 83 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。