小编Sim*_*tes的帖子

C# 9 记录验证

使用 C# 9 的新记录类型,如何在对象的构造过程中注入自定义参数验证/空检查/等而无需重新编写整个构造函数

类似的东西:

record Person(Guid Id, string FirstName, string LastName, int Age)
{
    override void Validate()
    {
        if(FirstName == null)
            throw new ArgumentException("Argument cannot be null.", nameof(FirstName));
        if(LastName == null)
            throw new ArgumentException("Argument cannot be null.", nameof(LastName));
        if(Age < 0)
            throw new ArgumentException("Argument cannot be negative.", nameof(Age));
    }
}
Run Code Online (Sandbox Code Playgroud)

c# validation c#-9.0 c#-record-type

20
推荐指数
5
解决办法
1822
查看次数

VSCode:调试开始时清除集成终端

"console": "integratedTerminal"在 launch.json 中使用时,程序输出被重定向到集成终端。但是,在终止调试会话并启动另一个调试会话后,终端会被重新使用,这可能非常烦人。

我还没有找到让 VSCode 清除终端的方法——可以使用属性清除 tasks.json 中的面板clear: true,但这仅适用于构建任务等任务,但对调试面板没有影响。

非常感谢帮助。

提前
致谢 - 西蒙

visual-studio-code

16
推荐指数
2
解决办法
1097
查看次数

C#Async/Await:在创建任务时保留AsyncLocal <T>上下文

AsyncLocal允许我们将上下文数据保存在异步控制流上.这非常简洁,因为以下所有简历(即使在另一个线程上)都可以检索和修改环境数据(MSDN上的AsyncLocal).

有没有办法'离开'子任务的当前异步本地上下文,从而创建一个新的?

AsyncLocal<string> Data = new AsyncLocal<string>();
Data.Value = "One";

Task.Factory.StartNew( () =>
{
    string InnerValue = Data.Value;
    //InnerValue equals to "One", I need it to be null.
} );
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,内部任务与外部控制流共享AsyncLocal上下文.有没有办法强制执行新的上下文?

更新:为了在这里解决我的问题,以下工作就像一个魅力(尽管事实上它并没有完全重置上下文):

AsyncLocal<string> Data = new AsyncLocal<string>();
Data.Value = "One";

Task.Factory.StartNew( () =>
{
    Data.Value = null;
    string InnerValue = Data.Value;
    //InnerValue equals to null now.
} );

string OuterValue = Data.Value; //Stays "one" even after the inner change.
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous

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

UWP应用程序和.NET Core RC2:不能引用netstandard1.4软件包

我有一个场景,我使用共享代码库运行UWP客户端应用程序,UWP IOT应用程序和.NET Core应用程序.在.NET Core RC1中,我构建了一个类库(Package),并使用"dotnet5.4"作为该库的基础框架.

使用"生成构建输出"我可以从.NET Core应用程序(控制台)引用创建的nuget包并使用解决方法(从%local%.dnx - >%local%.nuget复制包)UWP应用程序能够参考并使用该包.

现在在RC2中,事情发生了一些变化,我再次能够使用已升级的库(在项目文件中升级的工具,更改为project.json,netstandard1.4(因为1.5根据不能与UAP10一起使用))完美地使用.NET Core控制台应用程序.

对于UWP我无法添加库,因为我得到了几十个臭名昭着的

"[...] provides a compile-time reference assembly [...] but there is no run-time assembly compatible with [...]"
Run Code Online (Sandbox Code Playgroud)

错误.

经过一些环顾四周,我试图找出问题并发现我甚至无法添加对System.IO.FileSystem.Watcher的引用,原因如下:

System.IO.FileSystem.Watcher 4.0.0-rc2-24027 provides a compile-time reference assembly for System.IO.FileSystem.Watcher on UAP,Version=v10.0, but there is no run-time assembly compatible with win10-arm-aot.
Some packages are not compatible with UAP,Version=v10.0 (win10-x64-aot).
System.IO.FileSystem.Watcher 4.0.0-rc2-24027 provides a compile-time reference assembly for System.IO.FileSystem.Watcher on UAP,Version=v10.0, but there is no run-time assembly compatible with win10-x64. …
Run Code Online (Sandbox Code Playgroud)

