ReactiveUI 9.19.5版绑定不适用于Xamarin.Forms 4.1.0版
我正试着用ReactiveUI 9.19.5版绑定不适用于Xamarin.Forms 4.1.0版,xamarin.forms,reactiveui,Xamarin.forms,Reactiveui,我正试着用ReactiveUI来改变我的想法。我创建了一个包含以下主要包的新项目: Xamarin.表格4.1.0.618606 属性已更改。Fody 3.0.1 反应UI 9.19.5 ReactiveUI.XamForms 9.19.5 该项目有一个页面,其中包含关联的视图模型。我想使用ReactiveUI将视图与ViewModel绑定。但是绑定不起作用。项目生成并运行,但不会触发任何属性更改通知或任何命令。应用程序需要显示公司名称的列表。但该列表不显示在ViewModel中定义的集合。应用
ReactiveUI
来改变我的想法。我创建了一个包含以下主要包的新项目:
页面
,其中包含关联的视图模型
。我想使用ReactiveUI
将视图与ViewModel绑定。但是绑定不起作用。项目生成并运行,但不会触发任何属性更改通知
或任何命令
。应用程序需要显示公司名称的列表
。但该列表不显示在ViewModel
中定义的集合。应用程序应允许用户使用collectionchangecondrom
对列表进行排序,每次搜索query
查看代码:
<rxui:ReactiveContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rxui="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
xmlns:local="clr-namespace:Demo"
x:TypeArguments="local:MainViewModel"
x:Class="Demo.MainPage">
<Shell.TitleView>
<SearchBar x:Name="SearchHandler"
Placeholder="SelectCompany" />
</Shell.TitleView>
<StackLayout>
<ListView x:Name="CompaniesListView"
ItemsSource="{Binding Companies}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout Orientation="Horizontal"
Margin="10">
<Label x:Name="NameLabel"
Text="{Binding Name}" />
</StackLayout>
<BoxView HorizontalOptions="FillAndExpand"
HeightRequest="1"
Color="BlueViolet" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="ClickMe"
x:Name="OnlyButton" />
</StackLayout>
</rxui:ReactiveContentPage>
public partial class MainPage : ReactiveContentPage<MainViewModel>
{
MainViewModel vm;
public MainPage()
{
InitializeComponent();
this.Bind(ViewModel, vm => vm.Query, v => v.SearchHandler.Text);
this.BindCommand(ViewModel, vm => vm.ButtonClickedCommand, v => v.OnlyButton);
}
}
[AddINotifyPropertyChangedInterface]
public class MainViewModel : ReactiveObject
{
public ObservableCollection<Company> Companies { get; private set; }
public ReactiveCommand<Unit, Unit> ButtonClickedCommand { get; set; }
public ReactiveCommand<Unit, Unit> CollectionChange { get; set; }
public string Query { get; set; }
public MainViewModel()
{
Companies = new ObservableCollection<Company>
{
new Company{Name="EPF CORPORATION"},
new Company{Name="ABC COMPANY PVT. LTD."},
new Company{Name="UNIQUE COMPUTER SYSTEMS"},
new Company{Name="MICROSOFT PRIVATE LIMITED"},
};
this.WhenAny(x => x.Query, x => !string.IsNullOrWhiteSpace(x.Value));
CollectionChange = ReactiveCommand.CreateFromTask(async () => await SortCollection());
ButtonClickedCommand = ReactiveCommand.CreateFromTask(async () => await ButtonClicked());
async Task SortCollection()
{
ObservableCollection<Company> temp;
temp = new ObservableCollection<Company>(Companies.OrderByDescending(m => m.Name.ToLower().StartsWith(Query.ToLower(), StringComparison.CurrentCulture)));
Companies.Clear();
foreach (Company c in temp)
Companies.Add(c);
}
async Task ButtonClicked()
{
await Shell.Current.DisplayAlert("Button Clicked", "The reactive button command fired finally", "OK");
}
}
隐藏的代码:
<rxui:ReactiveContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rxui="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
xmlns:local="clr-namespace:Demo"
x:TypeArguments="local:MainViewModel"
x:Class="Demo.MainPage">
<Shell.TitleView>
<SearchBar x:Name="SearchHandler"
Placeholder="SelectCompany" />
</Shell.TitleView>
<StackLayout>
<ListView x:Name="CompaniesListView"
ItemsSource="{Binding Companies}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout Orientation="Horizontal"
Margin="10">
<Label x:Name="NameLabel"
Text="{Binding Name}" />
</StackLayout>
<BoxView HorizontalOptions="FillAndExpand"
HeightRequest="1"
Color="BlueViolet" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="ClickMe"
x:Name="OnlyButton" />
</StackLayout>
</rxui:ReactiveContentPage>
public partial class MainPage : ReactiveContentPage<MainViewModel>
{
MainViewModel vm;
public MainPage()
{
InitializeComponent();
this.Bind(ViewModel, vm => vm.Query, v => v.SearchHandler.Text);
this.BindCommand(ViewModel, vm => vm.ButtonClickedCommand, v => v.OnlyButton);
}
}
[AddINotifyPropertyChangedInterface]
public class MainViewModel : ReactiveObject
{
public ObservableCollection<Company> Companies { get; private set; }
public ReactiveCommand<Unit, Unit> ButtonClickedCommand { get; set; }
public ReactiveCommand<Unit, Unit> CollectionChange { get; set; }
public string Query { get; set; }
public MainViewModel()
{
Companies = new ObservableCollection<Company>
{
new Company{Name="EPF CORPORATION"},
new Company{Name="ABC COMPANY PVT. LTD."},
new Company{Name="UNIQUE COMPUTER SYSTEMS"},
new Company{Name="MICROSOFT PRIVATE LIMITED"},
};
this.WhenAny(x => x.Query, x => !string.IsNullOrWhiteSpace(x.Value));
CollectionChange = ReactiveCommand.CreateFromTask(async () => await SortCollection());
ButtonClickedCommand = ReactiveCommand.CreateFromTask(async () => await ButtonClicked());
async Task SortCollection()
{
ObservableCollection<Company> temp;
temp = new ObservableCollection<Company>(Companies.OrderByDescending(m => m.Name.ToLower().StartsWith(Query.ToLower(), StringComparison.CurrentCulture)));
Companies.Clear();
foreach (Company c in temp)
Companies.Add(c);
}
async Task ButtonClicked()
{
await Shell.Current.DisplayAlert("Button Clicked", "The reactive button command fired finally", "OK");
}
}
public分部类主页面:ReactiveContentPage
{
MainViewModelVM;
公共主页()
{
初始化组件();
Bind(ViewModel,vm=>vm.Query,v=>v.SearchHandler.Text);
这个.bind命令(ViewModel,vm=>vm.ButtonClickedCommand,v=>v.OnlyButton);
}
}
视图模型代码:
<rxui:ReactiveContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rxui="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
xmlns:local="clr-namespace:Demo"
x:TypeArguments="local:MainViewModel"
x:Class="Demo.MainPage">
<Shell.TitleView>
<SearchBar x:Name="SearchHandler"
Placeholder="SelectCompany" />
</Shell.TitleView>
<StackLayout>
<ListView x:Name="CompaniesListView"
ItemsSource="{Binding Companies}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout Orientation="Horizontal"
Margin="10">
<Label x:Name="NameLabel"
Text="{Binding Name}" />
</StackLayout>
<BoxView HorizontalOptions="FillAndExpand"
HeightRequest="1"
Color="BlueViolet" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="ClickMe"
x:Name="OnlyButton" />
</StackLayout>
</rxui:ReactiveContentPage>
public partial class MainPage : ReactiveContentPage<MainViewModel>
{
MainViewModel vm;
public MainPage()
{
InitializeComponent();
this.Bind(ViewModel, vm => vm.Query, v => v.SearchHandler.Text);
this.BindCommand(ViewModel, vm => vm.ButtonClickedCommand, v => v.OnlyButton);
}
}
[AddINotifyPropertyChangedInterface]
public class MainViewModel : ReactiveObject
{
public ObservableCollection<Company> Companies { get; private set; }
public ReactiveCommand<Unit, Unit> ButtonClickedCommand { get; set; }
public ReactiveCommand<Unit, Unit> CollectionChange { get; set; }
public string Query { get; set; }
public MainViewModel()
{
Companies = new ObservableCollection<Company>
{
new Company{Name="EPF CORPORATION"},
new Company{Name="ABC COMPANY PVT. LTD."},
new Company{Name="UNIQUE COMPUTER SYSTEMS"},
new Company{Name="MICROSOFT PRIVATE LIMITED"},
};
this.WhenAny(x => x.Query, x => !string.IsNullOrWhiteSpace(x.Value));
CollectionChange = ReactiveCommand.CreateFromTask(async () => await SortCollection());
ButtonClickedCommand = ReactiveCommand.CreateFromTask(async () => await ButtonClicked());
async Task SortCollection()
{
ObservableCollection<Company> temp;
temp = new ObservableCollection<Company>(Companies.OrderByDescending(m => m.Name.ToLower().StartsWith(Query.ToLower(), StringComparison.CurrentCulture)));
Companies.Clear();
foreach (Company c in temp)
Companies.Add(c);
}
async Task ButtonClicked()
{
await Shell.Current.DisplayAlert("Button Clicked", "The reactive button command fired finally", "OK");
}
}
[AddNotifyPropertyChangedInterface]
公共类MainViewModel:反应对象
{
公共可观测集合公司{get;private set;}
公共反应命令按钮ClickedCommand{get;set;}
公共反应命令集合更改{get;set;}
公共字符串查询{get;set;}
公共主视图模型()
{
公司=新的可观察集合
{
新公司{Name=“EPF CORPORATION”},
新公司{Name=“ABC公司私人有限公司”},
新公司{Name=“UNIQUE COMPUTER SYSTEMS”},
新公司{Name=“微软私人有限公司”},
};
this.whenny(x=>x.Query,x=>!string.IsNullOrWhiteSpace(x.Value));
CollectionChange=ReactiveCommand.CreateFromTask(异步()=>await-SortCollection());
ButtonClickedCommand=ReactiveCommand.CreateFromTask(异步()=>Wait ButtonClicked());
异步任务SortCollection()
{
可观察收集温度;
temp=newobserveCollection(companys.OrderByDescending(m=>m.Name.ToLower().StartsWith(Query.ToLower(),StringComparison.CurrentCulture));
公司;
foreach(c公司临时雇员)
加入(c);
}
异步任务按钮单击()
{
等待Shell.Current.DisplayAlert(“单击按钮”,“最后触发反应按钮命令”,“确定”);
}
}
我只想使用ReactiveObject
和ReactiveCommand
。我将坚持使用Xamarin.Forms Shell,直到ReactiveShell
不可用
如果有人能告诉我如何在xamarin.forms中正确使用ReactiveUI,我将不胜感激。在MainPage.xaml.cs中,您似乎从未创建过ViewModel 您正在对某个ReactiveObject使用INotifyPropertyChanged fody,但存在一个ReactiveUI fody。您不应在ReactiveObject上生成混合INotifyPropertyChange 您有一个
vm
属性,但也没有创建该属性
在ReactiveContentControl
上,它有一个名为ViewModel
的属性,您需要对该属性进行设置。该属性可以从另一个控件传入
同样在ViewModel.cs
文件中,您有一个this.WhenAny
但似乎对生成的可观察对象没有任何作用
还要注意,更好的排序方法可能是使用dynamicata框架,它现在是ReactiveUI系列的一部分
你可以这样做
ViewModel.cs:
public类MainViewModel:ReactiveObject
{
私人只读源列表公司;
私人只读只读可观察收藏公司;
//反应命令
公共反应命令按钮ClickedCommand{get;}
公共只读可观察收集公司=>\u分类公司;
[反应性]
公共字符串查询{get;set;}
公共主视图模型()
{
_公司=新源列表();
_companies.AddRange(新[]
{
新公司{Name=“EPF CORPORATION”},
新公司{Name=“ABC公司私人有限公司”},
新公司{Name=“UNIQUE COMPUTER SYSTEMS”},
新公司{Name=“微软私人有限公司”},
});
//延迟到每500毫秒更新一次。
var refreshObs=this.WhenAnyValue(x=>x.Query).Throttle(TimeSpan.frommissions(500));
_公司连接
.AutoRefreshOnObservable(=>refreshObs)
.Filter(m=>Query==null | | m.Name.IndexOf(Query,StringComparison.CurrentCultureIgnoreCase)>=0)//如果它包含文本的一部分。
.Sort(SortExpressionComparer.Ascending(t=>t.Name))
.ObserveOn(RxApp.MainThreadScheduler)
.绑定(外选公司)
.Subscribe();
ButtonClickedCommand=ReactiveCommand.CreateFromTask(异步()=>Wait ButtonClicked());
}
异步任务按钮单击()
{
等待Shell.Current.DisplayAler