小编dca*_*tro的帖子

以编程方式绑定ItemsSource

c#代码中的等价物是什么?

<ListView
    x:Name="taskItemListView"
    DataContext="{Binding SelectedItem, ElementName=itemListView}"
    ItemsSource="{Binding taskItems}">
...
</ListView>
Run Code Online (Sandbox Code Playgroud)

我尝试了以下代码,但它似乎不起作用......

Binding b = new Binding();
b.Path = new PropertyPath("taskItems");

DependencyProperty dp = DependencyProperty.Register("itemsSource", typeof(object), typeof(object), null);
BindingOperations.SetBinding(taskItemListView, dp, b);
Run Code Online (Sandbox Code Playgroud)

编辑:

基于@ sa_ddam213的答案,这有效:

Binding dataContextBinding = new Binding();
dataContextBinding.Path = new PropertyPath("SelectedItem");
dataContextBinding.Source = itemListView;
BindingOperations.SetBinding(taskItemListView, ListView.DataContextProperty, dataContextBinding );

Binding sourceBinding = new Binding();
sourceBinding.Path = new PropertyPath("taskItems");
BindingOperations.SetBinding(taskItemListView, ListView.ItemsSourceProperty, sourceBinding );
Run Code Online (Sandbox Code Playgroud)

c# xaml binding windows-runtime

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

是否有Lazy <T>的无效版本?

我想使用Lazy<T>我的类中的行为来确保某个操作只发生一次.我之前做过这个,通过一个Lazy本质上调用一个Func做某事然后返回一个空值.我不关心返回值,我只是想确保事情只发生一次.

我在想它可以被称为Once.我会自己构建它并在Lazy内部使用它,如果它还不存在,但我想在做之前先问一下.谢谢!

.net lazy-initialization

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

使用函数作为applicative functor/cartesians

我正在使用catslib.

使用他们的applicative functor实例(或者Cartesian,确切地说)组合两个列表很容易:

import cats._
import cats.implicits._

(List(23, 4), List(55, 56)).mapN(_ + _)
>> List(78, 79, 59, 60)
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法用两个函数做同样的事情:

val strLength: String => Int = _.length

(strLength, strLength).mapN(_ + _)
>> value mapN is not a member of (String => Int, String => Int)
Run Code Online (Sandbox Code Playgroud)

如果我明确地执行一些隐式转换,并且如果我创建一个类型别名以给编译器一个手,它确实有效:

type F[A] = Function1[String, A]
val doubleStrLength = catsSyntaxTuple2Cartesian[F, Int, Int]((strLength, strLength)).mapN(_ + _)

doubleStrLength("hello")
>> 10
Run Code Online (Sandbox Code Playgroud)

有更简单的方法吗?似乎过于冗长

编辑:如果你想玩它,我在这里创建一个工作表:https://scastie.scala-lang.org/dcastro/QhnD8gwEQEyfnr14g34d9g/2

scala scala-cats

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

StrikeThrough对TextBlock的影响

