我搜索了这个问题,人们已经回答了类似的问题,但由于某种原因,我无法得到任何工作.我一定在这里遗漏了一些东西......无论如何,当我运行以下代码时,永远不会调用TextBox_DragEnter处理程序.但是,如果我将xaml中的TextBox元素更改为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拖放时遇到一些麻烦......
我在将一个字节流从图像(在本例中为jpg)保存到System.IO.MemoryStream对象时遇到了一些困难.目标是保存System.Drawing.Image到a MemoryStream,然后使用将MemoryStream图像写入字节数组(我最终需要将其插入数据库).但是,data在MemoryStream关闭后检查变量表明所有字节都是零...我很难过,不知道我在哪里做错了...
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)
任何见解将不胜感激!
我在编写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方法...
这可能是一个快速的问题.我是解决方案配置和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文件吗?
提前谢谢了!
安德鲁
我正在尝试编写一个具有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) 我想尝试手动走几个对象的原型链,只是为了看看我在路上找到了什么.但是,我遇到了我尝试过的第一个问题.这是代码:
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引用,从而表示原型链的结束.但是,这似乎不起作用,因为将此算法应用于上述场景只会导致循环引用.
总之,我有两个问题:
编辑
我在这里遇到了关于StackOverflow的类似问题,但它没有明确地询问走原型链的正确方法.它确实指出了循环引用,但......
我无法定义必要的CSS样式以实现以下布局:

理想情况下,我希望左边两个div的宽度为200px. div#image总是有100px的高度.但是,我希望div#sidebar并且div#mainContent具有位于相同水平面上的较低边界.它们的大小应足够大,以包含各自的内容,这些内容在页面提供时确定.因此,具有更多内容的那个将导致另一个div向下延伸到相同的距离.
问题在于,通过绝对定位,元素div#sidebar和div#mainContent元素似乎并不承认其子元素的流动.也许我不完全理解绝对定位.此外,使用Javascript来设置页面上元素的内联样式似乎是不好的形式.有没有办法用CSS完成这个?
我也试过浮动div#image和div#sidebar,并设置margin-left属性div#mainContent,但无法让它工作......
任何帮助都感激不尽!
安德鲁
在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的工具 - >选项窗口,但无法找到任何内容.希望我只是忽视了一些事情......
提前谢谢了!
我似乎在创建派生自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) 每当我尝试执行以下代码时,我都会收到错误"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)
任何帮助将非常感谢.谢谢!
安德鲁
大多数情况下,我的混淆似乎是从我在WCF环境中理解安全性的尝试中解脱出来的.在WCF中,看起来证书可用于身份验证和加密.基本上,我试图理解:
我不完全确定这是正确的论坛.我的问题源于试图理解WCF,但我想我想理解这背后的理论.如果这是一个好主意,请为我建议正确的论坛.如果有必要的话,我很乐意尝试迁移这个问题.
提前致谢!
我一定是在某个地方犯了一个错误,但是在优化过程中它没有被写入标准输出。我正在尝试通过 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) 我似乎无法从 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) c# ×3
wpf ×3
linq ×2
asp.net ×1
binding ×1
casting ×1
certificate ×1
constructor ×1
css ×1
gulp ×1
html ×1
https ×1
image ×1
itemscontrol ×1
javascript ×1
node.js ×1
prototype ×1
proxyquire ×1
requirejs ×1
sinon ×1
stream ×1
templates ×1
textbox ×1
unit-testing ×1
wcf ×1
wcf-security ×1
web-config ×1
wpf-controls ×1
ws-security ×1