我正在阅读与处理空值有关的这篇文章.
其中一个建议(根据SO帖子)是在空值无效时使用Assert.
我(到目前为止)在测试项目中广泛使用了null.在普通代码(测试项目除外)中使用Asserts语句对我来说很奇怪.
为什么 - >因为我从未使用过这种方式,所以也不要在任何书中读过它.
问题
1.使用Asserts作为前提条件是否可行
2. 断言的优点/缺点检查参数并抛出Argument___Exception
如果重要,我要求.NET(不适用于java)
我的表单上有 2 个按钮,它们看起来都非常非常扁平。

我似乎找不到让它们看起来更像的方法:

我的按钮的 XAML 是:
<Button x:Name="bttnDailyReport" HorizontalAlignment="Left" Margin="618,27,0,0" VerticalAlignment="Top" Width="121" Height="93" FontFamily="Microsoft Sans Serif" FontSize="20" FontWeight="Bold" Grid.Column="1" BorderBrush="Black">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"><Run Text=" Generate"/><LineBreak/><Run Text="Daily Report"/></TextBlock>
</Button>
<Button x:Name="bttnCancel" HorizontalAlignment="Left" Margin="618,126,0,0" VerticalAlignment="Top" Width="121" Height="93" FontFamily="Microsoft Sans Serif" FontSize="20" FontWeight="Bold" Click="BttnCancelClick">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"><Run Text="Exit"/></TextBlock>
</Button>
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是,是否可以使按钮看起来像 WinForms 中的按钮,或者我是否使用扁平按钮?
假设我有:
ethernet_adapter.PacketArrived += (s, e) =>
{
//long processing...
};
Run Code Online (Sandbox Code Playgroud)
处理可能需要很长时间,而当它处于中间时,另一个数据包已到达.接下来会发生什么:处理完成然后触发另一个事件,或者可能在新线程上立即触发新事件?
我有一个函数返回一个对象,该对象表示我的数据库中的记录以及其他列.我没有为这个对象创建一个单独的类,而是想知道是否还有其他方法,例如:
public object GetRecord(string key)
{
var item = select new {column1, column2};
return item;
}
public void main()
{
var item = GetRecord(1);
// I want to be able to reference column1 on item.
var x = item.column1;
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,我在其中运行了一个进程BackgroundWorker.我想支持从用户界面取消,但我没有看到一个简单的方法来做到这一点.
该Process实际上是一个相当长的运行命令行exe文件.输出通过Progress.OutputDataReceived事件异步重定向,并用于向GUI报告进度.
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
using (var process = new Process())
{
//...
process.Start()
//...
process.WaitForExit();
}
}
private void CancelWorker()
{
worker.CancelAsync();
// where to actually listen for the cancellation???
}
Run Code Online (Sandbox Code Playgroud)
除了StandardInput应用程序本身将响应特定输入以中止之外,似乎没有办法让进程"监听"来自主线程的任何输入.
有没有办法根据主线程的取消请求取消进程?
出于我在运行过程中运行的EXE的目的,我可以调用Process.Close()退出而没有任何副作用,但该Process对象仅为worker_DoWork()方法所知,因此我需要跟踪Process实例以进行取消. ..这就是为什么我希望有更好的方法.
(如果重要的话,我的目标是.NET 3.5以解决兼容性问题.)
我试图理解Action<T>, Func<T> and Predicate<T>代表之间的差异,作为我的WPF/MVVM学习的一部分.
我知道Action<T> and Func<T>两个都是零到一个+参数,只Func<T>返回一个值,而Action<T>不是.
至于Predicate<T>- 我不知道.
因此,我提出了以下问题:
Predicate<T>办?(欢迎举例!)Action<T>返回,那么使用它会不会更简单?(或任何其他类型,如果我们正在讨论.)voidFunc<T>我希望你在问题中避免使用LINQ/List示例.
我已经看过那些了但是它们只是让它变得更加混乱,因为让我对这些代表"感兴趣"的代码与它无关(我想!).
因此,我想使用我熟悉的代码来获得我的答案.
这里是:
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return …Run Code Online (Sandbox Code Playgroud) 假设我想向用户显示一些验证错误.在MVVM模式中,我可以有一个标签绑定到我的viewmodel上的某个属性.但是,如果我想在严格遵守MVVM模式的同时显示消息框,该怎么办呢?我的viewmodel会绑定什么,以及它将如何触发创建/显示消息框?
我需要以下列方式进行自定义配置
<root>
<group>
<groupitem>
<property1/>
<property2/>
<property3/>
<property4/>
</groupitem>
<group>
</root>
Run Code Online (Sandbox Code Playgroud)
我没有找到任何如何做到这一点的例子.
以下非常接近,
但我仍然坚持定义与groupitem相对应的类.如果我改变property1,property2从元素属性,很容易定义.
但如果这些属性具有嵌套属性,那将会产生问题.
问题是,如何以上面定义的方式定义嵌套层次结构?
在其中一个模块中,我看到了以下样式设置.
<Style TargetType="Rectangle">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="300" Duration="0:0:1.5"
AccelerationRatio="0.10" DecelerationRatio="0.25"
Storyboard.TargetProperty="(Canvas.Width)" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
注意,TargetType是Rectangle,Storyboard.TargetProperty是Canvas.Width.样式/触发器仍然正常工作.这是动画的Rectangle.width财产.
我理解在Storyboard.TargetProperty(或XAML中的任何其他地方),我们必须使用PropertyPath语法,就像(ownerType.PropertyName).
我的问题是如何设置动画Canvas.Width是动画Rectangle.Width
我需要转换表的数据并进行一些操作.其中一个列数据类型是Varchar,但它存储十进制数字.我正在努力将varchar转换为十进制.
我试过跟随CAST(@ TempPercent1 AS DECIMAL(28,16))
问题是数据也有一些指数表示法的值,例如:1.61022e-016.sql查询在遇到这样的值时抛出错误.错误是Varchar
我需要知道如何在varchar到十进制转换期间处理指数表示法值?