我正在尝试组合这样的函数列表.
我有这个:
Func<int, bool>[] criteria = new Func<int, bool>[3];
criteria[0] = i => i % 2 == 0;
criteria[1] = i => i % 3 == 0;
criteria[2] = i => i % 5 == 0;
Run Code Online (Sandbox Code Playgroud)
我想要这个:
Func<int, bool>[] predicates = new Func<int, bool>[3];
predicates[0] = i => i % 2 == 0;
predicates[1] = i => i % 2 == 0 && i % 3 == 0;
predicates[2] = i => i % 2 == 0 && i % …Run Code Online (Sandbox Code Playgroud) 我正在尝试决定使用静态方法来加载/保存对象,还是使用构造函数/实例方法.
所以,对于object来说Project,实例版本就是
public Project(path) { // Load project here }
public void Save(path) { // Save project here }
Run Code Online (Sandbox Code Playgroud)
而静态版本将是
public static Project Load(path) { // Load project and return result }
public static void Save(path, proj) { // Save project }
Run Code Online (Sandbox Code Playgroud)
所以,你更喜欢哪一个?
更改列表框中的所选项目时,出现一个奇怪的错误,其中更改的项目显示为选中状态,但无法取消选择或重新选择它。
有没有办法解决这个问题?
这是一个演示该问题的示例应用程序。
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new WindowViewModel();
lst.SelectedIndex = 0;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((WindowViewModel)this.DataContext).Items[0] = "New Item";
}
}
public class WindowViewModel
{
public WindowViewModel()
{
Items = new ObservableCollection<string>();
Items.Add("Item1");
Items.Add("Item2");
Items.Add("Item3");
}
public ObservableCollection<string> Items { get; set; }
}
<Window x:Class="WpfSelectionIssue.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<Button Content="Change" Click="Button_Click" />
<ListBox x:Name="lst" ItemsSource="{Binding Items}" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
ImageOfIssue http://img136.imageshack.us/img136/9396/wpfselectionissue.jpg
我正在使用ReactiveUI和提供的ReactiveCollection<>类.
在ViewModel中我有一组对象,我希望创建一个observable来监视这些项目的IsValid属性.
这是我想要解决的方案.在我的ViewModel的构造函数中.
this.Items = new ReactiveCollection<object>();
IObservable<bool> someObservable = // ... how do I watch Items so when
// any items IsValid property changes,
// this observable changes. There
// is an IValidItem interface.
this.TheCommand = new ReactiveCommand(someObservable);
...
interface IValidItem { bool IsValid { get; } }
Run Code Online (Sandbox Code Playgroud)
编辑保罗的回答让我大部分都在那里.解决方案如下.
this.Items = new ReactiveCollection<object>();
this.Items.ChangeTrackingEnabled = true;
var someObservable = this.Items.Changed
.Select(_ => this.Items.All(i => i.IsValid));
Run Code Online (Sandbox Code Playgroud) 在javascript中我可以创建一个行为如下:
function* idMaker(){
var index = 0;
while(true)
yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
Run Code Online (Sandbox Code Playgroud)
C#等价物会是什么样的?
我想知道这是否会奏效:
static System.Collections.Generic.IEnumerable<int> MakeId()
{
int index = 0;
while (true)
yield return index++;
}
Run Code Online (Sandbox Code Playgroud)
但是根据我对C#的理解到目前为止,上述内容不会像我想要的那样工作,而是无限循环.
我正在尝试为一个模块编写一个单元测试,它会根据某些标准给出一个随机的数字列表.
我正在写的特定测试是对原始序列的重新洗牌.我正在测试那个
这样做的问题是,有时序列是在相同的顺序.处理这个问题的最佳方法是什么?
我正在使用NUnit(但如果它有帮助,可以使用另一个测试框架).
.net ×4
c# ×4
architecture ×1
enumerable ×1
expression ×1
generator ×1
iterator ×1
linq ×1
listbox ×1
nunit ×1
random ×1
reactiveui ×1
testing ×1
unit-testing ×1
wpf ×1
xaml ×1