小编Vac*_*ano的帖子

Linq方法体最佳实践问题

这两个陈述中的哪一个是更快/更好的做法?

myList.Where(x =>
            {
                bool itemOne= x.ItemOne == paramItemOne;
                bool itemTwo = x.ItemTwo == paramItemTwo;
                return itemOne && itemTwo;
            })


myList.Where(x => x.ItemOne == paramItemOne).Where(x=>x.ItemTwo == paramItemTwo)
Run Code Online (Sandbox Code Playgroud)

还是他们一样?

c# linq

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

代码分析警告CA1004使用通用方法

我有以下通用方法:

// Load an object from the disk
public static T DeserializeObject<T>(String filename) where T : class
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

    try
    {
        TextReader textReader = new StreamReader(filename);
        var result = (T)xmlSerializer.Deserialize(textReader);
        textReader.Close();
        return result;
    }
    catch (FileNotFoundException)
    { }

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

当我编译时,我得到以下警告:
CA1004:Microsoft.Design:考虑一种设计,其中'MiscHelpers.DeserializeObject(string)'在任何调用中都不需要显式类型参数'T'.

我已经考虑过了这一点,我不知道如何通过限制可以反序列化的类型来完成它的请求.我坦率地承认,我可能错过了解决这个问题的简单方法.

但如果我不是,那么我唯一的办法是压制这个警告吗?我有一个没有警告或消息的干净项目.我想保持这种方式.

我想我在问"为什么这是一个警告?" 充其量这似乎应该是一个信息.甚至那似乎有点多.它可以或不能修复.如果它不能那么你只是坚持警告没有追索权,但压制它.我错了吗?

c# generics compiler-warnings

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

Windows Phone 7 ListBox无法自定义?

我刚刚开始使用Visual Studio 2010 Express for Windows Phone进行开发,并且我放入了一个ListBox.我似乎无法操纵格式.无论我做什么,背景为黑色/灰色,所选项目都有蓝色边框,列表框中的项目之间有填充(我正在使用图像).这是我的代码:

XAML:

<Grid x:Name="ContentGrid" Grid.Row="1">
    <Grid Name="gallery">
        <Grid.RowDefinitions>
            <RowDefinition Height="370" />
            <RowDefinition Height="150" />
        </Grid.RowDefinitions>
        <Border BorderBrush="Red" Width="450"  CornerRadius="4" BorderThickness="2" 
            Background="Red" Margin="10,30,20,10" Padding="6,6,6,6">
            <Image Grid.Row="0" Grid.Column="0" Height="360"  x:Name="imgPreview"/>
        </Border>
        <ListBox x:Name="lbScrollGallery" Grid.Row="1" Grid.Column="0" Padding="0"
              VerticalAlignment="Top" Width="450" SelectionChanged="ScrollerSelectionChanged" 
              d:LayoutOverrides="HorizontalAlignment" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Width="100" Height="100" Stretch="Fill" Name="imgSource" 
                       VerticalAlignment="Center"
                       Source="{Binding Path=Url}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)

有什么方法可以自定义列表框吗?喜欢将背景更改为红色或在不同的项目0之间进行填充?

windows-phone-7

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

找出一个对象是否具有特定的类作为祖先

在C#中,如何查找给定对象是否具有特定的祖先?

例如,假设我有以下类结构.

ContainerControl
 |
 +----> Form
          |
          +--> MyNormalForm
          |
          +--> MyCustomFormType
                  |
                  +---> MyCustomForm

如果我有这样的方法:

 void MyCoolMethod (Form form)
Run Code Online (Sandbox Code Playgroud)

如何找到表单是否来自MyCustomFormType?

c# inheritance

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

ProgressBar在Windows Phone 7中?

任何人都可以提供有关如何在Windows Phone 7中使用进度条的代码示例.

windows-phone-7

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

XNA Texture2D Dispose()+ ObjectDisposedException

我是XNA框架的新手.我正在XNA中为windows phone 7编写一个示例应用程序.

目前我正面临一个问题.

在示例中,我正在加载Texture2D并将其处理到下一行并将其指定为null.我再次将相同的图像加载到相同的成员变量.但是在抽奖中我得到了ObjectDisposedException.

如果我删除了dispose调用,它将不会给出任何异常.

请帮我解决这个问题.

样品:

Texture2D texture = null;
 protected override void LoadContent()
 {
      texture = Content.Load<Texture2D>("Back");
      texture .Dispose();
      texture = null;

      texture = Content.Load<Texture2D>("Back");
}


protected override void Draw(GameTime gameTime)
{
      GraphicsDevice.Clear(Color.CornflowerBlue);

      spriteBatch.Begin();
      spriteBatch.Draw(texture , new Vector2(0, 0), Color.White);

      spriteBatch.End();

       base.Draw(gameTime);
}
Run Code Online (Sandbox Code Playgroud)

windows-phone-7 xna-4.0

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

WPF SimpleCommand可能与泛型?

我正在使用此代码来创建一个简单的命令:

public class SimpleCommand : ICommand
{
    public Predicate<object> CanExecuteDelegate { get; set; }
    public Action<object> ExecuteDelegate { get; set; }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        if (CanExecuteDelegate != null)
            return CanExecuteDelegate(parameter);
        return true;// if there is no can execute default to true
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        if (ExecuteDelegate != null)
            ExecuteDelegate(parameter);
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

我没写这个.但我喜欢使用它.当我使用它时,它最终会像这样:

// …
Run Code Online (Sandbox Code Playgroud)

c# generics wpf command

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

添加扩展方法的方法?

假设我有一个名为的类LogHelper,它有一个名为的静态方法GetLogger(string name).

有没有办法添加一个静态扩展方法GetLogger()

我知道通常扩展方法在声明它们时是静态的,但有没有办法在它们"帮助"的类上显示静态?

c# c#-3.0

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

如何描述构建过程?

我的C#解决方案需要更长的时间来编译我想要的.

有没有办法分析构建过程,看看花了这么长时间?

visual-studio-2008 visual-studio

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

用空字符串替换变量字符串

我有一个xml文件,我正在使用linq-to-XML来读取.Linq-to-XML保留了换行的换行符和空格.

所以不要像这样的guid:

"FB5417E2-DB15-481d-80D6-D0C4CB26EB1F"

我得到这样的东西:

"\n    FB5417E2-DB15-481d-80D6-D0C4CB26EB1F"

我已经使用这种方法来尝试并帮助弥补这个:

public static string ValueTrimmed(this XElement element)
{         
    if (element != null)
        // Remove the newlines and spaces
        return element.Value.Replace("\n      ", "");

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

问题是这只适用于"\n"+6个空格.

有没有办法删除"\n"+任意数量的空格?

注意:我有一些场景,其中"\n"+ x空格位于值的内部.
例如:

TextTextTextTextTextTextText\n     TextTextTextTextTextTextText

c# string

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