小编Edw*_*uay的帖子

关闭有什么特别之处?

我一直在阅读这篇关于闭包的文章,他们说:

  • "所有的管道都是自动的"
  • 编译器"创建一个包装类"并"延长变量的生命周期"
  • "你可以毫无顾虑地使用局部变量"
  • .NET编译器为您处理管道等.

所以我根据他们的代码做了一个例子,对我而言,闭包似乎就像常规的命名方法一样,也"无需担心地处理局部变量",其中"所有的管道都是自动的".

或者这个"局部变量的包装"解决了什么问题,使得闭包如此特殊/有趣/有用?

using System;
namespace TestingLambda2872
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int> AddToIt = AddToItClosure();

            Console.WriteLine("the result is {0}", AddToIt(3)); //returns 30
            Console.ReadLine();
        }

        public static Func<int, int> AddToItClosure()
        {
            int a = 27;
            Func<int, int> func = s => s + a;
            return func;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

回答

所以这个问题的答案就是阅读Jon Skeet关于 Marc指出的闭包的文章.本文不仅展示了在C#中导致lambda表达式的演变,还展示了如何在Java中处理闭包,这是本主题的优秀读物.

c# closures

12
推荐指数
1
解决办法
746
查看次数

如何使用DelegateCommand将信息从View传递到ViewModel?

在我的视图中,我有一个按钮.

当用户单击此按钮时,我希望ViewModel在数据库中保存TextBlock的上下文.

<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
    <TextBlock Text="{Binding FirstName}"/>
    <TextBox Text="Save this text to the database."/>
    <Button Content="Save" Command="{Binding SaveCommand}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

但是,在我的ViewModel中的DelegateCommand中,"Save()"方法不传递任何参数,那么如何从视图中获取数据呢?

#region DelegateCommand: Save
private DelegateCommand saveCommand;

public ICommand SaveCommand
{
    get
    {
        if (saveCommand == null)
        {
            saveCommand = new DelegateCommand(Save, CanSave);
        }
        return saveCommand;
    }
}

private void Save()
{
    TextBox textBox = ......how do I get the value of the view's textbox from here?....
}

private bool CanSave()
{
    return true;
}
#endregion
Run Code Online (Sandbox Code Playgroud)

wpf mvvm delegatecommand

12
推荐指数
4
解决办法
4万
查看次数

寻找模块的Prism示例将自己加载到菜单中

有没有人知道使用Prism的WPF代码示例,其中每个模块在另一个模块的菜单中将自己注册为menuitem?

(我目前有一个应用程序尝试使用EventAggregator执行此操作,因此一个模块侦听来自其他模块的已发布事件,这些模块需要在菜单中将其标题作为菜单项,但我遇到了订单问题加载和线程等​​我想找到一个使用经典Prism结构来做这个的例子.)

我在考虑这个问题:

Shell.xaml:

<DockPanel>
    <TextBlock Text="Menu:" DockPanel.Dock="Top"/>
    <Menu 
        Name="MenuRegion" 
        cal:RegionManager.RegionName="MenuRegion" 
        DockPanel.Dock="Top"/>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

合同视图:

<UserControl x:Class="ContractModule.Views.AllContracts"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Contracts">
    </MenuItem>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

客户观点:

<UserControl x:Class="CustomerModule.Views.CustomerView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Customers">
    </MenuItem>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

但是要知道我已经完成了非Prism MVVM应用程序结构,并且Menus总是很好地绑定到ViewModel中的ObservableCollections,上面似乎打破了这个漂亮的模式.以上是在Prism中做到这一点的习惯方式吗?

wpf containers prism bootstrapper

12
推荐指数
1
解决办法
1万
查看次数

为非设计人员开发使用色彩设计工具的最佳策略是什么?

在网页设计过程中进行颜色选择时,我使用免费的在线工具,如ColorSchemer,其中我:

  • 点击一种颜色
  • 使用它为我建议的颜色

这使我的设计看起来比我从色轮中自由选择颜色更好.

但除了这种简单的方法之外,使用这些工具制作更专业的色彩决策的最佳策略是什么?

替代文字http://i35.tinypic.com/i4ia7n.jpg

html css layout colors

12
推荐指数
1
解决办法
314
查看次数

如何将System.Linq.Enumerable.WhereListIterator <int>转换为List <int>?

在下面的例子中,我如何轻松转换eventScoresList<int>可以将其用作参数prettyPrint

Console.WriteLine("Example of LINQ's Where:");
List<int> scores = new List<int> { 1,2,3,4,5,6,7,8 };
var evenScores = scores.Where(i => i % 2 == 0);

Action<List<int>, string> prettyPrint = (list, title) =>
    {
        Console.WriteLine("*** {0} ***", title);
        list.ForEach(i => Console.WriteLine(i));
    };

scores.ForEach(i => Console.WriteLine(i));
prettyPrint(scores, "The Scores:");
foreach (int score in evenScores) { Console.WriteLine(score); }
Run Code Online (Sandbox Code Playgroud)

c# linq action func

12
推荐指数
2
解决办法
3万
查看次数

为什么我不能将List <Customer>作为参数传递给接受List <object>的方法?

以下代码给出了这个错误:

无法从'System.Collections.Generic.List'转换为'System.Collections.Generic.List'.

如何向编译器指出Customer确实从对象继承?或者它只是不与泛型集合对象继承(发送List<string>获取相同的错误).

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace TestControl3423
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            List<Customer> customers = Customer.GetCustomers();
            FillSmartGrid(customers);

            //List<CorporateCustomer> corporateCustomers = CorporateCustomer.GetCorporateCustomers();
            //FillSmartGrid(corporateCustomers);
        }


        public void FillSmartGrid(List<object> items)
        {
            //do reflection on items and display dynamically
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { …
Run Code Online (Sandbox Code Playgroud)

c# generics

12
推荐指数
4
解决办法
2万
查看次数

如何在StackPanel内垂直对齐TextBox?

在下面的XAML中,单词"Test" 水平居中但不垂直居中.

如何让它垂直居中?

<Window x:Class="TestVerticalAlign2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
    Title="Window1" Height="768" Width="1024">
    <DockPanel LastChildFill="True">
        <Slider x:Name="TheSlider"
                DockPanel.Dock="Left"
                Orientation="Vertical"
                HorizontalAlignment="Center"
                HorizontalContentAlignment="Center"
                Minimum="0"
                Maximum="10"
                Cursor="Hand"
                Value="{Binding CurrentSliderValue}"
                IsDirectionReversed="True"
                IsSnapToTickEnabled="True"
                Margin="10 10 0 10"/>
        <Border DockPanel.Dock="Right" Background="Beige"
                Padding="10"
                Margin="10"
                CornerRadius="5">
            <StackPanel Height="700">
                <TextBlock
                    Text="Test"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontSize="200" x:Name="TheNumber"/>

            </StackPanel>
        </Border>
    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

xaml stackpanel

12
推荐指数
3
解决办法
3万
查看次数

如何使用Dictionary <string,object>属性序列化对象?

在下面的示例代码中,我收到此错误:

Element TestSerializeDictionary123.Customer.CustomProperties vom Typ System.Collections.Generic.Dictionary`2 [[System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,Version = 2.0 .0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]无法序列化,因为它实现了IDictionary.

当我取出Dictionary属性时,它工作正常.

如何使用字典属性序列化此Customer对象?或者我可以使用哪种替换类型的Dictionary可序列化?

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;

namespace TestSerializeDictionary123
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = Customer.GetCustomers();

            Console.WriteLine("--- Serializing ------------------");

            foreach (var customer in customers)
            {
                Console.WriteLine("Serializing " + customer.GetFullName() + "...");
                string xml = XmlHelpers.SerializeObject<Customer>(customer);
                Console.WriteLine(xml);
                Console.WriteLine("Deserializing ...");
                Customer customer2 = XmlHelpers.DeserializeObject<Customer>(xml); …
Run Code Online (Sandbox Code Playgroud)

c# serialization dictionary xml-serialization

12
推荐指数
2
解决办法
2万
查看次数

如何将MouseDown事件放入样式中?

这有效:

XAML:

<Window x:Class="Test239992.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>
        <TextBlock Tag="1" Text="Customers" MouseDown="Handle_Click"/>
        <TextBlock Tag="2" Text="Appointments" MouseDown="Handle_Click"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Test239992
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Handle_Click(object sender, MouseButtonEventArgs e)
        {
            int id = Int32.Parse(((TextBlock)sender).Tag.ToString());
            MessageBox.Show("you chose id " + id.ToString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我如何将MouseDown事件放在一个样式中,这给了我错误 "无法在类型'System.Windows.Controls.TextBlock'上找到样式属性'MouseDown'":

<Window x:Class="Test239992.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">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}" …
Run Code Online (Sandbox Code Playgroud)

c# wpf styles event-handling

12
推荐指数
1
解决办法
2万
查看次数

如何获取FlowDocument超链接以启动浏览器并转到WPF应用程序中的URL?

WPF应用程序中的以下代码创建一个超链接,其外观和行为类似于超链接,但在单击时不执行任何操作.

我需要更改什么,以便当我单击它时,它会打开默认浏览器并转到指定的URL?

替代文字http://www.deviantsart.com/upload/4fbnq2.png

XAML:

<Window x:Class="TestLink238492.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 Margin="10">
        <ContentControl x:Name="MainArea"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace TestLink238492
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();

            FlowDocument doc = new FlowDocument();
            fdsv.Document = doc;
            fdsv.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
            doc.PagePadding = new Thickness(0);
            Paragraph paragraph = new Paragraph();
            doc.Blocks.Add(paragraph);

            Run run = new Run("this is flow document …
Run Code Online (Sandbox Code Playgroud)

c# wpf hyperlink flowdocument

12
推荐指数
2
解决办法
7708
查看次数