WinForm 使用 ReactiveUI

1.新建一个WinForm 项目,省略图片

2.安装  ReactiveUI 

3.新建Model,继承Reactiveobject

namespace ElectronBeamSystem.Model
{
    public class MenuButton: ReactiveObject
    {
        private string _menuText;
        private Color _menuColor;
        private Color _menuFontColor;
        public MenuButton()
        {
            _menuText = "MVVM控件";
            _menuColor = Color.Blue;
            _menuFontColor = Color.White;
        }
        public string MenuText { 
         get=>_menuText; 
         set=>this.RaiseAndSetIfChanged(ref _menuText,value);
        }
        public Color MenuColor 
        {
           get=>_menuColor;
            set => this.RaiseAndSetIfChanged(ref _menuColor, value);
        }
        public Color MenuFontColor 
        {
           get=> _menuFontColor;
            set => this.RaiseAndSetIfChanged(ref _menuFontColor, value);
        }

        
        
    }
}

3.新建ModelView

namespace ElectronBeamSystem.ViewMode
{
    public class MenuButtonModelView: ReactiveObject
    {
        private MenuButton _menuButton;
        private List<MenuButton> menuButtons ;
        public ReactiveCommand<Unit, Unit> ExampleCommand { get; } //定义命令
        public ReactiveCommand<Unit, Unit> ExampleCommandText { get; }
        public ReactiveCommand<Unit, Unit> ExampleCommandAdd { get; }
        public MenuButton _MenuButton {
         get=>_menuButton; set=>this.RaiseAndSetIfChanged(ref _menuButton,value);
        }
        public List<MenuButton> _MenuButtons {
         get=> menuButtons; set=>this.RaiseAndSetIfChanged(ref menuButtons, value);
        }
        public MenuButtonModelView()
        {
            _menuButton = new MenuButton();
            _MenuButtons= new List<MenuButton>();
            _MenuButtons.Add(new MenuButton());
            ExampleCommand = ReactiveCommand.Create(PerformAction); //绑定事件
            ExampleCommandText = ReactiveCommand.Create(PerformActionText);
            ExampleCommandAdd = ReactiveCommand.Create(PerformActionxx);


        }

        private void PerformActionText()
        {
           
        }
        private void PerformActionxx()
        {
           
        }

        private void PerformAction()
        {
            _menuButton.MenuColor = Color.Red;
            Console.WriteLine(_menuButton.MenuText);
            Console.WriteLine(_menuButton.MenuFontColor);
            Console.WriteLine(_menuButton.MenuColor);
            foreach (var item in _MenuButtons)
            {
                Console.WriteLine(item.MenuText);
            }
        }


    }
}

4.Form界面进行数据双向绑定

namespace ElectronBeamSystem
{
    public partial class Form1 : Form, IViewFor<MenuButtonModelView>
    {
        public Form1()
        {
            InitializeComponent();
            this.WhenActivated(x =>
            {
                x(this.Bind(ViewModel, vm => vm._MenuButton.MenuText, v => v.button1.Text));
                x(this.Bind(ViewModel, vm => vm._MenuButton.MenuColor, v => v.button1.BackColor));
                x(this.Bind(ViewModel, vm => vm._MenuButton.MenuFontColor, v => v.button1.ForeColor));
                x(this.Bind(ViewModel, vm => vm._MenuButton.MenuText, v => v.textBox1.Text));
                x(this.BindCommand(ViewModel, vm => vm.ExampleCommand, v => v.button1));
                x(this.BindCommand(ViewModel, vm => vm.ExampleCommandText, v => v.textBox1, "DoubleClick"));
                x(this.Bind(ViewModel, vm => vm._MenuButtons, v => v.dataGridView1.DataSource));
                x(this.BindCommand(ViewModel, vm => vm.ExampleCommandAdd, v => v.button2));
              

            });
          
            ViewModel = new MenuButtonModelView();
        }
        
        public MenuButtonModelView ViewModel { get; set; }
        object IViewFor.ViewModel { get => ViewModel; set => ViewModel = (MenuButtonModelView)value; }

      
    }
}

最终显示效果:

总结:第一次写文章,做个记录,上面的DataGrivdView 数据无法进行实时更新,有那位大佬知道的麻烦指导一下.绑定命令时传递参数在官网也没有找到相关实列. 官网:  ReactiveUIUsing ReactiveUI for WinForms MVVM Design - CodeProject

ps: DataGrid View 数据更新声明集合时采用BindingList  就可以及时更新

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ReactiveUI是一个实现了MVVM模式的框架,它可以在WinForm使用。它的核心目标是在ViewModel中的属性发生变化时执行相应的命令。ReactiveUI支持多种平台,包括WinForm。通过使用ReactiveUI,可以提高WinForm开发的效率。 在使用ReactiveUI开发WinForm时,可以使用ReactiveUI.WinForms版本。这个版本提供了与WinForm相关的功能和特性。 在使用ReactiveUI开发WinForm时,可以使用设计模式来提高开发效率。ReactiveUI提供了MVVM模式的实现,可以在ViewModel中处理数据的显示和提交。通过使用ReactiveUI,可以实现数据的双向绑定,当textBox中的内容发生变化时,ViewModel中相应的属性也会跟着改变,并将改变反映在textBox和label中。 示例项目的结构可以参考引用\[2\]中的图示。在示例项目中,可以看到使用ReactiveUI实现了MVVM模式,当修改textBox中的内容时,ViewModel中相应的属性也会跟着改变,并将改变反映在textBox和label中。 总结来说,ReactiveUI是一个可以在WinForm使用的实现了MVVM模式的框架。通过使用ReactiveUI,可以提高WinForm开发的效率,并实现数据的双向绑定。希望这个简单示例可以帮助想要在WinForm使用MVVM模式进行开发的开发者们。 #### 引用[.reference_title] - *1* *2* [MVVM模式开发WinForm-ReactiveUI](https://blog.csdn.net/wang_j90/article/details/80469750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [WinformReactiveUI的Collection型数据与控件的绑定](https://blog.csdn.net/SkyLee708/article/details/118391509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值