小编Noc*_*tis的帖子

C#:处理WebClient"协议违规"

我需要在路由器中读取一个位置,但是我得到以下异常 -

ServerProtocolViolation "The server committed a protocol violation. 
                        Section=ResponseHeader Detail=CR must be followed by LF"
Run Code Online (Sandbox Code Playgroud)

当我使用该.DownloadString(url)功能时会发生这种情况.有没有办法让WebClient忽略协议违规?在谷歌搜索告诉我,我应该在useUnsafeHeaderParsing某处设置选项.我可以通过程序来完成吗?如果我使用它会有什么影响?

编辑:附加代码 -

    public Readlog() {
        WebClient wc = new WebClient();

        string url = @"http://192.168.0.1/setup.cgi?next_file=log.htm&todo=cfg_init";
        Console.WriteLine(url);
        try {
            //wc.Headers.Add("User-Agent", "Mozilla/5.0(Windows; U; Windows NT 5.2; rv:1.9.2) Gecko/20100101 Firefox/3.6");
            wc.Credentials = new NetworkCredential("admin", "admin");
            //Next line causes exception System.Net.WebException
            //Message - "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF"
            //May be I need to use …
Run Code Online (Sandbox Code Playgroud)

c#

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

WPF:按住单击+双击问题

我必须处理单击和双击WPF应用程序中的按钮并进行不同的反应.不幸的是,在双击时,WPF会触发两次点击事件和双击事件,因此很难处理这种情况.

它试图用计时器解决它,但没有成功...我希望你能帮助我.

让我们看看代码:

private void delayedBtnClick(object statInfo)
{
    if (doubleClickTimer != null)
        doubleClickTimer.Dispose();
    doubleClickTimer = null;

    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new VoidDelegate(delegate()
    {
        // ... DO THE SINGLE CLICK ACTION
    }));
}

private void btn_Click(object sender, RoutedEventArgs e)
{
    if (doubleClickTimer == null)
        doubleClickTimer = new Timer(delayedBtnClick, null, System.Windows.Forms.SystemInformation.DoubleClickTime, Timeout.Infinite);
        }
    }
}

private void btnNext_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (doubleClickTimer != null)
        doubleClickTimer.Change(Timeout.Infinite, Timeout.Infinite);    // disable it - I've tried it with and without this line
        doubleClickTimer.Dispose();
    doubleClickTimer = null;

    //.... …
Run Code Online (Sandbox Code Playgroud)

wpf timer mouseclick-event

15
推荐指数
2
解决办法
3万
查看次数

Dirty Rects分组

我有一个非常简单的可滚动日历UI:

在此输入图像描述

但在滚动期间,日历会不时闪烁.我看过WPF Performance Suite,发现有大量的Dirty Rects(大约400个):

在此输入图像描述

日历的标记是ItemsControl,它绑定Days(仅限可见天数).看起来像WPF一天一天地重绘(所以这就是为什么这么简单的用户界面有这么多脏的原因).我想可能有一种方法可以告诉WPF不要重绘许多小矩形但是一次重绘整个ItemsControl(类似于Double Buffering在WinForms的所有好日子里所做的).

PS WritableBitmap解决了这个问题,但我希望有一个更好的方法

更新.如果我切换"显示脏区更新覆盖"选项,则以下是Calendar的外观:

在此输入图像描述

所以WPF正确地找到了脏区域.问题是为什么它决定使用这么多脏的重新绘制它.我的猜测是因为天数(白色的一个或两个像素)之间的空间在滚动期间是相同的.

更新2.

这是日历的标记:

<ItemsControl  Panel.ZIndex="1" Grid.Column="1" 
       ItemsSource="{Binding Days}" 
       VerticalAlignment="Center" 
       HorizontalAlignment="Stretch">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border Margin="1,0,1,0" Padding="0,0,3,0" 
                  CornerRadius="1" Width="28" Height="28" 
                  VerticalAlignment="Top">
                <Border.Background>
                    <MultiBinding Converter="{StaticResource DayOfWeekToColorConverter}">
                        <Binding Path="IsWeekend"/>
                    </MultiBinding>
                </Border.Background>
                <StackPanel>
                    <TextBlock  Style="{StaticResource TextStyle}" 
                          HorizontalAlignment="Center" 
                          VerticalAlignment="Center"/>
                    <Label  Style="{StaticResource LabelStyle}" 
                          Content="{Binding Date.Day}" 
                          HorizontalAlignment="Center" 
                          VerticalAlignment="Center"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

