是否有任何方法可以编写LINQ样式的"简写"代码,用于遍历抛出Exception的所有级别的InnerException?我宁愿写它而不是调用扩展函数(如下所示)或继承Exception类.
static class Extensions
{
public static string GetaAllMessages(this Exception exp)
{
string message = string.Empty;
Exception innerException = exp;
do
{
message = message + (string.IsNullOrEmpty(innerException.Message) ? string.Empty : innerException.Message);
innerException = innerException.InnerException;
}
while (innerException != null);
return message;
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个List<bool>.我需要获取前n项的索引,其中item value = true.
例如以下列表项(bool)
10011001000
TopTrueIndexes(3) = The first 3 indexes where bits are true are 0, 3, 4
TopTrueIndexes(4) = The first 4 indexes where bits are true are 0, 3, 4, 7
Run Code Online (Sandbox Code Playgroud)
我怎么能为此写一个lambda?
我已经阅读了Josh Smiths关于使用RelayCommand查看模型的绑定命令的文章.但是,我需要将ApplicationCommands.Save绑定到视图模型,以便当用户单击保存菜单项时,它将在窗口中处理.这怎么可能?
我的点网组件中的用户控件中有一个依赖项属性(字符串列表),如下所示
public partial class ItemSelectionUserControl : UserControl
{
public List<string> AvailableItems
{
get { return (List<string>)this.GetValue(AvailableItemsProperty); }
set { this.SetValue(AvailableItemsProperty, value); }
}
public static readonly DependencyProperty AvailableItemsProperty = DependencyProperty.Register(
"AvailableItems", typeof(List<string>), typeof(ItemSelectionUserControl), new FrameworkPropertyMetadata{BindsTwoWayByDefault =true});
public ItemSelectionUserControl()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在另一个用户控件中使用此usercontrol在不同的程序集中,如下所示
<UserControl
xmlns:ctrl="clr-namespace:HH.Windows.UserControls;assembly=HH.Windows.UserControls"
/>
// .....
<Grid>
<ctrl:ItemSelectionUserControl Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" AvailableItems="{Binding Path=CheckList}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我可以看到CheckList的get访问器被调用,但它没有设置依赖属性"AvailableItems".永远不会调用"AvailableItems"集中的断点.我究竟做错了什么?
我有一个清单.出于正当理由,我多次复制List并将其用于不同目的.在某些时候,我需要检查所有这些集合的内容是否相同.
好吧,我知道怎么做.但作为"短手"编码(linq ...)的粉丝,我想知道我是否能用最短的代码行检查这个效率.
List<string> original, duplicate1, duplicate2, duplicate3, duplicate4
= new List<string();
//...some code.....
bool isequal = duplicate4.sequenceequal(duplicate3)
&& duplicate3.sequenceequal(duplicate2)
&& duplicate2.sequenceequal(duplicate1)
&& duplicate1.sequenceequal(original);//can we do it better than this
Run Code Online (Sandbox Code Playgroud)
UPDATE
Codeinchaos指出了我没有想到的某些语句(重复和列表顺序).虽然sequenceequal会处理重复,但列表的顺序可能是个问题.所以我改变代码如下.我需要复制列表.
List<List<string>> copy = new List<List<int>> { duplicate1, duplicate2,
duplicate3, duplicate4 };
bool iseqaul = (original.All(x => (copy.All(y => y.Remove(x))))
&& copy.All(n => n.Count == 0));
Run Code Online (Sandbox Code Playgroud)
UPDATE2
感谢Eric使用HashSet可以非常有效,如下所示.这不会覆盖重复.
List<HashSet<string>> copy2 =new List<HashSet<string>>{new HashSet<string>(duplicate1),
new HashSet<string>(duplicate2),
new HashSet<string> duplicate3),
new HashSet<string>(duplicate4)};
HashSet<string> origninalhashset = new HashSet<string>(original);
bool eq = copy2.All(x …Run Code Online (Sandbox Code Playgroud) 我想编写一个应用程序来处理某些用户操作.
应用程序将始终是透明的,应该点击进入.因此,将看到后面的窗口,并且当透明的应用程序点击时,我应该能够点击后面的窗口.我只想在透明的应用程序中处理某些用户操作(如双击).
是否有可能实现这一目标?任何指南都表示赞赏.
我有一个表达式帮助器来帮助从对象层次结构中获取值
public static Func<T, object> GetMemberExpressionFunc<T>(string expressionString)
{
if (string.IsNullOrEmpty(expressionString))
{
throw new InvalidOperationException("invalid Expression");
}
var parameter = Expression.Parameter(typeof(T));
Expression memberExpression = parameter;
var tokens = expressionString.Split('.');
memberExpression = tokens.Aggregate(memberExpression, Expression.PropertyOrField);
var convertExpression = Expression.Convert(memberExpression, typeof(object));
return Expression.Lambda<Func<T, object>>(convertExpression, parameter)
.Compile();
}
Run Code Online (Sandbox Code Playgroud)
用法
public class A
{
public B BObj { get; set; }
}
public class B
{
public string Name { get; set; }
}
static void Main(string[] args)
{
var obj = new A {BObj = …Run Code Online (Sandbox Code Playgroud) 编辑:基本问题是将List绑定到ListBox(或任何其他控件).所以我正在编辑这个问题.
我将一个字符串列表绑定到ListBox,如下所示.但是,当我更改文本框的内容时,它不会更改源列表中的字符串.为什么?
public partial class MainWindow : Window
{
List<string> _nameList = null;
public List<string> NameList
{
get
{
if (_nameList == null)
{
_nameList = new List<string>();
}
return _nameList;
}
set
{
_nameList = value;
}
}
public MainWindow()
{
NameList.Add("test1");
NameList.Add("test2");
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
和XAML
<ListBox Grid.Row="0" Grid.Column="0" DataContext="{Binding ElementName=main}" ItemsSource="{Binding NameList}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding .,Mode=OneWayToSource , UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我们的应用程序中有几个模块.我有一个项目,其中包含一组用于模块的自定义用户控件.请注意,此用户控件不会注入模块,而是用作XAML内联中的用户控件.
现在,用户控件可以在加载时抛出异常.我想在模块级别处理异常.我的想法是将一个eventaggregator注入模块构造函数,并在捕获异常时触发错误事件.
例如,模块的主视图如下
XAML
<inf:DockBase
x:Class="MainView"
xmlns:inf="clr-namespace:Infrastructure"
xmlns:usercontrols="clr-namespace:UserControls;assembly=Myusercontrollib">
<Grid>
<!--some code here-->
<usercontrols:CustomListControl source={Binding myList}/>
</Grid>
</inf:DockBase>
Run Code Online (Sandbox Code Playgroud)
代码背后
public MainView()
{
InitializeComponent();
}
public MainView(MainViewViewModel viewmodel, IEventAggregator eventAggregator)
:this()
{
_eventAggregator = eventAggregator;
}
Run Code Online (Sandbox Code Playgroud)
我在哪里可以从模块级别的用户控件中捕获激活?
我有两个从不同来源填充的列表,检查两个列表是否包含相同项目的最佳方法是什么?订单并不重要
List<Tuple<string, string, string>> list1;
List<Tuple<string, string, string>> list2;
Run Code Online (Sandbox Code Playgroud) 我需要修改List<int> 索引.我有另一个索引List<int>
List<int> values;
List<int> indexes;
indexes.Select(i=>values[i]).ForEach(val=>val = 0);
Run Code Online (Sandbox Code Playgroud)
当然,我知道上面的语法不会起作用.但是,我知道需要修改的项目的索引,我想用lambda修改索引列表.有可能吗?
我ItemsControl在WPF中使用了一个窗口.itemscontrol与集合绑定,集合是一组视图模型(用户控件).我的问题 - 由于集合中的许多视图模型,视图超出了当前窗口.我用滚动条尝试了很多东西来处理它但没用.有什么建议?问题是如何在窗口中包含itemscontrol(滚动)?
下面的XAML
<Window x:Class="WpfApplicationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WpfApplicationTest"
Title="MainWindow" Height="350" Width="525">
<Grid Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ItemsControl Grid.Row="0" Grid.Column="0" ItemsSource="{Binding UserControlCollection}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Bottom">
<Button Content="OK" Width="100" Margin="3" />
<Button Content="Cancel" Width="100" Margin="3" />
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我们的应用程序是模块化的(模块组执行特定的事情).这些模块具有事件处理程序.可以从其他模块或应用程序菜单触发这些事件.
情况:
模块A(具有UI)接收事件"deleteitem".事件参数应包含要删除的项目名称.但在这种情况下它是null.在某个地方,有人搞砸了什么.
问题(S):
模块应该抛出吗?记住,该模块将抛入事件处理程序并可能导致应用程序崩溃,因为模块编写者不知道是否处理了异常.
上述场景是关于从模块excpetions这可能导致应用程序崩溃的投掷一个更大的问题的snaphot.反对它的论点是应用程序可以在没有特定模块的情况下继续工作.那么,谁应该确保 - 模块或应用程序?
c# ×11
wpf ×7
lambda ×2
mvvm ×2
c#-4.0 ×1
data-binding ×1
exception ×1
itemscontrol ×1
itemsource ×1
linq ×1
list ×1
listbox ×1
modularity ×1
prism ×1
relaycommand ×1
windows ×1
wpfdatagrid ×1