我知道这是旧的,但我仍然不太了解这些问题.任何人都可以告诉我为什么以下不起作用(抛出一个runtime关于铸造的例外)?
public abstract class EntityBase { }
public class MyEntity : EntityBase { }
public abstract class RepositoryBase<T> where T : EntityBase { }
public class MyEntityRepository : RepositoryBase<MyEntity> { }
Run Code Online (Sandbox Code Playgroud)
而现在的铸造生产线:
MyEntityRepository myEntityRepo = GetMyEntityRepo(); // whatever
RepositoryBase<EntityBase> baseRepo = (RepositoryBase<EntityBase>)myEntityRepo;
Run Code Online (Sandbox Code Playgroud)
那么,任何人都可以解释这是如何无效的?而且,我没有心情去解释 - 是否有一行代码我可以用来实际进行演员表演?
我有一个按钮:
<Button x:Name="MyButton" Command="SomeCommand"/>
Run Code Online (Sandbox Code Playgroud)
有没有办法从源执行命令?调用按钮上的单击没有帮助:
MyButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Run Code Online (Sandbox Code Playgroud)
我的意思是 - 这确实引发了事件,但它没有提高命令.有什么类似于此RaiseEvent但只是为了命令?如果没有 - 我怎样才能实例化ExecutedRoutedEventArgs?可能吗?
最后 - 请不要告诉我如何avoid调用命令.
好吧,这一直困扰着我一段时间.我想知道其他人如何处理以下情况:
<ComboBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding SelectedItem}"/>
Run Code Online (Sandbox Code Playgroud)
DataContext对象的代码:
public ObservableCollection<MyItem> MyItems { get; set; }
public MyItem SelectedItem { get; set; }
public void RefreshMyItems()
{
MyItems.Clear();
foreach(var myItem in LoadItems()) MyItems.Add(myItem);
}
public class MyItem
{
public int Id { get; set; }
public override bool Equals(object obj)
{
return this.Id == ((MyItem)obj).Id;
}
}
Run Code Online (Sandbox Code Playgroud)
显然,当RefreshMyItems()调用该方法时,组合框会收到Collection Changed事件,更新其项目并且在刷新的集合中找不到SelectedItem =>将SelectedItem设置为null.但我需要组合框使用Equals方法来选择新集合中的正确项目.
换句话说 - ItemsSource集合仍然包含正确的MyItem,但它是一个new对象.而且我希望组合框能够使用类似的东西Equals来自动选择它(这更加困难,因为首先是源集合调用Clear()重置集合并且此时已将SelectedItem设置为null).
更新2在复制粘贴下面的代码之前,请注意它远非完美!请注意,默认情况下它不会绑定两种方式.
更新 …
关于这个主题有很多问题,但我的版本略有改动.
我们有以下代码:
interface IFoo { }
interface IBar : IFoo { }
class Foo : IFoo { }
class Bar : IBar { }
bool Implements_IFoo(Type type) { /* ??? */ }
Run Code Online (Sandbox Code Playgroud)
现在,故事的转折:Implements_IFoo当Type仅实现IFoo而不是从IFoo派生的任何接口时,该方法应该只返回true.这里举例说明此方法的一些示例:
Implements_IFoo(typeof(Foo)); // Should return true
Implements_IFoo(typeof(Bar)); // Should return false as Bar type
// implements an interface derived from IFoo
Run Code Online (Sandbox Code Playgroud)
请注意,可能有许多从IFoo派生的接口,您不一定知道它们的存在.
显而易见的方法是通过反射找到从IFoo派生的所有接口,然后只检查typeof(Bar).GetInterfaces()就是那里存在的任何一个.但我想知道是否有人可以提出更优雅的解决方案.
PS问题源于我发现的一些代码,它们使用了对class(if(obj.GetType() == typeof(BaseClass)) { ... })的检查.我们正在用特定代码的接口替换类.另外,以防万一 - 我将此检查作为布尔标志实现,所以这个问题纯粹是假设的.
简短的问题 - 简单易懂的词汇中真正的区别是什么?
来自MSDN的摘录:
区别在于这些模式的描述的最后部分.但我无法理解.任何人都可以用更人道的方式解释它吗?
这是一个关于非常简单的构造的问题 - 我有以下XAML:
<Viewbox Height="100" Stretch="Uniform">
<TextBlock FontFamily="Georgia">My Cool Text</TextBlock>
</Viewbox>
Run Code Online (Sandbox Code Playgroud)
这很容易理解.然而,当我启动程序时,我得到了奇怪的模糊文本(我的项目中没有任何位图效果).
(左侧 - VS2010中的设计器视图,右侧 - 正在运行的应用程序)
有没有人有任何关于为什么会发生这种情况的建议?
我在我的项目中使用EntityFramework POCO +代理+延迟加载.今天,我很奇怪为什么看到该类Transaction具有其相关集合Rows物化到HashSet(而不是EntityCollection).我需要EntityCollection跟踪集合中的更改.
public class Transaction
{
public virtual ICollection<TransactionRow> Rows { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然而,其他实体类将其相关集合具体化EntityCollection.
我正在加载Transaction通过ObjectQuery,所以它应该在上下文中.还创建了对象的代理.
任何人都可以告诉 - 实体框架如何决定使用什么 - HashSet或EntityCollection?为什么有些东西会成为HashSets?
entity-framework poco hashset entity-framework-4 entitycollection
这只是一个讨论的问题-在WPF中进行视图/编辑控件的最佳方法是什么?例如,我们有一个实体对象Person,它具有一些道具(名称,姓氏,地址,电话等)。控件的一种表示形式是只读视图。另一个将对该人具有编辑视图。例:
<UserControl x:Name="MyPersonEditor">
<Grid>
<Grid x:Name="ViewGrid" Visibility="Visible">
<TextBlock Text="Name:"/>
<TextBlock Text="{Binding Person.Name}"/>
<Button Content="Edit" Click="ButtonEditStart_Click"/>
</Grid>
<Grid x:Name="EditGrid" Visibility="Collapsed">
<TextBlock Text="Name:"/>
<TextBox Text="{Binding Person.Name}"/>
<Button Content="Save" Click="ButtonEditEnd_Click"/>
</Grid>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我希望这个想法很明确。我现在看到的两个选项
这只是一个讨论问题,没有太多麻烦,但我只是想知道是否还有其他可能性和解决方案。
我在刷新相关的实体集合时遇到了一些麻烦.
基本上问题如下:
public class Student
{
public virtual ICollection<Lecture> Lectures { get; set; }
public void AddLecture(Lecture lecture)
{
Lectures.Add(lecture);
}
public void CancelChanges()
{
_context.Refresh(RefreshMode.StoreWins, this);
_context.LoadProperty(this, (o) => o.Lectures,
MergeOption.OverwriteChanges);
}
}
public class Grade
{
public virtual Student { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我有一些用于添加讲座的GUI,如果我们想要,我们可以取消编辑过程:
public void ExampleEdit()
{
Student student = _context.Students.SingleOrDefault(/* blah */);
student.AddLecture(_context.Lectures.SingleOrDefault(/* e.g. math */));
student.CancelChanges();
// At this point student SHOULD have no lectures anymore since the
// property was loaded with overwrite …Run Code Online (Sandbox Code Playgroud) 这只是一个"确定"的问题..NET 4中的x64系统的递归/循环是否存在任何已知的特殊限制/错误?
我的情况非常简单 - 一个程序在任何x86系统上运行正常,但它在x64系统上崩溃与StackOverflowException(该程序包含一些xml /映射代码,包含在几个地方的递归等).
目前解决此问题的唯一方法是调用corflags /32BIT+我的程序集(程序开始正常运行).但我想知道 - 是否有任何特殊情况可以导致x64上的错误/问题?
谢谢.
问题:
Grid_KeyUp)MessageBox.Show(...))Keys.Return) 关闭MessageBox我有一个非常简单的孤立样本,它显示了我遇到的问题。只需创建一个空的 WPF 项目并使用此 XAML:
<Grid KeyUp="Grid_KeyUp">
<Button Click="Button_Click"></Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
以及隐藏的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Grid_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("KEYUP: " + e.Key);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "TEST");
}
}
Run Code Online (Sandbox Code Playgroud)
如果单击该按钮并按 [Return] 关闭 MessageBox,输出窗口将打印:
KEYUP: Return
Run Code Online (Sandbox Code Playgroud)
奇怪的事实:
KEYUP: Escape,但是当我按 [Space] 时,控制台不会打印任何内容! …我使用:EntityFramework + POCO
这是事情:
public interface IBaseType
{
int Id { get; set; }
}
public class BaseType : IBaseType
{
public virtual int Id { get; set; }
}
public class DerivedType : BaseType
{
}
Run Code Online (Sandbox Code Playgroud)
问题:
public class EntityFetcher<T> where T : BaseType
{
public object GetById(int id)
{
ObjectSet<T> objectSet = (ObjectSet<T>)GetTheObjectSet(typeof(T));
return objectSet.SingleOrDefault((o) => o.Id == id);
}
}
Run Code Online (Sandbox Code Playgroud)
如果T是BaseType这一切完美的作品,但是:问题是,在当的EntityFramework一个类继承了另一个他们分享的ObjectSet,因此,如果T是DerivedType则GetTheObjectSet返回ObjectSet<BaseType>,不能被强制转换为ObjectSet<DerivedType>. …
wpf ×6
.net ×2
c# ×2
casting ×2
poco ×2
64-bit ×1
collections ×1
combobox ×1
command ×1
controls ×1
covariance ×1
crud ×1
generics ×1
hashset ×1
inheritance ×1
interface ×1
keyup ×1
mode ×1
objectset ×1
recursion ×1
reflection ×1
routedevent ×1
selecteditem ×1
viewbox ×1