小编Mar*_*ijn的帖子

C#使用new []

我有一个关于使用的问题new[].

想象一下:

Object.SomeProperty = new[] {"string1", "string2"};
Run Code Online (Sandbox Code Playgroud)

SomeProperty需要一个字符串数组.

我知道这段代码会起作用.但我想知道它在幕后做了什么.是否new[]创建了类的实例,object并在SomeProperty其中将其自动转换为string对象?

谢谢

c# new-operator

7
推荐指数
2
解决办法
2327
查看次数

如何防止在Angular指令之间共享范围?

我的所有指令都使用相同的范围,我希望我的指令可以自己操作.

指示:

app.directive('headerSort', function () {
    return {
        restrict: 'A',
        controller: function ($scope, $element, $attrs) {
            $scope.caption = $attrs.caption;

            $scope.doSort = function () {
                $scope.orderField = $attrs.headerSort;
                $scope.reverse = !$scope.reverse;
            };
        },
        template: '<div data-ng-click="doSort();">' +
                    '{{caption}}' +
                    '<i class="icon-sort"></i>' +
                  '</div>'
    };
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<th data-header-Sort="FullName" data-caption="Full name"></th>
<th data-header-Sort="FirsName" data-caption="First name"></th>
<th data-header-Sort="Age" data-caption="Age"></th>
Run Code Online (Sandbox Code Playgroud)

结果是所有列都具有值'Age'并按Age排序.我当然希望每列都对它自己的列进行排序.我怎样才能做到这一点?

更新:忘了提及,orderFieldreverse用于ng-repeat | orderBy:

<tbody id="customerRows" data-ng-repeat="customer in customers | orderBy:orderField:reverse">
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive angularjs-scope

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

为什么当从另一个对象复制对象时,Angular不会数据绑定数据?

我创建了一个简单的jsfiddle来说明我的问题:

小提琴

HTML:

<div ng-controller="MyCtrl">   
    <div ng-repeat="p in products">
        <span ng-click="overwrite(p)">{{ p.id }}: {{ p.name }}</span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

    var products = [{id:1, name:'first'}, {id:2, name:'second'}];
    $scope.products = products;

    var prod = {id: 3, name:'third'};

    $scope.overwrite = function(p){
        p.id = 4;
        p.name = 'forth';

        p = prod; // this doesn't work nor does angular.copy(prod)
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,当我手动设置属性时,值是bind.但是当我覆盖一个对象时,没有任何反应.这怎么可能?当我想要恢复其原始状态的对象时,我该怎么办?

想象一下,我使用创建备份对象var productBackup = angular.copy(product).然后我对原始产品进行了更改,之后我决定取消更改.我想用这个来做product = productBackup.但这不起作用!在这种情况下,我是否需要像这样手动设置所有属性?

product.id = productBackup.id;
product.name = productBackup.name; …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs

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

使用Angular上传文件时,HttpPostedFileBase为null

我将Angular与MVC结合使用.当我想上传文件时,HttpPostedFileBase为空.

HTML:

<input type="file" data-ng-model="fileName" onchange="angular.element(this).scope().fileInputChanged(this)">
Run Code Online (Sandbox Code Playgroud)

角度:

scope.fileInputChanged = function (element) {
    scope.$apply(function (scope) {
        console.log('files:', element.files);

        _.each(element.files, function (element, index, list) {
            scope.files.push(element);
        });
    });
}

scope.uploadDocuments = function () {
    var formData = new FormData();

    // add uploaded files to form data object
    for(var i in scope.files){
        formData.append("uploadedFile", scope.files[i]);
    }

    // create xml http request object
    var httpRequest = new XMLHttpRequest();
    httpRequest.upload.addEventListener("progress", uploadProgress, false);
    httpRequest.addEventListener("load", uploadComplete, false);
    httpRequest.addEventListener("error", uploadFailed, false);
    httpRequest.addEventListener("abort", uploadCanceled, false);
    httpRequest.open("POST", "/Customer/UploadDocuments");

    // …
Run Code Online (Sandbox Code Playgroud)

javascript c# asp.net-mvc angularjs

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

C#在对象中加载初始数据的位置?

与此问题类似: C#Constructor Design但这个问题略有不同.

我有一个Customer类和一个CustomerManager类.当创建CustomerManager类的实例时,我想加载所有客户.这就是我陷入困境的地方.我可以通过以下几种方式做到:

  1. 在构造函数中加载所有客户(我不喜欢这个,因为如果我有很多客户可能需要一段时间)
  2. 在执行数据库相关操作的CustomerManager类的每个方法中,检查加载的本地客户列表,如果没有,请加载列表:

    public method FindCustomer(int id)
    {
      if(_customers == null)
      // some code which will load the customers list
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建一个加载所有客户的方法.必须在调用执行数据库相关操作的方法之前调用此方法:

    在课堂里:

    public LoadData()
    {
       // some code which will load the customers list
    }
    
    Run Code Online (Sandbox Code Playgroud)

    形式如下:

    CustomerManager manager = new CustomerManager();
    manager.LoadData(); 
    Customer customer = manager.FindCustomer(int id);
    
    Run Code Online (Sandbox Code Playgroud)

做这个的最好方式是什么?

编辑:

我觉得我在这里被误解了.也许是因为我不够清楚.在CustomerManager类中,我有几种方法取决于本地列表(_customers).所以,我的问题是,我应该在哪里填写该清单?

c# winforms

6
推荐指数
2
解决办法
729
查看次数

C#如何用正则表达式替换重音不敏感的字符串?

我想在字符串中执行不区分重音的替换.我希望'客户'匹配'cliënt',反之亦然.

我的代码看起来像这样:

Regex reg = new Regex("client");
string result = reg.Replace("here goes the content with client and cliënt", "replacementWith");
Run Code Online (Sandbox Code Playgroud)

那么,我如何确保'客户'匹配'客户'和'cliënt',反之亦然?

c# regex

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

如何创建时间轴控件?

我想创建(或下载,但我找不到任何)时间轴控件.我的想法是这样的:

截图

在这张图片中,我只对时间表感兴趣,左边的资源和水平的时间.

有谁能告诉我开发这种控制的好方法是什么?我已经尝试了tablelayout控件,但我现在卡住了,因为我无法计划需要2分钟的事情.由于性能的原因,我也不想为每一分钟绘制一个列,它看起来太忙了.

c# winforms

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

如何防止工具提示在自定义控件中闪烁?

我做了一个自定义控件,当条件满足时,我想显示一个工具提示:

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);

    var plannedItem = GetPlannedItemByPosition(e.Location);

    if (plannedItem != null)
        _tooltip.SetToolTip(this, plannedItem.Description);
    else
        _tooltip.RemoveAll();
}
Run Code Online (Sandbox Code Playgroud)

此代码工作正常,除了工具提示闪烁的面孔.

这个自定义控件,描绘了OnPaint事件中的所有信息,也许这与它有关?如果是这样,我怎样才能防止工具提示闪烁?

c# winforms

6
推荐指数
3
解决办法
8257
查看次数

哪些文件存储在Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0中?

我想知道存储哪种文件

C:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework.NETFramework\v4.0 \

这些文件是否适用于WPF/Silverlight?

哪个安装程序将文件安装在该文件夹中?

我想知道这一点,因为我们开发了一个带有自定义WPF控件的winforms应用程序.当我们在裸系统上部署应用程序时,应用程序在使用这些控件时崩溃...

c# assemblies .net-4.0 winforms

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

Angular中的根元素是什么?

我写了这样一个指令:

app.directive('headersort', function () {
    return {
        restrict: 'E',
        scope: {
            sortBy: '=',
            title: '='
        },
        template: '<th>{{ title }}</th>',
        replace: true,
        link: function (scope, element, attributes) { 
            scope.sortBy = attributes.sortBy;
            scope.title = attributes.title;

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

我这样使用它:

<headersort sortBy="Name" title="Product">
Run Code Online (Sandbox Code Playgroud)

我想要的是<headersort sortBy="Name" title="Product">被替换为<th>Product</th>.但我得到一个错误说:

Template must have exactly one root element. was: <th>{{ title }}</th>

但我确实有一个根元素,对吗?我的根元素是<th>,为什么角度抛出这个错误?根元素的条件/定义是什么?

javascript angularjs angularjs-directive

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