小编And*_*rew的帖子

Wpf:拖放到文本框

我搜索了这个问题,人们已经回答了类似的问题,但由于某种原因,我无法得到任何工作.我一定在这里遗漏了一些东西......无论如何,当我运行以下代码时,永远不会调用TextBox_DragEnter处理程序.但是,如果我将xaml中的T​​extBox元素更改为TextBlock元素,则会调用它.有没有办法从TextBox元素获得相同的行为?以下代码完全隔离了问题......

MainWindow.xaml:

<Window x:Class="Wpf1.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">
    <Grid Name="myGrid">
        <TextBox AllowDrop="True" PreviewDragEnter="TextBox_DragEnter" PreviewDrop="TextBox_Drop" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace Wpf1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TextBox_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void TextBox_Drop(object sender, DragEventArgs e)
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提前谢谢了!

安德鲁

编辑:

为了澄清,我想允许将自定义对象放入文本框中.在文本框的Drop处理程序中,我想将文本框的文本设置为对象中的属性,然后将TextBox的IsReadOnly属性设置为false.我只是在为TextBox拖放时遇到一些麻烦......

wpf textbox drag-and-drop

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

难以将图像保存到MemoryStream

我在将一个字节流从图像(在本例中为jpg)保存到System.IO.MemoryStream对象时遇到了一些困难.目标是保存System.Drawing.Image到a MemoryStream,然后使用将MemoryStream图像写入字节数组(我最终需要将其插入数据库).但是,dataMemoryStream关闭后检查变量表明所有字节都是零...我很难过,不知道我在哪里做错了...

using (Image image = Image.FromFile(filename))
{
    byte[] data;

    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        data = new byte[m.Length];
        m.Write(data, 0, data.Length);
    }

    // Inspecting data here shows the array to be filled with zeros...
}
Run Code Online (Sandbox Code Playgroud)

任何见解将不胜感激!

c# image stream

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

整数包含使用Linq

我在编写linq查询时遇到一些困难,该查询将检查整数中的连续数字是否包含在表的主键中.因此,假设有一个Employees在列上使用主键调用的表Employees.Id.假设此主键是Sql Server数据类型INT.我想使用Entity Framework Code First编写一个linq查询,它将返回主键包含字符串456的所有员工.例如:

string filter = "456";

var results = from e in myDbContext.Employees
  where e.Id.Contains(filter)
  select e;
Run Code Online (Sandbox Code Playgroud)

问题是C#中的整数数据类型不提供Contains方法...

c# linq entity-framework casting

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

关于调试模式的MVC Web.Debug.Config问题

这可能是一个快速的问题.我是解决方案配置和web.config xml文件转换的新手.我想添加一个转换,将Asp.Net Mvc网站的编译元素的debug属性设置为true:

Web.Debug.config:

  <system.web>
    <compilation debug="true" xdt:Transform="SetAttributes(debug)" />
  </system.web>
Run Code Online (Sandbox Code Playgroud)

Web.config文件:

<compilation targetFramework="4.0">
  <assemblies>
         ...
  </assemblies>
</compilation>
Run Code Online (Sandbox Code Playgroud)

但是当我按F5时,Visual Studio中会弹出一个窗口,说"该页面无法在调试模式下运行,因为web.config文件中未启用调试." 然后,它为我提供了更改Web.config文件的选项.但我认为Web.Debug.config文件的目的是允许自动设置...在按F5后,我可以让Visual Studio使用转换的Web.config文件吗?

提前谢谢了!

安德鲁

asp.net web-config asp.net-mvc-2

9
推荐指数
2
解决办法
4948
查看次数

与ItemsControl的TwoWay绑定

我正在尝试编写一个具有ItemsControl的用户控件,其ItemsTemplate包含一个允许TwoWay绑定的TextBox.但是,我必须在我的代码中的某处犯一个错误,因为绑定似乎只是Mode = OneWay.这是我项目的一个非常简化的摘录,但它仍然包含问题:

<UserControl x:Class="ItemsControlTest.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <StackPanel>
            <ItemsControl ItemsSource="{Binding Path=.}"
                          x:Name="myItemsControl">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Mode=TwoWay,
                                                UpdateSourceTrigger=LostFocus,
                                                Path=.}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <Button Click="Button_Click"
                    Content="Click Here To Change Focus From ItemsControl" />
        </StackPanel>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

以下是上述控件的代码:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace ItemsControlTest
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public ObservableCollection<string> MyCollection
        {
            get { return (ObservableCollection<string>)GetValue(MyCollectionProperty); }
            set { SetValue(MyCollectionProperty, value); …
Run Code Online (Sandbox Code Playgroud)

wpf binding itemscontrol

6
推荐指数
1
解决办法
4793
查看次数

难以手动走原型链

我想尝试手动走几个对象的原型链,只是为了看看我在路上找到了什么.但是,我遇到了我尝试过的第一个问题.这是代码:

function MyObject() { }
var x = new MyObject();
console.log('--------------------------------------------');
console.log('x.constructor.name: ' + x.constructor.name);
console.log('x.constructor.prototype.constructor.name: ' + x.constructor.prototype.constructor.name);
console.log(x.constructor.prototype === Function.prototype ? 'Good guess.' : 'No, you are wrong.');
console.log(x.constructor === MyObject ? 'Good guess.' : 'No, you are wrong.');
console.log('--------------------------------------------');
Run Code Online (Sandbox Code Playgroud)

上面的代码会在Google Chrome的开发人员工具控制台中生成以下输出:

--------------------------------------------
x.constructor.name: MyObject
x.constructor.prototype.constructor.name: MyObject
No, you are wrong.
Good guess.
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)

x的构造函数是MyObject函数是有道理的,因为x是使用MyObject上的new关键字实例化的(这取决于构造函数的定义).因此,我理解输出的第一行(注意:我开始计算从零起的输出行).然而,第二行让我感到困惑.我想知道MyObject的原型是什么.显然,它不是Function类型的对象,如输出的第3行所示,它告诉我我错了.第四行输出只是加强了第一行的输出.

更一般地说,我假设走一个对象的原型链的正确方法是从有问题的对象转到它的构造函数,然后从构造函数到构造函数的原型,假设最后一个引用不是空值.我假设应该重复这个两步过程(包括转到构造函数,然后转到构造函数的原型),直到从构造函数到原型的null引用,从而表示原型链的结束.但是,这似乎不起作用,因为将此算法应用于上述场景只会导致循环引用.

总之,我有两个问题:

  1. 为什么x.constructor === x.constructor.prototype.constructor(换句话说,为什么循环引用),以及什么样的对象是x.constructor.prototype,无论如何(或者,换句话说,如何它是否实例化/它来自哪里)?
  2. 为了正确地遍历对象x的原型链,如何校正上述算法?

编辑

我在这里遇到了关于StackOverflow的类似问题,但它没有明确地询问走原型链的正确方法.它确实指出了循环引用,但......

javascript constructor prototype

6
推荐指数
1
解决办法
368
查看次数

CSS布局问题

我无法定义必要的CSS样式以实现以下布局:

在此输入图像描述

理想情况下,我希望左边两个div的宽度为200px. div#image总是有100px的高度.但是,我希望div#sidebar并且div#mainContent具有位于相同水平面上的较低边界.它们的大小应足够大,以包含各自的内容,这些内容在页面提供时确定.因此,具有更多内容的那个将导致另一个div向下延伸到相同的距离.

问题在于,通过绝对定位,元素div#sidebardiv#mainContent元素似乎并不承认其子元素的流动.也许我不完全理解绝对定位.此外,使用Javascript来设置页面上元素的内联样式似乎是不好的形式.有没有办法用CSS完成这个?

我也试过浮动div#imagediv#sidebar,并设置margin-left属性div#mainContent,但无法让它工作......

任何帮助都感激不尽!

安德鲁

html css

5
推荐指数
1
解决办法
185
查看次数

Visual Studio中的Linq查询格式

在Visual Studio 2010中键入以下内容后,

using(var myEntities = new MyEntities())
{
    IQueryable<Employee> employees =
        from    e in myEntities.Employees
        where   e.Name == name &&
                e.Password == hasher.ComputeHash(password)
        select  e;

    ...  Code left out for simplicity ...
Run Code Online (Sandbox Code Playgroud)

当我进入右大括号时遇到格式问题.具体来说,文本编辑器重新格式化我的linq查询中的空格,以便我最终得到,

using(var myEntities = new MyEntities())
{
    IQueryable<Employee> employees =
        from e in myEntities.Employees
        where e.Name == name &&
                e.Password == hasher.ComputeHash(password)
        select e;

    ...  Code left out for simplicity ...
}
Run Code Online (Sandbox Code Playgroud)

是否可以在Visual Studio的格式设置中更改某些内容以防止我的linq查询中的空格被自动删除?我已经尝试了谷歌搜索,我已经浏览了Visual Studio的工具 - >选项窗口,但无法找到任何内容.希望我只是忽视了一些事情......

提前谢谢了!

linq code-formatting visual-studio

5
推荐指数
1
解决办法
1301
查看次数

WPF:从Window继承

我似乎在创建派生自System.Windows.Window的自定义窗口类型时遇到了一些麻烦.似乎有两个问题正在发生.首先,有一个编译时错误说明

在'Control'类型上找不到静态成员'ContentProperty'

这是对自定义窗口的ControlTemplate中的ContentPresenter元素的引用(请参阅下面的BaseWindowResource.xaml的代码示例).我不知道为什么会这样,因为BaseWindow派生自Window,因此必须有一个Content属性......

第二个问题是,当从BaseWindow派生的Window1完成渲染时,我似乎无法触发BaseWindow的ContentRendered事件...我需要处理BaseWindow中的ContentRendered事件,因为处理程序将包含很多代码,否则需要复制到每个派生类...

无论如何,这是代码.任何帮助都感激不尽!

干杯,

安德鲁

App.xaml中:

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary Source="/BaseWindowResource.xaml" />
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

BaseWindowResource.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication4">
    <Style TargetType="{x:Type local:BaseWindow}" x:Key="BaseWindowStyleKey">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Rectangle Margin="20" Fill="Green" x:Name="MyRect" />
                        <ContentPresenter Margin="30" x:Name="MyContentPresenter"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

BaseWindow.cs:

    public class BaseWindow : Window
    {
        public BaseWindow()
        {
            Style = FindResource("BaseWindowStyleKey") as Style;

            ContentRendered += new EventHandler(BaseWindow_ContentRendered);
        }

        void BaseWindow_ContentRendered(object sender, EventArgs e)
        {
            ContentPresenter contentPresenter …
Run Code Online (Sandbox Code Playgroud)

wpf templates wpf-controls

4
推荐指数
1
解决办法
2186
查看次数

如何读取,然后写入C#中的嵌入式资源

每当我尝试执行以下代码时,我都会收到错误"Stream is not writable".我知道在内存中仍然有对流的引用,但我不知道如何解决这个问题.这两个代码块按顺序调用.我认为第二个可能是函数调用或者调用堆栈中的两个更深层次,但我认为这不重要,因为我在第一个块中有"using"语句应该自动清理流.我确信这是C#中的常见任务,我只是不知道如何做到这一点......

string s = "";

using (Stream manifestResourceStream =
    Assembly.GetExecutingAssembly().GetManifestResourceStream("Datafile.txt"))
{
    using (StreamReader sr = new StreamReader(manifestResourceStream))
    {
        s = sr.ReadToEnd();
    }
}
Run Code Online (Sandbox Code Playgroud)

...

string s2 = "some text";

using (Stream manifestResourceStream =
    Assembly.GetExecutingAssembly().GetManifestResourceStream("Datafile.txt"))
{
    using (StreamWriter sw = new StreamWriter(manifestResourceStream))
    {
        sw.Write(s2);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感谢.谢谢!

安德鲁

c# embedded-resource

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

证书,加密和身份验证

大多数情况下,我的混淆似乎是从我在WCF环境中理解安全性的尝试中解脱出来的.在WCF中,看起来证书可用于身份验证和加密.基本上,我试图理解:

  1. 如何将X509证书用作身份验证令牌?ssl证书通常不是公开的吗?这不会使它们无法用于身份验证吗?如果没有,是否有一些通常用于此目的的协议?
  2. 使用WCF加密消息时,使用的证书是仅发送给客户端,仅发送给服务器,还是两者都发布?如果使用来自客户端和服务器的证书,我有点不清楚为什么.这主要源于我对https的理解,在这种情况下,只有颁发给服务器的证书(并且链接到根CA颁发的某些证书​​)才有必要建立加密连接并验证服务器.

我不完全确定这是正确的论坛.我的问题源于试图理解WCF,但我想我想理解这背后的理论.如果这是一个好主意,请为我建议正确的论坛.如果有必要的话,我很乐意尝试迁移这个问题.

提前致谢!

https wcf ws-security certificate wcf-security

3
推荐指数
1
解决办法
1973
查看次数

RequireJS:丑化不起作用

我一定是在某个地方犯了一个错误,但是在优化过程中它没有被写入标准输出。我正在尝试通过 requirejs 优化文件,但输出没有被缩小。根据文档,UglifyJS 应该缩小代码。

无论如何,以下代码是微不足道的,但它隔离了问题。

源代码/索引.js:

require(['config'], function () {
    require(['myMod'], function (myMod) {
        console.log(myMod.x());
    })
})
Run Code Online (Sandbox Code Playgroud)

src/myMod.js:

define(function () {
    let myMod = {
        x: 5
    };

    return myMod;
})
Run Code Online (Sandbox Code Playgroud)

源代码/config.js:

define(function () {
    require.config({
        baseUrl: 'src'
    });
})
Run Code Online (Sandbox Code Playgroud)

这是执行优化的 gulp 任务:

gulp.task('optimize', function (cb) {
    let config = {
        appDir: 'src',
        dir: 'dist/src',
        generateSourceMaps: true,
        preserveLicenseComments: false,
        removeCombined: true,
        baseUrl: './',
        modules: [{
            name: 'index',
            include: ['myMod']
        }]
    }

    let success = function (buildResponse) { console.log(buildResponse); …
Run Code Online (Sandbox Code Playgroud)

requirejs gulp requirejs-optimizer

3
推荐指数
1
解决办法
2413
查看次数

Proxyquire:无法存根 fs.readFileSync

我似乎无法从 NodeJS 核心在 fs 上存根 readFileSync。以下代码隔离了该问题。通过 Mocha 运行测试结果如下:

> mocha tests/test.js

      Some description
        1) "before all" hook


      0 passing (15ms)
      1 failing

      1) Some description "before all" hook:
         TypeError: Cannot read property 'charCodeAt' of undefined
          at Object.stripBOM (internal/module.js:48:14)
          at Object.require.extensions.(anonymous function) (node_modules/proxyquire/lib/proxyquire.js:276:43)
          at Proxyquire._withoutCache (node_modules/proxyquire/lib/proxyquire.js:179:12)
          at Proxyquire.load (node_modules/proxyquire/lib/proxyquire.js:136:15)
          at Context.<anonymous> (tests/test.js:12:15)
Run Code Online (Sandbox Code Playgroud)

这是测试/test.js

var proxyquire = require('proxyquire'),
    sinon = require('sinon'),
    fs = require('fs'),
    sut;

describe('Some description', function () {
    var readFileSyncStub;

    before(function () {
        readFileSyncStub = sinon.stub(fs, 'readFileSync');
        readFileSyncStub.withArgs('someFile.js', { encoding: …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js sinon proxyquire

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