小编Dea*_*bit的帖子

迭代索引属性(反射)

我想对我只能通过反射访问的索引属性进行迭代,

但是(并且我完全知道这可能是一个令人尴尬的简单回答,MSDN /谷歌失败= /)我找不到/想到除了递增计数器PropertyInfo.GetValue(prop, counter)直到TargetInvocationException被抛出的方式.

翼:

foreach ( PropertyInfo prop in obj.GetType().GetProperties() )
{
    if ( prop.GetIndexParameters().Length > 0 )
    {
        // get an integer count value, by incrementing a counter until the exception is thrown
        int count = 0;
        while ( true )
        {
            try
            {
                prop.GetValue( obj, new object[] { count } );
                count++;
            }
            catch ( TargetInvocationException ) { break; }
        }

        for ( int i = 0; i < count; i++ ) …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

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

为什么人们不将DependencyProperties包装在泛型类中?

我不喜欢详细的dp,因为大多数代码都是重复的,我只是把它包装在一个泛型类中.

看过相当多的示例代码之后,我想知道为什么更多的人不会这样做.

我在演示应用程序中没有遇到任何问题,它使ViewModel更易于管理.

样品:

class GenericDependancyProperty<T> : DependencyObject
{
    // Value dependency property
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register( "Value", typeof( T ), typeof( GenericDependancyProperty ),
            new FrameworkPropertyMetadata( (T)default(T),
                new PropertyChangedCallback( OnValueChanged ) ) );

    // getter/setter for the Value dependancy property
    public T Value
    {
        get { return (T)GetValue( ValueProperty ); }
        set { SetValue( ValueProperty, value ); }
    }

    // Handles changes to the Value property.       
    private static void OnValueChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
        GenericDependancyProperty<T> …
Run Code Online (Sandbox Code Playgroud)

c# generics wpf dependency-properties

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

XAML 自动类型转换

我注意到可以从值转换器返回“Visible”、“Collapsed”、“Images/xyz.png”或“#FFB3D1”等字符串,并且绑定的魔力设法解决它。隐藏/显示 UI 元素,找到 xyz.png 图像或将某些东西涂成粉红色...

我很长时间以来都认为这是理所当然的,现在它不适用于我的最新代码,所以我的问题是如何手动调用此功能?


解释:

我通过创建一个自定义 MarkupExtension 进行了扩展,该扩展将 MultiConverter 连接到 MultiBinding 并返回初始化的绑定。然而,当这个多重转换器返回“#FFB3D1”或“Red”等字符串时,似乎什么也没有发生。

// PseudoCode from my MarkupExtension, setting up & returning the binding/multi-converter
public override object ProvideValue( IServiceProvider serviceProvider )
{
    MultiBinding outputBinding = new MultiBinding();    
    foreach ( Binding b in bindings )
    {
        outputBinding.Bindings.Add( b );
    }    

    outputBinding.Converter = converter;
    return outputBinding.ProvideValue( serviceProvider );
}
Run Code Online (Sandbox Code Playgroud)

我认为因为我正在代码中创建多重绑定+转换器,所以它跳过了命名空间中某处的步骤Binding.Magic


解决方案:

    public override object ProvideValue( IServiceProvider serviceProvider )
    {
        // get targets
        IProvideValueTarget serv = (IProvideValueTarget)serviceProvider.GetService( typeof( IProvideValueTarget ) …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf xaml

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

$ .Ajax没有"_"查询字符串参数

我正在使用$ .ajax方法动态地包含插件脚本文件,遗憾的是,由于整个项目都托管在Dynamics CRM 2011中,因此没有查询字符串参数可以传递给此请求而不会以错误的方式摩擦CRM.

执行以下操作:

$.ajax({
    url: includeUrl, // == "Templates.js"
    dataType: "script",
    success: function (includedFile) {
        window.Includes.push(includedFile);
    }
});
Run Code Online (Sandbox Code Playgroud)

将返回500 - Internal Server Error查看我已请求以下URL的firebug控制台:

http://server:5555/Organisation/WebResources/grid_/Templates.js?_=1366828753001
Run Code Online (Sandbox Code Playgroud)

已经_=1366828753001通过$ .Ajax方法附加了这个参数.. CRM不太喜欢这个...我问这知道我可能介于摇滚和硬地之间但有没有办法调用$ .ajax强制它不会将此ID附加到查询字符串中?

CRM完全错误:

<description>CRM Parameter Filter - Invalid parameter '_=1366828753001' in Request.QueryString on page /Organisation/Handlers/WebResource.ashx
The raw request was 'GET /Organisation/WebResources/grid_/Templates.js?_=1366828753001' called from http://server:5555/Organisation/WebResources/grid_/EditableGrid.htm.</description>
Run Code Online (Sandbox Code Playgroud)

ajax jquery dynamics-crm-2011

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

绑定到Children.Count

我正在使用表单的绑定

{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=Rows}
Run Code Online (Sandbox Code Playgroud)

尽管在xaml中添加了子节点,但当我在转换器中中断时,值始终为0.

我假设正在进行的是在调用此绑定之后才会添加子项.

我也假设绑定在被调用一次后被破坏,因为.Count是一个只读属性(我之前有一个类似的问题,我必须在属性中添加一个空的setter来维护绑定并愚弄WPF)因此添加子项后绑定不更新.

但是,我坚持认为你想出一个问题的解决方案,让它工作...... = /

<UniformGrid x:Name="MyUniformGrid"
     Rows="{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=R}"
     Columns="{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=C}">
    <Button Content="Hello, World!" />
    <Button Content="Hello, World!" />
    <Button Content="Hello, World!" />
    <Button Content="Hello, World!" />
    <Button Content="Hello, World!" />
    <Button Content="Hello, World!" />
   </UniformGrid>
Run Code Online (Sandbox Code Playgroud)

谢谢,拉比特

c# wpf xaml binding

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

OPTIONS 预检请求未到达 IIS 托管服务

我有一个本地主机网站和一个 IIS(7.5) 托管的 WCF 服务,它是这样实现的,并附加了一个 Visual Studio 调试器。每当我向我的服务发出 CORS 请求时,我都会收到以下 404 响应以及标准的 ASP.Net 错误页面:

OPTIONS http://192.168.200.44/api/values HTTP/1.1
Host: 192.168.200.44
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Origin: http://localhost:51946
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Response >>
HTTP/1.1 404 Not Found
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 26 Sep 2013 09:38:49 GMT
Content-Length: 1245
Run Code Online (Sandbox Code Playgroud)

Originally I was receiving erroneous 200 messages which …

iis wcf iis-7 cors http-status-code-404

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

$ .ajax的onreadystatechange事件

我想使用onreadystatechange来自XMLHttpRequestJQuery(2.0.2)的(底层)的事件$.ajax(...)来触发同步ajax请求,这样我就可以向最终用户显示长时间运行请求的准确状态指示.但似乎这个功能已从最新版本的JQuery中删除(参见此处),使具有异步Web请求的微调器成为向用户表示活动的唯一选择.

使用XMLHttpRequest我会做类似以下的事情(虽然我仍然不想使用JQuery),JQuery中是否还有一种方法可以访问readystate更改功能?是否有一个暴露onreadystatechange事件的插件?

function pad(width, string, padding) { // from stackoverflow.com/a/15660515/424963 
  return (width <= string.length) ? string : pad(width, string + padding, padding)
}

$(function(){
    $("<div>Loading</div>").appendTo($("body"));
    var anticache = "?anticache=" + new Date().getTime();

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4){
            $("div").html("Done");
        }
        else  {
            $("div").html("Loading" + pad(xhr.readyState, "", "."));
        }
    };

    xhr.open("POST", "jsfiddle.net" + anticache, true );
    xhr.send();    
});
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

javascript ajax jquery xmlhttprequest

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