【问题标题】:How to use childwindow in this case在这种情况下如何使用子窗口
【发布时间】:2014-11-26 12:12:54
【问题描述】:

我正在开发一个 Silverlight 5 现有应用程序,其中遵循 MVVM。

我在 View 文件夹中创建了一个我自己的 ErrorMessageBox.xaml(子窗口),我处于必须在 Model 文件夹内的类中弹出此 ErrorMessageBox 的情况。

我发现 ErrorMessageBox 在 Model 中不可访问(因为它在 View 文件夹中)。所以最后我在 Model 中创建了一个 ErrorMessageBox.xaml 以便它将被用于所有 模型文件夹中的类。

当我尝试弹出这个子窗口(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;
        }
    }
}

为什么它不起作用?如何让它发挥作用?

【问题讨论】:

  • 您是否尝试过使用 Dispatcher.BeginInvoke() 并尝试使用 istopmost=true 将 CFW 置于最前面

标签: c# .net silverlight mvvm silverlight-5.0


【解决方案1】:

第一件事是你不应该把你的 childwindow 类放在模型文件夹中,因为它破坏了 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 的值时,它都应该显示子窗口。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2015-11-30
    • 2017-05-16
    • 2012-10-06
    • 2011-05-18
    • 2019-11-29
    • 2022-01-23
    • 2018-05-20
    • 2013-02-25
    相关资源
    最近更新 更多