在Windows应用商店应用中,有没有办法对TextBlock的内容应用StrikeThrough效果?如果没有,RichTextBlock或任何类似的控件都可以.它是通过XAML还是通过编程方式(C#)无关紧要,但我更喜欢通过XAML,因此它会在设计器中显示.

我在微软的文档中找到了这个,但我不知道如何使用它:http: //msdn.microsoft.com/en-us/library/windows/apps/windows.ui.text.itextcharacterformat.strikethrough.aspx

.net c# xaml microsoft-metro windows-store-apps

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

锁定与比较和交换

我一直在阅读无锁技术,比如比较和交换,并利用Interlocked和SpinWait类来实现线程同步而不需要锁定.

我已经运行了一些我自己的测试,我只是有很多线程尝试将字符附加到字符串.我尝试使用常规locks和比较和交换.令人惊讶的是(至少对我而言),锁比使用CAS显示出更好的结果.

这是我的代码的CAS版本(基于).它遵循copy-> modify-> swap模式:

    private string _str = "";
    public void Append(char value)
    {
        var spin = new SpinWait();
        while (true)
        {
            var original = Interlocked.CompareExchange(ref _str, null, null);

            var newString = original + value;                
            if (Interlocked.CompareExchange(ref _str, newString, original) == original)
                break;
            spin.SpinOnce();
        }
    }
Run Code Online (Sandbox Code Playgroud)

更简单(也更有效)的锁定版本:

    private object lk = new object();
    public void AppendLock(char value)
    {
        lock (lk)
        {
            _str += value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我尝试添加50.000个字符,则CAS版本需要1.2秒,锁定版本需要700毫秒(平均值).对于100k字符,它们分别需要7秒和3.8秒.这是在四核(i5 2500k)上运行的.

我怀疑CAS显示这些结果的原因是因为它在最后一次"交换"步骤失败了.我是正确的.当我尝试添加50k字符(50k成功交换)时,我能够计算出70k(最佳情况)和近200k(最差情况)失败尝试之间的数量.最糟糕的情况是,每5次尝试中有4次失败.

所以我的问题是:

  1. 我错过了什么?CAS不应该给出更好的结果吗?好处在哪里?
  2. 究竟为什么CAS何时才是更好的选择?(我知道有人问过,但我找不到任何令人满意的答案,这也解释了我的具体情况).

我的理解是,采用CAS的解决方案尽管难以编码,但随着争用的增加,其规模要好得多,并且比锁具更好.在我的例子中,操作非常小且频繁,这意味着高争用和高频率.那么为什么我的测试显示不然呢?

我认为较长的操作会使情况更糟 - …

.net c# multithreading locking compare-and-swap

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

两个不同的应用程序可以同时使用同一台摄像机吗?

我希望能够使用摄像机的视频流(甚至每秒几帧),而另一个应用程序也在使用摄像机。这可能吗?

android

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

初始化DateTime时的异常

DateTime在控制器动作的参数中初始化变量,如下所示

public ActionResult ForFahrzeug(DateTime initDat = default(DateTime), long id = 0, long   
                             days = 0, string ExpGnr = "")
    {
        //body
    }
Run Code Online (Sandbox Code Playgroud)

编译时没有错误,但在运行时发生异常.

Server Error in '/' Application.
Encountered an invalid type for a default value.
Description: An unhandled exception occurred during the execution of the current web   
   request. Please review the stack trace for more information about the error and where    it originated in the code.

Exception Details: System.FormatException: Encountered an invalid type for a default value. …
Run Code Online (Sandbox Code Playgroud)

c# datetime asp.net-mvc-4

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

将字符串拆分为List <int> ignore none int values

我使用以下代码将字符串拆分为List <int>,但有时字符串包含非整数值,这些值的处理方式不同.

示例字符串可能类似于:1,2,3,4,x

代码看起来像:

List<int> arrCMs = new List<int>(strMyList.Split(',').Select(x => int.Parse(x)));
Run Code Online (Sandbox Code Playgroud)

问题是,一旦它命中'x'就会抛出错误,因为'x'不能被解析为整数.

如何使其忽略非整数值?我确信我应该能够用int.TryParse做一些事情,但不能完全理解它.

谢谢

c# string asp.net-mvc

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

如何检查接口的MethodInfo是否是"新"方法

我正在尝试使用反射来检索接口及其基接口的所有方法的列表.

到目前为止我有这个:

var methods = type.GetMethods().Concat(
                type.GetInterfaces()
                    .SelectMany(@interface => @interface.GetMethods()));
Run Code Online (Sandbox Code Playgroud)

我希望能够过滤出在基本接口中声明的阴影方法的方法,即"新"方法:

public interface IBaseInterface
{
    string Method();
}

public interface IInterfaceWithNewMethod : IBaseInterface
{
    new string Method();
}
Run Code Online (Sandbox Code Playgroud)

使用我当前的代码,结果包括两种方法 - 我只想检索IInterfaceWithMethod.Method并过滤掉IBaseInterface.Method.

小提琴:https://dotnetfiddle.net/fwVeLS

PS:如果有帮助,您可以假设我可以访问派生接口的具体实例.该实例的类型仅在运行时已知(它是动态代理).

.net c# reflection

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

如何实现覆盖更具体类型的基础成员的效果?

我试图找出一种方法,使派生类由具有比抽象基类中指定的更具体类型的成员组成.这里有一些代码可以满足我的需求,但不符合我提到的规定.因此,客户端代码(在Main中)必须在"BarProperty"上使用强制转换,这是我想要避免的,我想.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteFoo myFoo = new ConcreteFoo();
            myFoo.BarProperty = new ConcreteBar();

            ((ConcreteBar)myFoo.BarProperty).ConcreteBarPrint();

            Console.ReadKey();
        }
    }

    abstract class AbstractFoo
    {
        protected void FooMethod()
        {
            Console.WriteLine("The Foo method!");
        }

        public abstract AbstractBar BarProperty { get; set; }
    }

    class ConcreteFoo : AbstractFoo
    {
        private ConcreteBar _concreteBar;

        public override AbstractBar BarProperty
        {
            get
            {
                return _concreteBar;
            }
            set
            {
                _concreteBar = (ConcreteBar)value;
            }
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c# inheritance

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