小编thm*_*shd的帖子

为嵌套(子)对象订阅INotifyPropertyChanged

我正在寻找一个干净而优雅的解决方案来处理INotifyPropertyChanged嵌套(子)对象的事件.示例代码:

public class Person : INotifyPropertyChanged {

  private string _firstName;
  private int _age;
  private Person _bestFriend;

  public string FirstName {
    get { return _firstName; }
    set {
      // Short implementation for simplicity reasons
      _firstName = value;
      RaisePropertyChanged("FirstName");
    }
  }

  public int Age {
    get { return _age; }
    set {
      // Short implementation for simplicity reasons
      _age = value;
      RaisePropertyChanged("Age");
    }
  }

  public Person BestFriend {
    get { return _bestFriend; }
    set {
      // - Unsubscribe …
Run Code Online (Sandbox Code Playgroud)

.net c# events inotifypropertychanged

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

从ASP.NET MVC 4项目中排除WIF授权的特定路径

我们已经在Visual Studio 2012 的Identity and Access ...扩展的帮助下,在ASP.NET 4.5 MVC 4项目中成功配置了Windows身份基础(WIF).但是无法从授权中排除特定路径以允许匿名访问.

当我们访问我们的默认路由(即/Home)时,被动重定向会将我们重定向到配置的发行者Uri.这是当前的.但现在假设我们想要/Guest从STS身份验证中排除Path ,这样每个人都可以访问http://ourhost/Guest而无需路由到STS颁发者.只有静态文档位于那里.

片段来自Web.config:

<system.identityModel>
  <identityConfiguration>
    <audienceUris>
      <add value="http://ourhost/" />
    </audienceUris>
    <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <trustedIssuers>
        <add thumbprint="9B74****40D0" name="OurSTS" />
      </trustedIssuers>
    </issuerNameRegistry>
    <certificateValidation certificateValidationMode="None" />
  </identityConfiguration>
</system.identityModel>
<system.identityModel.services>
  <federationConfiguration>
    <cookieHandler requireSsl="false" />
    <wsFederation passiveRedirectEnabled="true" issuer="http://oursts/Issue" realm="http://ourhost/" reply="http://ourhost/" requireHttps="false" />
  </federationConfiguration>
</system.identityModel.services>
Run Code Online (Sandbox Code Playgroud)

我们还有......

<system.webServer>
  <!-- ... -->
  <modules runAllManagedModulesForAllRequests="true">
    <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    <add name="SessionAuthenticationModule" …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc claims-based-identity wif asp.net-mvc-4

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

await和Task.Wait之间的区别

第一种方式:

var tds=SearchProcess();
await tds;

public async  Task<XmlElement> SearchProcess()
{
}
Run Code Online (Sandbox Code Playgroud)

第二种方式:

var tds= Task.Factory.StartNew(()=>SearchProcess());
Task.WaitAll(tds);

public XmlElement SearchProcess()
{
}
Run Code Online (Sandbox Code Playgroud)

在上述两种方法中,存在任何性能差异吗?

c#

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

具有变量名称、QueueTriggerAttribute 限制的 Azure Functions QueueTrigger

假设以下典型的队列触发函数:

public void Run([QueueTrigger("queue1")]object data, ILogger log)
{
    // Do something with data
}
Run Code Online (Sandbox Code Playgroud)

我的问题是它"queue1"必须是一个常量字段,因此必须在编译时定义它。另外,我想要一个队列触发器的基类,它可以像这样工作:

public abstract class QueueBase<TModel>
{
    public void Run([QueueTrigger("queueName")]TModel data, ILogger log)
    {
        // Do something with data, log something etc.
        OnRunExecuted(data);
        // Do something with data, log something etc.
    }

    public abstract void OnRunExecuted(TModel data);
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我可以编写自己的类,这些类继承自QueueBase但甚至可以存在于没有Microsoft.Azure.WebJobs依赖项的库中:

public class MyQueueHandler : QueueBase<MyModel>
{
    public void OnRunExecuted(MyModel data) => ...;
}
Run Code Online (Sandbox Code Playgroud)

但不可能传递队列名称......是吗?

.net c# .net-core azure-functions

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

Silverlight MVVM绑定以不需要的顺序更新

场景:在Silverlight 4 MVVM项目中,我们有一个ListBox包含项目的控件,所选项目与ViewModel中的相应属性双向绑定.另一个控件(例如,我将其剥离为单个控件TextBox)是绑定到所选项目内容的数据.该值应在休假/焦点丢失时更新.

问题:当TextBox更改中的值并TextBox通过按Tab键离开时,一切都按预期工作 - 值会更新.但是,如果用户单击其中的其他项ListBox,则在触发setter的内容之前触发SelectedItem setter TextBox,从而没有机会处理用户输入.

屏幕

在向属性设置器添加断点时,您可以在调试器中看到在处理更新ListView之前首先应用新选择TextBox.

期望的行为:在用户选择另一个项目之前,我们需要知道当前选择的项目已被修改.不希望有一个自定义更新触发器,它会在每次按键时发出通知(我们知道这是可能的).

你能帮我吗?

代码(一个非常简单的例子):

视图模型

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class ItemViewModel : ViewModelBase
{
    private string _content;

    public ItemViewModel(string initContent)
    {
        _content = initContent;
    }

    public string Content …
Run Code Online (Sandbox Code Playgroud)

data-binding silverlight mvvm silverlight-4.0

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

WebJob QueueTrigger TimeoutException:操作“GetMessages”未在“00:02:00”中完成

我的问题 60304305有点相关......我的 Azure webJob 应用程序处理多个队列,但它们不会产生高流量。尽管如此,该应用程序仍会不时遇到超时异常,并且几乎不可能重现该错误,但是在 Queue 函数内的断点处中断并保持一段时间时,我取得了一些成功。

System.TimeoutException
  HResult=0x********
  Message=The operation 'GetMessages' with id '********-****-****-****-************' did not complete in '00:02:00'.
  Source=Microsoft.Azure.WebJobs.Extensions.Storage
  StackTrace:
   at Microsoft.Azure.WebJobs.Extensions.Storage.TimeoutHandler.<ExecuteWithTimeout>d__1`1.MoveNext() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Extensions.Storage\TimeoutHandler.cs:line 30
   at Microsoft.Azure.WebJobs.Host.Timers.WebJobsExceptionHandler.<>c__DisplayClass3_0.<OnUnhandledExceptionAsync>b__0() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Timers\WebJobsExceptionHandler.cs:line 54
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

  This exception was originally thrown at this call stack:
    Microsoft.Azure.WebJobs.Extensions.Storage.TimeoutHandler.ExecuteWithTimeout<T>(string, string, Microsoft.Azure.WebJobs.Host.Timers.IWebJobsExceptionHandler, System.Func<System.Threading.Tasks.Task<T>>) in TimeoutHandler.cs
    Microsoft.Azure.WebJobs.Host.Timers.WebJobsExceptionHandler.OnUnhandledExceptionAsync.AnonymousMethod__0() …
Run Code Online (Sandbox Code Playgroud)

.net azure azure-webjobs

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

Visual Studio IDE0059 C# 不必要的值分配错误?

我有以下 C# 代码(我将其减少到最低限度以简化它)。Visual Studio 2019,.NET Framework 4.7.2。

public void Demo()
{
    ReportStart();
    var success = false;
    try
    {
        int no = 1;
        switch (no)
        {
            case 1:
            default:
                break;
        }

        DoSomething();

        success = true;
    }
    finally
    {
        ReportEnd(success);
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这并没有什么不妥。该函数可能会失败(我不想抓住它)但在离开之前,它会向另一个方法报告成功执行。调试时,它会做它应该做的事情。

有趣的是,Visual Studio 2019 将报告以下内容:

IDE0059

当我按照建议选择“删除冗余分配”时,它将删除该行success = true;,从而有效地改变结果!

现在你会问开关/外壳是什么?删除它时,建议消失:

动画片

有什么原因吗,还是 Visual Studio 中的错误?

.net c# roslyn visual-studio-2019

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

使用MongoDB的NodaTime:值类NodaTime.ZonedDateTime无法反序列化

我正在使用最新版本的NodaTimeMongo DB官方驱动程序.我有一个简单的POCO类,它使用NodaTime ZonedDateTime作为一些属性中.NET DateTime的替代品.

public class MyPOCO
{
    [BsonId]
    [Key]
    public ObjectId SomeId { get; set; }

    public string SomeProperty { get; set; }

    public ZonedDateTime SomeDateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我可以轻松地将Model放入集合中,但是当我尝试读取查询的模型时,我得到以下内容MongoDB.Bson.BsonSerializationException:

值类NodaTime.ZonedDateTime无法反序列化

什么是解决/解决这个问题的好或最佳做法?

UPDATE

在发布我的问题解决方案后,我面临一个可能的新问题......当我查询集合并在我的查询中使用DateTime时,就像where SomeDateTime < now' (where现在is a variable I create from system time) it seems that each document must be deserialized using myZonedDateTimeSerializer`可以在where子句之前进行评估.这看起来像是一个很大的性能问题,不是吗?我真的要考虑再次回到BCL DateTime,即使它会受到伤害.

更新2

我正在接受我的解决方案ZonedDateTimeSerializer,但我对NodaTime与MongoDB的结合感到不舒服,而两者都是很好的个性化解决方案.但是他们目前在没有大量操纵的情况下一起工作得不好.

c# mongodb nodatime mongodb-.net-driver

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

在C#中协变使用泛型Lazy类

假设这适用:

public class Cat : Animal { }
Run Code Online (Sandbox Code Playgroud)

并假设我有一个方法:

public void Feed(Animal animal) { ... }
Run Code Online (Sandbox Code Playgroud)

我可以这样称呼它:

var animal = new Cat();
Feed(animal);
Run Code Online (Sandbox Code Playgroud)

Feed重构为仅支持Lazy<Animal>参数时,如何才能使其正常工作?我想以var lazyAnimal = new Lazy<Cat>();某种方式传递给我.

这显然不起作用:

var lazyAnimal = new Lazy<Cat>();
Feed(lazyAnimal);
Run Code Online (Sandbox Code Playgroud)

c# covariance lazy-initialization c#-4.0

4
推荐指数
2
解决办法
354
查看次数

Silverlight自动选择http和https之间的安全传输模式

我们的Silverlight应用程序可以在httphttps(SSL,使用传输安全性)模式下运行.在我们的ServiceReferences.ClientConfig文件中,我们只是这样配置我们的服务端点:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="DefaultEndpoint"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647">
          <security mode="None" />
          <!-- Enable for SSL: mode="Transport" -->
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="/services/DefaultService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="DefaultEndpoint"
                contract="OurNamespace.IOurContractAsync"
                name="DefaultEndpoint" />
    </client>
  </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

可以在两种模式下访问配置的端点.它只取决于加载XAP文件的上下文:From http://example.com/slpage.htmlhttps://example.com/slpage.html.不幸的是,我们必须在"无"和"传输"之间手动切换安全模式设置.其他一切都已按预期工作.当安全模式为"无"并且我们通过https访问时,我们得到一个例外"提供了..https但是预期了http ......",反之亦然.是否有机会让Silverlight自动决定应该使用哪种安全模式?这个问题最简单的解决方案是什么?

提前致谢

托马斯

silverlight ssl wcf silverlight-4.0

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

替换Silverlight中缺少TextTrimming选项"CharacterEllipsis"

的Silverlight(至少,如第4版的)不具有CharacterEllipsis选项TextTrimming,这WPF具有.它可以用于TextBlock.这意味着,如果没有足够的空间来展示"那令人难以置信",我可以修剪为"那是......"而不是"那太棒了......"我们宁愿这么想.

不过,我们会尝试实现自定义文本修剪功能.基本上,那不是那么难.一种非常愚蠢的方法是测量字符串的像素,与可用宽度进行比较,并通过剪切最后一个字符并在文本仍然不适合的循环中添加"..."来操纵字符串.以下是一个如何工作的示例:

// Not perfect but good enough for us
private bool AutoTrim(string fullText, TextBlock textBlock, double maxWidth)
{
    double factor = maxWidth / textBlock.ActualWidth;
    if (factor > 1)
        return false;

    int newTextLength = (int)Math.Floor((double)fullText.Length * factor);
    string trimTest;
    do
    {
        trimTest = fullText.Substring(0, newTextLength--);
        textBlock.Text = trimTest + "..."; // problematic...
        factor = maxWidth / textBlock.ActualWidth;
    }
    while (factor < 1 && newTextLength > 0);

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

但是在代码后面(或在代码中Behavior)执行此操作会导致一些问题:例如,当我们想要更新显示的文本并设置TextBlock的 …

silverlight silverlight-4.0 texttrimming

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

带有版本通配符的 ASP.NET MVC 参考脚本文件(无捆绑)

在 ASP.NET MVC 4 项目中,我想引用这样的版本化脚本文件:

// Just some pseudo-code:
<script src="@Latest("~/Scripts/jquery-{0}.min.js")"></script>

// Resolves to the currently referenced script file
<script src="/Scripts/jquery-1.10.2.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

这样当通过 NuGet 更新新的脚本版本时,引用会自动更新。我知道捆绑和缩小功能,但它只是太多了。我只想要解决通配符的小部分。我的文件已经缩小了,而且我也不想要这些包。

你有一些聪明的想法如何解决这个问题吗?

asp.net asp.net-mvc razor asp.net-mvc-4

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