我最近升级到R#7.1并且我遇到了这个问题,其中To Property With Backing Field动作取代了我的支持字段并将它们移到了类的顶部.
例:
第1步:定义一个自动属性:
public class MyClass
{
//... Lots of members here
public int MyNewProperty {get;set;} // <- Create auto Property
}
Run Code Online (Sandbox Code Playgroud)
第2步:ReSharper的"带有支持领域的财产"

预期结果:
public class MyClass
{
//... Lots of members here
private int _myNewProperty; // <- Backing field immediately above property
public int MyNewProperty
{
get
{
return _myNewProperty;
}
set
{
_myNewProperty = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
获得的结果:
public class MyClass
{
private int _myNewProperty; // <- Backing field …Run Code Online (Sandbox Code Playgroud) 我们刚刚在代码中找到了这些:
public static class ObjectContextExtensions
{
public static T Find<T>(this ObjectSet<T> set, int id, params Expression<Func<T, object>>[] includes) where T : class
{
...
}
public static T Find<T>(this ObjectSet<T> set, int id, params string[] includes) where T : class
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,除了params.之外,它们具有相同的签名.
它们被用于多种方式,其中之一:
DBContext.Users.Find(userid.Value); //userid being an int? (Nullable<int>)
Run Code Online (Sandbox Code Playgroud)
对我来说奇怪的是,它解决了第一次超载.
Q1:为什么这不会产生编译错误?
Q2:为什么C#编译器将上述调用解析为第一种方法?
编辑:只是为了澄清,这是C#4.0,.Net 4.0,Visual Studio 2010.
我试图将2个不同的WPF控件绑定到ViewModel中的相同属性,一个CheckBox.IsChecked和一个Expander.IsExpanded.我想要实现的行为是让CheckBox影响ViewModel(因此也影响Expander),但不影响其他方式.就像是:
Checkbox Checked -> ViewModel property set to frue -> Expander.Expand
Checkbox Unchecked -> ViewModel property set to false -> Expander.Collapse
Expander Expanded -> Nothing else affected
Expander Collapsed -> Nothing else affected
Run Code Online (Sandbox Code Playgroud)
这是XAML:
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Expander IsExpanded="{Binding IsChecked, Mode=OneWay}">
<Expander.Header>
<CheckBox IsChecked="{Binding IsChecked}" Content="Is Checked"/>
</Expander.Header>
<TextBlock Text="Expanded!"/>
</Expander>
</Window>
Run Code Online (Sandbox Code Playgroud)
和守则:
using System.ComponentModel;
using System.Windows;
namespace WpfApplication9
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码:
AppDomain.CurrentDomain.AssemblyLoad += (sender, args) =>
{
var token = args.LoadedAssembly.GetName().GetPublicKeyToken();
if (!IsValidToken(token))
{
Process.GetCurrentProcess().Kill();
}
};
Run Code Online (Sandbox Code Playgroud)
凡IsValidToken()进行比较加载反对我作为字节数组应用硬编码授权的公钥标记列表组件的公钥标记.
这是防止代码注入攻击的良好安全措施吗?此外,鉴于我稍后将使用NetReactor混淆我的应用程序,这是否必要?我试图阻止任何"窥探"我的应用程序,不仅来自Snoop工具,而且来自任何外部不受欢迎的来源.
在WPF中,我可以添加任何UI到ListBoxItem通过提供的S ListBox有ItemTemplate:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Gray" CornerRadius="8" Padding="4,0,4,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Column="1" Content="Is Active Customer" IsChecked="{Binding IsActive}"/>
<Label Content="Id:" Grid.Row="1" HorizontalAlignment="Right"/>
<Label Content="Name:" Grid.Row="2" HorizontalAlignment="Right"/>
<TextBox Text="{Binding Id}" Grid.Row="1" Grid.Column="1"/>
<TextBox Text="{Binding Name}" Grid.Row="2" Grid.Column="1"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
结果是:

有没有办法在Windows窗体中实现相同的功能?
编辑:
1 -有什么办法,以实现在Windows窗体一样,同时保持separation of concerns之间View和Application Logic以这样的方式,如果我后来想完全重新定义View,我就不必重构整个应用程序?
2 - winforms是否支持数据绑定,使得我的每一个ListBoxItems都可以绑定到一个复杂的Entity,最终包括从Model数据到UI数据的中间类型转换,然后返回,这样我就不必写入大量的样板代码填充视图,然后将UI值传回模型以便保存? …
我是C#的新手,我有一个问题,在C++中我通常会使用friend标识符.现在我知道friendC#中不存在关键字,但我没有任何解决方法的经验(除了将所有类变量设为公共属性,如果可以的话我想避免使用).
我有以下场景:
public class A
{
public string Info { get; set; }
/* much more data */
}
public class B
{
private A m_instanceOfA;
public B(A a) { m_instanceOfA = a; }
public Info { get return A.info; set A.Info = value; }
/* And some more data of its own*/
}
public class C
{
private A m_instanceOfA;
// I need a constructor of C, which needs to set C.m_instanceOfA
// to the …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试将DataGrid.ItemsSource绑定到自定义RowCollection,它实现了IList和INotifyCollectionChanged:
Public Class RowCollection(of T)
Implements IList(Of T)
Implements INotifyCollectionChanged
Private _List As New List(Of T)
...
(对不起VB代码,我很快就会将所有代码翻译成C#.)
请注意,该类不是从任何现有的CLR集合派生的.我创建了自己的类,因为我需要覆盖GetItemAt,以实现记录分页.Collection内部添加和删除其自己的私有List中的对象_List.
现在,我可以在DataGrid中查看项目,但只要双击要编辑的单元格,我就会收到InvalidOperationException:'EditItems'不可用..
我的问题是,为了使我的集合与DataGrid完全兼容,我应该实现哪些其他接口?
对于初学者来说,也许有人可以帮助更好地命名这个问题。
我想要实现的目标:
下面是我想要实现的目标的一个非常简单的示例,仅使用基本工作部件就能够得出我正在寻找的解决方案。
我希望能够在 .ascx.cs 代码隐藏中创建以下文件:
public partial class DynamicControl<T> : System.Web.UI.UserControl where T : class
{
public List<T> Items;
public void Add(T item) {
Items.Add(item);
}
}
Run Code Online (Sandbox Code Playgroud)
在 .ascx 文件中正确引用此内容:
<% foreach (var item in Items) { %>
<p>item.Name</p>
<% } %>
Run Code Online (Sandbox Code Playgroud)
并将其加载到带有如下标记的 .aspx 文件中:
<custom:DynamicControl ID="DynamicControl1" ItemType="Person" runat="server" />
Run Code Online (Sandbox Code Playgroud)
或者甚至在 .aspx.cs 代码隐藏中为:
DynamicControl1.ItemType = Type.GetType("Person");
Run Code Online (Sandbox Code Playgroud)
真的,一切皆有可能。
我只需要一种在我的网站上创建通用用户控件的方法,我可以在其中以编程方式(或在标记中)设置项目类型,并使用 .ascx.cs 和 .ascx 文件以及引用的 .aspx 文件。
我尝试过的:
1)创建一个DynamicControlBuilder并继承该类ControlBuilder,然后使用 将该类附加为属性[ControlBuilder(typeof(DynamicControlBuilder))]。结果使得控件不会将泛型类型添加到 .aspx.designer.cs 文件中,并且 .ascx 文件也不知道如何呈现。
2)创建使用泛型类型的类的内部属性 …
我需要在列表框项目上添加功能,用户可以通过单独单击每个项目来选择项目,也可以执行shift +单击以选择列表中的一系列项目.
<ListBox ItemsSource="{Binding ItemFields, Mode=TwoWay}"
VerticalAlignment="Stretch" HorizontalAlignment="Left"
Margin="16,156,0,34" Name="fRListbox" Width="499" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="reportDatagrid_MouseDown"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
在xaml.cs上我写了下面的代码:
private void ListItem_MouseClick(object sender, MouseButtonEventArgs e)
{
if ((e.LeftButton == MouseButtonState.Pressed) && Keyboard.IsKeyDown(Key.RightShift))
{
fRListbox.SelectionMode = SelectionMode.Extended;
}
else if ((e.LeftButton == MouseButtonState.Pressed) && Keyboard.IsKeyDown(Key.LeftShift))
{
fRListbox.SelectionMode = SelectionMode.Multiple;
}
else if (e.LeftButton == MouseButtonState.Pressed)
{
fRListbox.SelectionMode = SelectionMode.Multiple;
}
}
Run Code Online (Sandbox Code Playgroud)
但Shift + Click功能无效.我是WPF的新手任何人都可以指导我.
我是WPF的新手,但我搜索了很多,最后决定向你们寻求帮助......
我有一个类 - 位置 主要属性为 -
地点名称
LocationID
我希望将此类绑定到WPF中的组合框.我从数据库中获取位置列表.我需要在组合框中显示列表,第一个文本/值对为 ---选择一个--- /-1.现在,到目前为止,我已经做到了 -
创建 -
public ObservableCollection<ComboBoxItem> cbLocationList { get; set; }
cbLocationList = new ObservableCollection<ComboBoxItem>();
SelectedcbDefaultLocationListItem = new ComboBoxItem { Content = "---Select One---" , Tag="-1"};
cbLocationList.Add(SelectedcbDefaultLocationListItem);
Run Code Online (Sandbox Code Playgroud)
将项目填入循环中 -
foreach (Location loc in LocationtList)
{
cbLocationList.Add(new ComboBoxItem { Content = loc.LocationName, Tag=loc.LocationID.ToString() });
}
Run Code Online (Sandbox Code Playgroud)
我在XAML 中将cbLocationList设置为 -
ItemsSource="{Binding cbLocationList}"
Run Code Online (Sandbox Code Playgroud)
组合框.这很好用,但在重置表单时,我需要将组合框的值重置为"-1".我无法使用tag属性执行此操作.(我搜索过,似乎我们没有像ListItem那样的值属性)每个主体似乎建议我将它与类绑定并设置DisplayMemberPath和SelectedValuePath.现在如果我直接绑定我的Location类,如何插入--Select One--项.我可以通过创建一个虚拟对象并在绑定之前将其插入我的列表中来实现.但这是在WPF中工作的最佳方式吗?也许我错过了一种完全不同的方法.请指教.
提前致谢.!
c# ×8
wpf ×4
.net ×2
listbox ×2
xaml ×2
.net-4.0 ×1
asp.net ×1
assemblies ×1
binding ×1
collections ×1
combobox ×1
datagrid ×1
friend ×1
generics ×1
itemssource ×1
listboxitem ×1
mvvm ×1
oop ×1
overloading ×1
params ×1
roslyn ×1
toolkit ×1
winforms ×1