我通常List<T>用于收藏.但是,如果我需要对集合进行快速查找,那么例如在下面的示例中,我将使用一个字典,以便我可以通过id以下方式快速查找:
Dictionary<int, Customer>
Run Code Online (Sandbox Code Playgroud)
但是既然我可以使用LINQ来查询List<T>,如下所示,是否有任何理由经历使用Dictionary而不是List的麻烦?字典更快还是LINQ在幕后做一些让它同样快的东西?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>()
{
new Customer { Id = 234, FirstName = "Jim", LastName = "Smith" },
new Customer { Id = 345, FirstName = "John", LastName = "Thomas" },
new Customer { Id = 654, FirstName = "Rick", LastName = "Ashton" },
new Customer { Id = …Run Code Online (Sandbox Code Playgroud) 我可以使用XDocument来构建以下文件,该文件正常工作:
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null),
new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
),
new XElement(_singularCamelNotation,
new XElement("id", "2"),
new XElement("whenCreated", "2008-12-31")
)
)
);
Run Code Online (Sandbox Code Playgroud)
但是,我需要通过迭代这样的集合来构建XML文件:
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null));
foreach (DataType dataType in _dataTypes)
{
XElement xelement = new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
));
xdoc.AddInterally(xelement); //PSEUDO-CODE
}
Run Code Online (Sandbox Code Playgroud)
有Add,AddFirst,AddAfterSelf,AddBeforeSelf …
以下代码将两个文本元素放在顶部,即使第二个文本元素标记为"Bottom".背景颜色一直到底部,所以DockPanel似乎伸展到底部.
我对DockPanel的理解是什么?
<Window x:Class="TestIndexer934.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestIndexer934.Commands"
Title="Main Window" Height="400" Width="800">
<DockPanel HorizontalAlignment="Left" Background="Beige">
<TextBlock DockPanel.Dock="Top" Text="Testing top"/>
<TextBlock DockPanel.Dock="Bottom" Text="Testing bottom"/>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud) 我有一个工具,可以动态 生成 .xaml和.xaml.cs文件,并将它们放在适当的Visual Studio 目录中.
要将它们添加到项目中,我必须:
有没有办法让我告诉项目"包括硬盘上项目文件夹下的所有现有项目"?
我需要使XAML区域可打印,因此制作此按钮处理程序:
private void Button_Click_Print(object sender, RoutedEventArgs e)
{
Customer.PrintReport(PrintableArea);
}
Run Code Online (Sandbox Code Playgroud)
在PrintReport中,我将frameworkelement打包到其他元素中,以便以与屏幕上不同的方式打印它,如下所示:
public void PrintReport(FrameworkElement fwe)
{
StackPanel sp = new StackPanel();
sp.Children.Add(fwe);
TextBlock tb = new TextBlock();
tb.Text = "hello";
sp.Children.Add(tb);
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == true)
{
dialog.PrintVisual(sp, "Print job");
}
}
Run Code Online (Sandbox Code Playgroud)
但上面给出了以下错误:
指定的元素已经是另一个元素的逻辑子元素.首先断开它.
有没有一种简单的方法来克隆FrameworkElement,以便我可以操作副本,打印它,然后忘记它,让XAML中的原始元素显示在屏幕上完好无损?
我想象的是这样的东西:
FrameworkElement fwe2 = FrameworkElement.Clone(fwe); //pseudo-code
Run Code Online (Sandbox Code Playgroud) 类似于我从使用声明中返回的问题(其答案通常是 " 是的,没关系 ")我想知道从foreach语句中返回是否同样没有副作用并被认为是公认的练习,或者当我做这就是我把一个指针挂在中间某个内部的枚举,等等.
这是一个例子:
public string GetCurrentTransaction(string idText)
{
foreach (var transaction in transactions)
{
if (idText.IsEquivalentTo(transaction.IdText))
{
return transaction.Content;
}
}
return "";
}
Run Code Online (Sandbox Code Playgroud) 我安装了Windows 7.
然后,我从此处使用Web Platform Installer安装了Web Developer 2010 Express .
然后我从这里安装了Silverlight 4 Toolkit 的4月15日版本.
然后我添加了这个参考:
替代文字http://www.deviantsart.com/upload/ijk0lm.png
然后在我的XAML中,我像这样引用它,但它没有给我智能感知并且告诉我我缺少一个程序集引用:
替代文字http://www.deviantsart.com/upload/cd4vrj.png
更新:
xmlns:tk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Run Code Online (Sandbox Code Playgroud)
即使在重建之后也不起作用
在Web Developer 2010 Express中使用Silverlight 4 Toolkit需要做什么?
所以我在另一台计算机上用Web Developer Express 2008中的Silverlight 3再次测试它,它确实按照我记忆的方式工作,所以为什么它不能在Silverlight 4中以这种方式工作,我需要更改才能获得它上班?:
wrappanel visual-studio-2010 silverlight-toolkit dockpanel silverlight-4.0
我在网格面板中有一个tbar,如下所示:

