如何使用 ReactiveUI 和 winforms 绑定到用户控件
How to bind to user controls using ReactiveUI and winforms
我正在尝试学习 ReactiveUI。我一直在查看存储库 https://github.com/reactiveui/ReactiveUI/tree/master/samples/getting-started
中的 wpf 示例项目
我决定尝试将其转换为 winforms,但遇到了问题。
在 wpf 示例中,搜索功能通过 OAPH 填充 'Main' ViewModel (AppViewModel) 的 属性,称为 SearchResults,它是 'Child' ViewModels (NuGetDetailsViewModel) 的 IEnumerable
public IEnumerable<NugetDetailsViewModel> SearchResults => _searchResults.Value;
在 'Main' 视图(主窗口)中有一个列表框,它的 ItemSource 绑定到 SearchResults,即 ViewModels 的 IEnumerable。
似乎有一些魔法可以为给定的 ViewModel 找到并显示适当的视图。它甚至在评论中这样说:
In our MainWindow when we register the ListBox with the collection of NugetDetailsViewModels if no ItemTemplate has been declared it will search for a class derived off IViewFor and show that for the item.
在 winforms 中,我 认为 我有两个问题,但我可能会出一个..或更多:
- 寻找 ViewModel 的 View 的魔法似乎并没有起作用,但这可能是由于问题二造成的。
- 如何将 ViewModels 的 IEnumerable 绑定到 winforms 控件?
在 winforms 中,我使用 flowlayoutpanel 代替 ListBox 并尝试了以下几种变体:
this.OneWayBind(ViewModel, vm => vm.ResultsList, v => v.flowLayoutPanel1.DataBindings)
我已经能够直接在视图中使用一些转换代码来直接更新 flowLayoutPanel,但是它需要直接了解子视图并且不适合我,而且不像我想的那样自动像。
this.OneWayBind(ViewModel,
vm => vm.ResultsList,
v => v.flowLayoutPanel1,
selector: value =>
{
this.flowLayoutPanel1.Controls.Clear();
foreach (var value in values)
{
this.flowLayoutPanel1.Controls.Add(new AssemblyInfoView() { ViewModel = value });
}
return this.flowLayoutPanel1;
} ));
为清楚起见,链接到我的 'Child' ViewModel 的 'Child' 视图也源自 ReactiveUserControl。
我使用以下代码注册视图:
Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
并已检查它们是否已注册。
如果有人能够提供帮助,我们将不胜感激。
作为附录,如果有人知道一些使用 ReactiveUI 的更复杂的示例项目,特别是使用 winforms 的项目,那将非常有帮助。
谢谢。
你检查过这个样本了吗https://github.com/reactiveui/ReactiveUI/tree/master/samples/getting-started/ReactiveDemo?
在 ReactiveUI 9.11 中有一项新功能,允许您绑定到任何具有 Control.ControlCollection
的控件或 TableLayoutControlCollection
这将允许您将其自动添加到控件中。
这是通过一个名为 ISetMethodBindingConverter
的新接口提供的,它允许您覆盖我们绑定引擎中 'Set' 的工作方式。
现在可以在此处找到 WinForms 应用程序的示例:https://github.com/reactiveui/ReactiveUI.Samples/tree/master/winforms/ReactiveDemo
我正在尝试学习 ReactiveUI。我一直在查看存储库 https://github.com/reactiveui/ReactiveUI/tree/master/samples/getting-started
中的 wpf 示例项目我决定尝试将其转换为 winforms,但遇到了问题。
在 wpf 示例中,搜索功能通过 OAPH 填充 'Main' ViewModel (AppViewModel) 的 属性,称为 SearchResults,它是 'Child' ViewModels (NuGetDetailsViewModel) 的 IEnumerable
public IEnumerable<NugetDetailsViewModel> SearchResults => _searchResults.Value;
在 'Main' 视图(主窗口)中有一个列表框,它的 ItemSource 绑定到 SearchResults,即 ViewModels 的 IEnumerable。 似乎有一些魔法可以为给定的 ViewModel 找到并显示适当的视图。它甚至在评论中这样说:
In our MainWindow when we register the ListBox with the collection of NugetDetailsViewModels if no ItemTemplate has been declared it will search for a class derived off IViewFor and show that for the item.
在 winforms 中,我 认为 我有两个问题,但我可能会出一个..或更多:
- 寻找 ViewModel 的 View 的魔法似乎并没有起作用,但这可能是由于问题二造成的。
- 如何将 ViewModels 的 IEnumerable 绑定到 winforms 控件?
在 winforms 中,我使用 flowlayoutpanel 代替 ListBox 并尝试了以下几种变体:
this.OneWayBind(ViewModel, vm => vm.ResultsList, v => v.flowLayoutPanel1.DataBindings)
我已经能够直接在视图中使用一些转换代码来直接更新 flowLayoutPanel,但是它需要直接了解子视图并且不适合我,而且不像我想的那样自动像。
this.OneWayBind(ViewModel,
vm => vm.ResultsList,
v => v.flowLayoutPanel1,
selector: value =>
{
this.flowLayoutPanel1.Controls.Clear();
foreach (var value in values)
{
this.flowLayoutPanel1.Controls.Add(new AssemblyInfoView() { ViewModel = value });
}
return this.flowLayoutPanel1;
} ));
为清楚起见,链接到我的 'Child' ViewModel 的 'Child' 视图也源自 ReactiveUserControl。
我使用以下代码注册视图:
Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
并已检查它们是否已注册。
如果有人能够提供帮助,我们将不胜感激。
作为附录,如果有人知道一些使用 ReactiveUI 的更复杂的示例项目,特别是使用 winforms 的项目,那将非常有帮助。
谢谢。
你检查过这个样本了吗https://github.com/reactiveui/ReactiveUI/tree/master/samples/getting-started/ReactiveDemo?
在 ReactiveUI 9.11 中有一项新功能,允许您绑定到任何具有 Control.ControlCollection
的控件或 TableLayoutControlCollection
这将允许您将其自动添加到控件中。
这是通过一个名为 ISetMethodBindingConverter
的新接口提供的,它允许您覆盖我们绑定引擎中 'Set' 的工作方式。
现在可以在此处找到 WinForms 应用程序的示例:https://github.com/reactiveui/ReactiveUI.Samples/tree/master/winforms/ReactiveDemo