2 WPF和DLL,模型更改通知

时间:2017-01-14 18:29:07

标签: c# .net wpf xaml mvvm

我试图在MVVM之后的解决方案中制作两个WPF项目和一个DLL项目。

第一个WPF用作第二个WPF的管理面板(例如:你写文本,按一个按钮,文本显示在第二个WPF窗口中)。

我想把我的模型放在DLL中。

我的问题是我不知道如何在第二个WPF中显示文本(通知?)。

我在viewmodel中实现了INotifyPropertyChanged

然后我被困住了,我不知道该怎么做......

我的解决方案如下所示:

WPF_Solution

  1. DisplayWPF
    • MainWindow.xaml
  2. Dll_mvvm
    • Text.cs:
  3. ManagementWPF
    • DisplayViewModel.cs
    • MainWindow.xaml
  4. 显示和管理均指DLL。

    Text.cs:

    public class Text
    {
        string _textToDisplay;
    
        /// <summary>
        /// Text to display on screen
        /// </summary>
        public string TextToDisplay
        {
            get { return _textToDisplay; }
            set { _textToDisplay = value; }
        }
    }
    

    DisplayViewModel.cs:

    public class DisplayViewModel : INotifyPropertyChanged
    {
        private Text _text;
    
        /// <summary>
        /// Constructs the default instance of a ToDisplayViewModel
        /// </summary>
        public DisplayViewModel()
        {
            _text = new Text();
    
        }
    
    
        /// <summary>
        /// Accessors
        /// </summary>
        public Text Text
        {
            get { return _text; }
    
            set { _text = value; }
        }
    
    
    
        public string TextToDisplay
        {
            get { return Text.TextToDisplay; }
            set
            {
                if (Text.TextToDisplay != value)
                {
                    Text.TextToDisplay = value;
                    RaisePropertyChanged("TextToDisplay");
                }
            }
        }
    
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    管理小组:

    public partial class MainWindow : Window
    {
        private DisplayViewModel _displayThings;
        private Affichage.MainWindow _displayer = new Affichage.MainWindow();
        public MainWindow()
        {
            InitializeComponent();
            _displayThings = (DisplayViewModel)base.DataContext;
            _displayer.Show();
        }
    
        private void disp_btn_Click(object sender, RoutedEventArgs e)
        {
            _displayThings.TextToDisplay = textBox.Text;
        }
    }
    

    WPF只是“控制”窗口中的一个按钮和一个文本框,以及“显示”窗口中的一个文本框。链接到viewmodel

    <Window.DataContext>
        <!-- Declaratively create an instance of DisplayViewModel -->
        <local:DisplayViewModel />
    </Window.DataContext>
    

    TextBox与Text="{Binding TextToDisplay}&#34;

    绑定

    我的Viewmodel也应该与DLL共享吗?

    如何通知其他项目模型中的更改?

1 个答案:

答案 0 :(得分:0)

删除以下标记,因为它将创建DisplayViewModel类的新实例:

<Window.DataContext>
    <!-- Declaratively create an instance of DisplayViewModel -->
    <local:DisplayViewModel />
</Window.DataContext>

...并将子窗口的DataContext属性分配给您实际设置TextToDisplay属性的DisplayViewModel类的实例:

public partial class MainWindow : Window
{
    private DisplayViewModel _displayThings = = new DisplayViewModel();
    private Affichage.MainWindow _displayer = new Affichage.MainWindow();
    public MainWindow()
    {
        InitializeComponent();
        _displayer.DataContext = _displayThings;
        _displayer.Show();
    }

    private void disp_btn_Click(object sender, RoutedEventArgs e)
    {
        _displayThings.TextToDisplay = textBox.Text;
    }
}