这是产生它的代码:
var grid = new Ext.grid.GridPanel({
region: 'center',
style: 'margin: 10px',
store: new Ext.data.Store({
data: myData,
reader: myReader
}),
title: 'Testing',
tbar: ['Filters:', {
width: 100,
xtype: 'combo',
mode: 'local',
value: 'en',
triggerAction: 'all',
forceSelection: true,
editable: false,
fieldLabel: 'Produkt',
name: 'language',
hiddenName: 'language',
displayField: 'name',
valueField: 'value',
store: new Ext.data.JsonStore({
fields : ['name', 'value'],
data : [
{name : 'German', value: 'de'},
{name : 'English', value: 'en'},
{name : 'French', value: 'fr'}
]
})
}],
Run Code Online (Sandbox Code Playgroud)
我需要更改什么才能使保管箱右对齐而不是左对齐?
感谢@dogbane,这完美地运作,这是我如何正确对齐文本和下拉列表: …
在630 x 400窗口中,我正在加载XAML元素:
问题是,当我设置UserControl的背景时,颜色只会下降到内容.我希望UserControl的背景当然涵盖整个UserControl.我试过了:
但颜色仍然拒绝下降.我不想设置固定宽度,因为用户可以增加应用程序的大小.
如何获取UserControl的背景颜色以填充UserControl的整个区域而不仅仅是其内容的区域?
PageItemOptionsView.xaml:
<UserControl x:Class="TestMenu234.Views.PageItemOptionsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
VerticalContentAlignment="Stretch"
VerticalAlignment="Stretch"
Background="#ddd">
<StackPanel Margin="10">
<TextBlock Text="This is the options area."/>
<Button Content="Click to go to the Manage Customers page."
Width="200"/>
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
MainView.xaml:
<Window x:Class="TestMenu234.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestMenu234.Commands"
xmlns:vm="clr-namespace:TestMenu234.ViewModels"
xmlns:v="clr-namespace:TestMenu234.Views"
Title="Main Window" Height="400" Width="630" MinWidth="630">
Run Code Online (Sandbox Code Playgroud)
...
<DockPanel LastChildFill="False">
<Menu DockPanel.Dock="Top">
<MenuItem
Header="Pages" ItemsSource="{Binding AllPageItemViewModels}"
ItemTemplate="{StaticResource CodeGenerationMenuTemplate}"/> …Run Code Online (Sandbox Code Playgroud) 我有一个27 x 27像素的图像,我在WPF中显示,但它显示大于窗口的大小.
如何让它显示其实际尺寸?
替代文字http://www.deviantsart.com/upload/m20dk6.png
XAML:
<Window x:Class="TestImage23434.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 x:Name="MainStackPanel"/>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System;
namespace TestImage23434
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TextBlock tb = new TextBlock();
tb.Text = "above image ";
MainStackPanel.Children.Add(tb);
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"C:\test\circle.png"));
img.HorizontalAlignment = HorizontalAlignment.Left;
img.VerticalAlignment = VerticalAlignment.Top;
MainStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
MainStackPanel.VerticalAlignment = VerticalAlignment.Top;
MainStackPanel.Children.Add(img);
TextBlock tb2 …Run Code Online (Sandbox Code Playgroud)