.net wpf

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

Mvvm-Light Silverlight,使用带有Combobox的EventToCommand

我已经在我的视图模型中将ComboBox的SelectedItemChangeEvent连接到ICommand.一切似乎工作正常,但我不知道如何获得ComboxBox的SelectedItem.我想我需要使用EventToCommand的CommandParameter - 我将它绑定到我的ViewModel中具有ComboBox的selectedItem的东西吗?我试过这个:

<ComboBox 
  Width="422"
  Height="24"
  DisplayMemberPath="Name"
  ItemsSource="{Binding CategoryTypes}"
  SelectedItem="{Binding SelectedCategory}"
  >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <MvvmLight:EventToCommand 
              Command="{Binding SelectCategoryCommand,Mode=TwoWay}"
              CommandParameter="{Binding SelectedCategory, Mode=TwoWay}"
              MustToggleIsEnabledValue="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

在我的视图模型中:

public ICommand SelectCategoryCommand
{
    get
    {
        return new SelectCategoryCommand(this);
    }
}

public CategoryType SelectedCategory
{
    get; set;
}
Run Code Online (Sandbox Code Playgroud)

和ICommand

public class SelectCategoryCommand : ICommand
{
    private RowViewModel _rowViewModel;

    public SelectCategoryCommand(RowViewModel rowViewModel)
    {
        _rowViewModel = rowViewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    { …
Run Code Online (Sandbox Code Playgroud)

silverlight selecteditem mvvm-light

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

Gnuplot:多个堆叠直方图,每个组使用相同的密钥

我试图创建具有多个堆叠柱状图就像例如8情节在这里.但对于我的数据,每个组都有相同的四个类别.

在此输入图像描述

如何更改颜色和键以使每个堆叠的列的颜色变为红色,绿色,蓝色,粉红色?所以关键只有我正在绘制的4件事中的每一件都有一份?

这是我用来绘制的线:

plot newhistogram "1", 'addresses.dat' using 2:xtic(1) t 2, '' u 3 t 3, \
'' u 4 t 4, '' u 5 t 5, newhistogram "2", '' u 6 t 6, '' u 7 t 7, '' u 8 t 8,\
'' u 9 t 9
Run Code Online (Sandbox Code Playgroud)

我的数据格式与我上面链接的示例格式相同:

Address PAL_Code BASH App Kernel PAL_Code BASH App Kernel
FFT 1 1 2 2 1 1 3 4
RADIX 1 2 3 4 1 2 4 5
LU …
Run Code Online (Sandbox Code Playgroud)

plot gnuplot

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

nvarchar不是可识别的游标选项

我正在运行一些sql,它从临时表中获取信息并将其放入永久表中.我是从3年前写的一步一步的指南中得到的,写这篇文章的人早就不见了.它声明在这里使用这个sql.

declare @Password nvarchar(100); 
set @Password ='rewards';
if not exists(select 1 from sys.openkeys where key_name = 'Sym_UserPassData' 
and database_name = db_name())  OPEN SYMMETRIC KEY Sym_UserPassData DECRYPTION BY
CERTIFICATE UserPassTables with password='asbpass71509new';
INSERT INTO [User] (Username, [Password], AllowChange, ForceChange, FullName,
SalesRep, OpenLink, UserProfileID, LastUpdatedBy,UserEmail) 
(SELECT Username,EncryptByKey(Key_GUID('Sym_UserPassData'),@Password), 
AllowChange, ForceChange, FullName, SalesRep, [OpenLink ], UserProfileID,    
LastUpdateBy, UserEmail FROM TempUsers)
Run Code Online (Sandbox Code Playgroud)

之后它说了以下内容

如果每行的密码都是唯一的,请设置@Password ='密码'; 关闭并用[密码]替换@Password.

所以起初我只是更改了第二行,所以它说

declare @Password nvarchar(100);
set [Password]
...
Run Code Online (Sandbox Code Playgroud)

但是这给了我一个密码列的错误,所以我把它改成:

declare [Password] nvarchar(100); 
set [Password]
if not exists(select 1 from sys.openkeys where …
Run Code Online (Sandbox Code Playgroud)

sql t-sql

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

是否可以在WPF中为静态资源提供类型转换器?

我有一个新手WPF问题.

想象一下,我的用户控件有一个名称空间声明,如下所示:

xmlns:system="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)

我有这样的用户控制资源:

<UserControl.Resources>
    <system:Int32 x:Key="Today">32</system:Int32>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

然后在我的用户控件的某处我有这个:

<TextBlock Text="{StaticResource Today}"/>
Run Code Online (Sandbox Code Playgroud)

这将导致错误,因为它Today被定义为整数资源,但Text属性需要一个字符串.这个例子是人为的,但希望能说明这个问题.

问题是,如果我的资源类型与属性类型完全匹配,我有没有办法为我的资源提供转换器?类似于IValueConverter的绑定或类型转换器.

谢谢!

wpf converter staticresource

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

如何在应用程序运行时发现新的MEF部件?

我正在使用MEF在我的应用程序中加载插件.一切正常,但我希望在将它们放入我的app文件夹时发现新的部分.这可能吗?DirectoryCatalog有一个Changed事件,但我不确定它是如何工作的.

这是我现在的代码:

public sealed class RevealerFactory
{
    private static readonly Lazy<RevealerFactory> lazy = 
            new Lazy<RevealerFactory>(() => new RevealerFactory());

    public static RevealerFactory Instance { get { return lazy.Value; } }

    private FileSystemWatcher watcher;

    private RevealerFactory()
    {
        Initialize();
    }

    [ImportMany(RequiredCreationPolicy = CreationPolicy.Shared)]
    private IEnumerable<Lazy<IRevealer, IRevealerCapabilities>> Revealers { 
        get;
        set; 
    }

    public IRevealer GetRevealer(Uri uri)
    {
        return (from revealer in Revealers
                where uri.Host.Equals(revealer.Metadata.Host, 
                                      StringComparison.OrdinalIgnoreCase) 
                   && revealer.Value.IsRevelable(uri)
                select revealer.Value).FirstOrDefault();
    }

    private void Initialize()
    {
        var catalog = new DirectoryCatalog(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
                                      + "/SDownloader/Revealers");
        var container …
Run Code Online (Sandbox Code Playgroud)

c# plugins mef

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

当系统处于睡眠状态时,Windows服务中的计时器如何表现?

假设我有一个Windows服务,它有一个定时器设置为每6小时运行一次,我希望它每天发射4次.比方说:0000,0600,1200 1800.(军事时间,00:00等)

如果系统在1000点睡觉,在1700点唤醒,会发生什么?

  1. 它会在1900年重新开火,因为它有两个小时的计时器吗?
  2. 它是否会立即开火(因为它错过了1200预约),然后再次在2300开火(将其加入当前时间6小时?)

我注意到,当计算机进入睡眠状态时,它不会触发OnPauseOnContinue方法.

如果有人能够在上述案例中阐明系统的行为,那将是非常有用的.
干杯,并提前感谢.

c# windows-services timer

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

在许多列表视图中使用相同的样式

我想在许多列表视图中使用相同的样式.在我的风格中,我也定义了gridview列.

但是当我尝试运行时,它会引发异常:

View不能由多个ListView共享.

我怎么解决这个问题?


XAML:

<Style x:Key="articleList" TargetType="{x:Type ListView}">
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
<Setter Property="ListView.ItemsSource" Value="{Binding}"/>
<Setter Property="ListView.View">
    <Setter.Value>
        <GridView>
            <GridViewColumn Header="Subject" Width="300">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Subject}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Size" Width="75">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding SizeFormatted}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Poster" Width="175">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Poster}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Age" Width="75">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding AgeFormatted}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)

wpf

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