.net c# dnx windows-10-iot-core uwp

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

UWP XAML x:无法识别绑定的继承接口

在UWP XAML应用程序中使用x:Bind时,请考虑以下事项:

interface IBaseInterface
{
    string A { get; set; }
}
interface ISubInterface : IBaseInterface
{
    string B { get; set; }
}
class ImplementationClass : ISubInterface
{
    public string A { get; set; }
    public string B { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在Page类中,我们有以下内容:

public partial class MainPage : Page
{
    public ISubClass TheObject = new ImplementationClass { A = "1", B = "2" };
    //All the rest goes here
}
Run Code Online (Sandbox Code Playgroud)

在MainPage XAML中,我们有以下代码段:

<TextBlock Text={x:Bind Path=TheObject.A}></TextBlock>
Run Code Online (Sandbox Code Playgroud)

这导致以下编译器错误:XamlCompiler错误WMC1110:无效的绑定路径'A':在'ISubInterface'类型上找不到属性'A'

但是,以下工作正常:

<TextBlock …
Run Code Online (Sandbox Code Playgroud)

c# xaml uwp xbind

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

Windows 10 UAP - 编译数据绑定

根据Windows 10 SDK启动视频(http://www.microsoftvirtualacademy.com/training-courses/a-developers-guide-to-windows-10-preview?prid=ch9courselink),有一种新的绑定类型Windows 10通用应用程序平台称为"编译数据绑定".

代替

"{Binding Path=..."
Run Code Online (Sandbox Code Playgroud)

新的风格是

"{x:Bind Path=..."
Run Code Online (Sandbox Code Playgroud)

但是,这只会引发编译器错误,即放置=后面的任何内容都不存在于上下文中.
设置

"{x:Bind Path=DataContext...."
Run Code Online (Sandbox Code Playgroud)

也不起作用.
有没有人设法让新的绑定工作?是否有关于该主题的任何文档,因为我似乎无法找到任何东西(甚至没有你可以"逆转engeneer"的样本......

更新:
感谢Nick的回复,我可以添加以下内容:
由于我通常在初始化Page/UserControl后插入视图模型,因此Page/UserControl似乎没有注意到更新的ViewModel属性(即使Page/UserControl实现并且"触发" "INotifyPropertyChanged".显然,Pages/UserControls中有一个名为Bindings的新字段,可以强制重置所有已编译的数据绑定.

因此,一旦您更改了ViewModel(或x:Bind引用的其他属性),您只需调用:

Binding.UpdateAll()
Run Code Online (Sandbox Code Playgroud)

这样,Page/UserControl重新评估所有已编译的数据绑定并接受"数据上下文切换".

c# windows xaml binding win-universal-app

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

Windows 10 IOT生命周期(或:如何属性终止后台应用程序)

为了在带有Windows 10 IOT Core的无头Raspberry Pi 2上使用UWP应用程序,我们可以使用后台应用程序模板,该模板基本上创建了一个新的UWP应用程序,只有在启动时执行的后台任务:

<Extensions>
  <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundApplication1.StartupTask">
    <BackgroundTasks>
      <iot:Task Type="startup" />
    </BackgroundTasks>
  </Extension>
</Extensions>
Run Code Online (Sandbox Code Playgroud)

为了使应用程序保持运行,我们可以使用以下启动代码:

public void Run( IBackgroundTaskInstance taskInstance )
{
  BackgroundTaskDeferral Deferral = taskInstance.GetDeferral();

  //Execute arbitrary code here.
}
Run Code Online (Sandbox Code Playgroud)

这样,应用程序将继续运行,并且在IOT Universe中的任何超时后操作系统都不会终止应用程序.

到目前为止,太棒了.

但是:我希望能够在设备关闭时正确关闭后台应用程序(或者要求应用程序"轻轻地"关闭.

在"普通"UWP应用程序中,您可以订阅OnSuspending事件.
如何在此背景场景中获得有关即将关闭/关闭的通知?

非常感谢帮助.
提前致谢!
-Simon

.net c# win-universal-app

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

UWP:窗口按钮的样式在关闭按钮上不起作用

在Windows 10 UWP中,我们可以设置标题栏和窗口按钮(windows chrome)的样式.但是,当使用悬停或单击它们时,这对关闭按钮不起作用.请看以下代码段:

        Color PrimaryColor = Color.FromArgb( 0xFF, 0xFF, 0x00, 0x00 );
        Color ContrastColor = Color.FromArgb( 0xFF, 0x00, 0x00, 0x00 );
        Color SemiColor = Color.FromArgb( 0xFF, 0x7F, 0x00, 0x00 );

        ApplicationView AppView = ApplicationView.GetForCurrentView();

        AppView.TitleBar.ButtonInactiveBackgroundColor = ContrastColor;
        AppView.TitleBar.ButtonInactiveForegroundColor = PrimaryColor;
        AppView.TitleBar.ButtonBackgroundColor = ContrastColor;
        AppView.TitleBar.ButtonForegroundColor = PrimaryColor;

        AppView.TitleBar.ButtonHoverBackgroundColor = PrimaryColor;
        AppView.TitleBar.ButtonHoverForegroundColor = ContrastColor;

        AppView.TitleBar.ButtonPressedBackgroundColor = SemiColor;
        AppView.TitleBar.ButtonPressedForegroundColor = ContrastColor;
Run Code Online (Sandbox Code Playgroud)

这应该使所有按钮变黑,并带有红色前景(图标).按下时应使用半红色背景.
它可以完美地用于最小和最大按钮 - 在关闭按钮上只有背景和前景(以及它的非活动版本)才能正确显示.悬停和按下状态回退到默认窗口值.

请参阅下图.关闭按钮有另一个红色(默认值)和悬停时的默认白色前景.

TitleBar按钮 _

有谁知道问题是什么?这是一个错误还是我做错了什么?

提前致谢!
-Simon

.net c# win-universal-app

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

泛型类型推断的通用(响应请求)

我目前正在开发一个通用请求/响应抽象,以便在接收基于请求的预定义响应类时向组件发送任意类请求.

我有两个基类:

abstract class RequestClass<T>
    where T : ResponseClass
{
}

abstract class ResponseClass
{
}
Run Code Online (Sandbox Code Playgroud)

因此,请求类定义其响应类.为了演示这种情况,我有以下示例类:

class DoSomethingRequest : RequestClass<DoSomethingResponse>
{
}

class DoSomethingResponse : ResponseClass
{
}
Run Code Online (Sandbox Code Playgroud)

现在要处理请求,我有以下方法:

TResult SendRequest<T, TResult>( T Request )
    where T : RequestClass<TResult>
    where TResult : ResponseClass
{
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好; 但是,虽然我希望以下工作正常,但编译器无法推断出以下请求的类型参数:

DoSomethingResponse Response = SendRequest( new DoSomethingRequest() );
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?编译器应该具有所需的所有信息,并推断"DoSomethingRequest是DoSomethingResponse的RequestClass"并且编译没有问题.有没有其他方法可以使我的方案工作?

感谢帮助.在此先感谢您的问候!-Simon

.net c# compiler-construction generics

5
推荐指数
0
解决办法
72
查看次数

如何在本地运行Windows 10 IOT应用程序?

有没有办法允许正常的Windows 10安装运行Windows 10 IOT应用程序进行调试?现在,当我将架构从ARM(Raspberry Pi 3)设置为x64(本地机器)时,我在启动调试会话时收到以下错误消息:

无法激活Windows应用商店应用. 激活请求失败,错误

我在这里读到它与"嵌入模式"有关但我无法找到任何关于它的信息,并且组策略似乎没有包含任何关于启用嵌入模式的信息......

非常感谢帮助!提前致谢!

c# raspberry-pi win-universal-app iot windows-10-iot-core

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