WPF窗体之间数据传递

1.由于需要把主窗体的数据,发送到UserControl中。考虑到Android中有EventBus。觉得自己写个方法实现。

下面是中间件

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace InfusionBagSmartLaber.Communication
{
    public class EventBus
    {
        static EventBus instance;

        public Action<string> ReceiveFunc;
        public Action<int,int,int> EventAction;
        public static EventBus getInstance()
        {
            if (instance == null)
            {
                instance = new EventBus();
            }
            return instance;
        }

        public void SetPoint(string str)
        {
            ReceiveFunc(str);
            Debug.WriteLine("Event Bus SetPoint");
        }

        public void SetPoint(int ID, int Step, int val = 3)
        {
            EventAction(ID,Step,val);
            Debug.WriteLine("Event Bus SetPoint");
        }

    }
}

2.MainWindow.xaml.cs 中使用

        public void SetPoint(int ID, int Step, int val = 3)
        {
           
            string info = $"SetPoint({ID},{Step},{val})"; //我的JS 方法是initValue
        
            this.webBrowser.EvaluateScriptAsync(info); 
           
            EventBus.getInstance().SetPoint(ID, Step, val);
       
        }

3.UserControl.xaml.cs中使用

using CefSharp;
using InfusionBagSmartLabeler;
using InfusionBagSmartLaber.Communication;
using InfusionBagSmartLaber.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InfusionBagSmartLaber.View
{
    /// <summary>
    /// Interaction logic for SystemStatus.xaml
    /// </summary>
    public partial class SystemStatus : UserControl
    {  
        public SystemStatus()
        {
            InitializeComponent(); 
        }

        protected override void OnVisualParentChanged(DependencyObject oldParent)
        { 
            base.OnVisualParentChanged(oldParent);
 
            Debug.WriteLine("OnVisualParentChanged");

  
            EventBus.getInstance().EventAction += SetPoint;
 
        } 

        public void SetPoint(int ID, int Step, int val = 3)
        {
            string info = $"SetPoint({ID},{Step},{val})"; //我的JS 方法是initValue
                                                          // Debug.WriteLine("Debug : " + info);
            this.webBrowser.EvaluateScriptAsync(info);
            Debug.WriteLine($"SetPoint {info}");
        }

 


        //private string _myString;
        public string MyString
        {
            get { return (string)GetValue(MyStringProperty); }
            //set { _myString = value; textBox1.Text = _myString; }
            set { SetValue(MyStringProperty, value); }
        }

        public static readonly DependencyProperty MyStringProperty = DependencyProperty.Register("MyString", typeof(string),
            typeof(SystemStatus), new PropertyMetadata("defaultValue", new PropertyChangedCallback(OnMyStringPropertyChange)));

        static void OnMyStringPropertyChange(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            SystemStatus source = (SystemStatus)sender;
            Debug.WriteLine("OnMyStringPropertyChange");
           // source.textBox1.Text = args.NewValue.ToString() + args.NewValue.ToString().Length.ToString();
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            Debug.WriteLine("OnTextChanged");
            //UserControl1.OnMyStringPropertyChange(sender,)
        } 

    }
}

4.当然也可以反过来使用。就可以实现UserControl传递数据到MainWindow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值