我有一个包含任意数量的列表框 UIElement大小未知s。
我希望能够在添加每个项目后跟踪列表框的建议大小。这将允许我将一个大列表(例如:100 个项目)拆分为几个(例如:10)个大致相同视觉效果的较小列表大小,而不管列表中每个元素的视觉大小。
但是,似乎 Measure 传递仅在第一次调用 Measure 时影响ListBox的DesiredSize属性:
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
ListBox listBox = new ListBox();
this.Content = listBox;
// Add the first item
listBox.Items.Add("a"); // Add an item (this may be a UIElement of random height)
listBox.Measure(new Size(double.MaxValue, double.MaxValue)); // Measure the list box after the item has been added
Size size1 = listBox.DesiredSize; // reference to the size the ListBox "wants" …Run Code Online (Sandbox Code Playgroud) 我继承了一个没有经过深思熟虑的代码库,并且遇到了一些循环引用问题.我的团队目前没有时间或资源去做一个巨大的重构,所以我想知道是否有一些我可以翻转的开关说"以原子方式构建这两个dll".
一个简化的例子:
DLL D0:
class A1 -> References B1
class B1 -> References A1
Run Code Online (Sandbox Code Playgroud)
A1和B1类相互引用,因为它们在同一个DLL中,这很好 - 但它们并没有太多共同点,所以我想把它们分成不同的dll.
但是,由于循环引用问题,以下是不可能的:
DLL D1: class A1 -> References D2.B1
DLL D2: class B1 -> References D1.A1
Run Code Online (Sandbox Code Playgroud)
我希望能够告诉MSBuild构建(D1 + D2),好像所有代码都在一个dll中一样.我可以在这做什么,或者我是否遵循SOP并"入侵它?"
免责声明:我理解这意味着我的模型存在更大的问题(最明显的是接口的使用不当),但在小公司中,现实世界的场景要求我向后弯腰以修复生产错误而牺牲最佳做法.
我试图在XAML中填充自定义字符串数组,但收到错误.我将一个ComboBox子类化,并添加了一个我想用自定义值填充的字符串[]:
public class MyComboBox : ComboBox
{
public string[] MyProperty { get { return (string[])GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } }
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string[]), typeof(MyComboBox));
}
Run Code Online (Sandbox Code Playgroud)
我的XAML如下:
<Window x:Class="Samples.CustomArrayProperty"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Samples"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="CustomArrayProperty" Height="300" Width="300">
<Grid>
<local:MyComboBox Height="20">
<local:MyComboBox.MyProperty>
<sys:String>Monday</sys:String>
<sys:String>Wednesday</sys:String>
<sys:String>Friday</sys:String>
</local:MyComboBox.MyProperty>
</local:MyComboBox>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到错误:"'星期一'不是属性'MyProperty'的有效值.".
我究竟做错了什么?
如果我这样做,我会枚举我的程序中的所有类型:
List<SerializableAttribute> attributes=new List<SerializableAttribute>() ;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in assembly.GetTypes())
{
attributes.AddRange(
type.GetCustomAttributes(false)
.OfType<SerializableAttribute>()
.ToList());
}
}
Run Code Online (Sandbox Code Playgroud)
.NET dll附带的元数据是否被索引以允许我执行以下操作:
List<SerializableAttribute> attributes = typeof(SerializableAttribute)
.GetClassesIAmDefinedOn();
Run Code Online (Sandbox Code Playgroud)
还有其他选择我不考虑吗?
(SerializableAttribute只是一个例子)
有没有什么办法可以在我通过向执行的DetachedCriteria添加ICriterion来点击数据库之前在SubType字段上过滤我的NHibernate查询?
我的代码看起来像这样:
DetachedCriteria detachedCriteria = DetachedCriteria.For(typeof(MyObject));
ProjectionList projectionList = Projections.ProjectionList();
projectionList.Add(Projections.SqlProjection("{alias}.SubType as SubType", new string[] { "SubType" }, new IType[] { TypeFactory.GetAnsiStringType(15) }));
dc.SetProjection(projectionList);
dc.Add(Expression.Eq("SubType", "MYOBJECT"));
using(ISession session = ...)
{
ICriteria criteria = detachedCriteria.GetExecutableCriteria(session);
// Blows up because I don't know how to reference the SubType
// field with an ICriterion (Expression.Eq("SubType", "MYOBJECT"))
IList list = criteria.List();
...
}
Run Code Online (Sandbox Code Playgroud)
虽然这可能不是实现我的目标的正确方法,但我希望它至少是可能的,因为我不期待必须重构我期望/产生ICriterion的接口.我也不一定能够在我需要创建ICriterion对象的地方附近访问会话(但是我可以完全控制将使用的各种NHibernate字段/表的别名/命名).
如果我想获取某个目录中所有文件的列表,我可以输入如下内容:
var files = (new DirectoryInfo(@"C:\Temp")).GetFiles();
Run Code Online (Sandbox Code Playgroud)
但是,以下也有效(请注意"new"关键字之前缺少括号):
var files = new DirectoryInfo(@"C:\Temp").GetFiles();
Run Code Online (Sandbox Code Playgroud)
为什么允许这种情况发生?声明如下:
2 + 4 * 3 and (2 + 4) * 3
Run Code Online (Sandbox Code Playgroud)
由于括号,将分别解析为14和18.
看起来我的代码示例类似于在DirectoryInfo对象的静态版本上调用".GetFiles()"方法(因为缺少"new"关键字),其中括号与"new"关键字相结合明确表示我正在使用DirectoryInfo对象的实例.
即:
2 + 4 * 3
new DirectoryInfo() .GetFiles()
Run Code Online (Sandbox Code Playgroud)
VS:
(2 + 4 ) * 3
(new DirectoryInfo()) .GetFiles()
Run Code Online (Sandbox Code Playgroud)
为什么会出现这种情况,有一个简单的解释吗?C#词法分析器处理这两种情况(有/无括号)不是更有效吗?我的比喻有缺陷吗?
有没有办法让C#编译器OR在if语句中优化我的子句?
例如:
if(Function_that_returns_boolean() || boolean_value)
{
// Do something here...
}
Run Code Online (Sandbox Code Playgroud)
执行时间可能比完全不同
if(boolean_value || Function_that_returns_boolean())
{
// Do something here...
}
Run Code Online (Sandbox Code Playgroud)
取决于Function_that_returns_boolean()内部实际工作量.
编译器不太可能(也许不可能)确切地知道效率如何Function_that_returns_boolean(),并且我的所有(简短)测试都表明OR语句总是从左到右处理,即使使用最积极的编译器优化也是如此.
这是否已由编译器处理 (即我错了)?如果没有,有什么提示我可以给它吗?
也许类似的东西Attribute让编译器知道可以自由重新排列我的代码(如果不存在,可以保留它)?
[OrStatementUsage(Speed.Fast)] // Always push statement left when possible
public bool Fast_function_that_returns_boolean()
{
return a + b == c; // fast
}
[OrStatementUsage(Speed.Slow)] // Always push statement right when possible
public bool Slow_function_that_returns_boolean()
{
Thread.Sleep(1000);
return a + b == …Run Code Online (Sandbox Code Playgroud) c# ×5
wpf ×2
arrays ×1
icriteria ×1
itemscontrol ×1
listbox ×1
nhibernate ×1
optimization ×1
reflection ×1
size ×1
uielement ×1
xaml ×1