小编Gra*_*ush的帖子

从File.OpenRead()返回流

我正在编写一个允许ASP.Net网站检索文件的WCF服务(基于本文).我的问题是,当我返回流时,它是空白的.

为简单起见,我已将代码分离为一个简单的winforms应用程序,以尝试查找返回流的问题,这是代码:

    private Stream TestStream()
    {
        Stream fs = File.OpenRead(@"c:\testdocument.docx");
        return fs;
    }

    // This method converts the filestream into a byte array so that when it is 
    // used in my ASP.Net project the file can be sent using response.Write
    private void Test()
    {            
        System.IO.MemoryStream data = new System.IO.MemoryStream();
        System.IO.Stream str = TestStream();

        str.CopyTo(data);
        byte[] buf = new byte[data.Length];
        data.Read(buf, 0, buf.Length);                       
    }
Run Code Online (Sandbox Code Playgroud)

这段代码的结果buf是12,587字节长(文件的正确长度),但它只包含0.

如果我尝试它,Word文档会打开没有问题,我错过了一些明显的东西吗?

c# filestream

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

通过EF/Linq投射到KeyValuePair

我正在尝试从EF/Linq查询加载KeyValuePairs列表,如下所示:

return (from o in context.myTable 
select new KeyValuePair<int, string>(o.columnA, o.columnB)).ToList();
Run Code Online (Sandbox Code Playgroud)

我的问题是,这会导致错误

"LINQ to Entities中仅支持无参数构造函数和初始值设定项."

有一个简单的方法吗?我知道我可以为此创建一个自定义类而不是使用KeyValuePair,但这似乎重新发明了轮子.

c# linq entity-framework projection keyvaluepair

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

使用反射获取MemberInfo的类型

我正在使用反射来加载具有项目类结构的树视图.类中的每个成员都具有分配给它们的自定义属性.

获取类的属性我没有问题,MemberInfo.GetCustomAttributes()但是如果类成员是自定义类,然后需要解析自身以返回自定义属性,我需要一种方法.

到目前为止,我的代码是:

MemberInfo[] membersInfo = typeof(Project).GetProperties();

foreach (MemberInfo memberInfo in membersInfo)
{
    foreach (object attribute in memberInfo.GetCustomAttributes(true))
    {
        // Get the custom attribute of the class and store on the treeview
        if (attribute is ReportAttribute)
        {
            if (((ReportAttribute)attribute).FriendlyName.Length > 0)
            {
               treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName });
            }
        }
        // PROBLEM HERE : I need to work out if the object is a specific type
        //                and then use reflection to get the structure and attributes. …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection attributes custom-attributes

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

为listview列设置不同的对齐方式

我无法使用左对齐或居中对齐的列绘制列表视图.我已经看过了我在这里或其他论坛上找到的一些解决方案,但它们似乎适用于所有列,或者我无法让它们正常工作.

到目前为止我得到的最好的是这段代码,但是所有东西都是左对齐的(我已经把右对齐来测试代码).有人能告诉我哪里出错了吗?

<ListView Name="lsvQuestions" DockPanel.Dock="Bottom">                
     <ListView.View>
         <GridView>
             <GridViewColumn Width="450" Header="Question Text">
                 <GridViewColumn.CellTemplate>
                     <DataTemplate>
                         <TextBlock Text="{Binding QuestionText}" TextAlignment="Left"/>
                     </DataTemplate>
                 </GridViewColumn.CellTemplate>
                 </GridViewColumn>
                 <GridViewColumn Width="200" Header="Type">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding QuestionType}" TextAlignment="Right"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                 </GridViewColumn>
                 <GridViewColumn Width="100" Header="Page Number">
                     <GridViewColumn.CellTemplate>
                         <DataTemplate>
                             <TextBlock Text="{Binding QuestionPageNumber}" TextAlignment="Center"/>
                         </DataTemplate>
                     </GridViewColumn.CellTemplate>
                 </GridViewColumn>
                 <GridViewColumn Width="100" Header="Order">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding QuestionOrder}" TextAlignment="Center"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>               
    </ListView>
Run Code Online (Sandbox Code Playgroud)

wpf listview

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

在某些Android 4.X设备上未使用appcompat库应用主题

在将appcompat库更新到最新版本(v21)以应用材质设计之后,我的自定义主题不再在Android 4.0.X设备上使用,除非我在XML中明确声明它.

我在Android 2.3.X和5.0.0设备上测试了主题,它可以在这些设备上运行.但它不适用于我的HTC One S和Android 4.0.3.我需要做些什么来解决这个问题?

我的themes.xml

<style name="Theme.Custom" parent="@style/Theme.AppCompat.Light">
    <item name="android:editTextStyle">@style/Custom.Widget.EditText</item>
    <item name="android:buttonStyle">@style/Custom.Widget.Button</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我的styles.xml

<style name="Custom.Widget.EditText" parent="android:Widget.EditText">
    <item name="android:background">@drawable/edit_text_holo_light</item>
    <item name="android:textColor">@color/text_primary</item>
    <item name="android:textColorHint">@color/text_hint_color</item>
</style>

<style name="Custom.Widget.Button" parent="android:Widget.Button">
    <item name="android:background">@drawable/btn_default_holo_light</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">64dip</item>
    <item name="android:textColor">@color/button_text_primary</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <!-- other elements -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >

        <EditText
            android:id="@+id/view_username_edit_username_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textNoSuggestions|textCapSentences"
            android:hint="@string/UsernameScreen_usernameHint"
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:maxLength="@integer/username_max_length"
            android:textSize="@dimen/text_size_medium" />

        <Button
            android:id="@+id/view_username_save_Button"
            android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-appcompat android-theme material-design

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

Linq&Paging - 无法使用OrderBy返回分页数据

我一直在尝试在我编写的WCF服务上实现一个简单的分页系统,该服务使用Linq To SQL来查询数据库,但似乎是从一个问题转到另一个问题.

我希望WCF服务返回此类型的列表:

[DataContract]
public class TestType
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码:

int pageNumber = 0;
int pageSize = 25;

List<TestType> results = (from caseTypes in context.cch
                          select new TestType()
                          {
                              ID = caseTypes.cch_id,
                              Name = caseTypes.cch_case_ref
                          }                               
                          ).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList<TestType>(); 
Run Code Online (Sandbox Code Playgroud)

但是,当我运行代码时,我得到错误:

The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the …
Run Code Online (Sandbox Code Playgroud)

.net paging wcf linq-to-sql

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

苗条的PHP框架图像上传放数据库

我是瘦身php框架的新手,我想上传一个图像并将文件名放在数据库中POST,可以有人给我一些示例代码.

php file-upload slim

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

KeyEventArgs.Handled与KeyEventArgs.SupressKeyPress

使用之间有什么区别

e.Handled = true
Run Code Online (Sandbox Code Playgroud)

e.SuppressKeyPress = true
Run Code Online (Sandbox Code Playgroud)

我已经读过SuppressKeyPress调用e.Handled但其他情况呢?

.net c# vb.net winforms

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

如果在所需字符串之前未直接包含字符,则正则表达式仅匹配

我正在尝试解决这个CodingBat问题:

如果给定的字符串包含"xyz"的外观,则返回true,其中xyz不直接以句点(.)开头.所以"xxyz"计算但"x.xyz"没有.

xyzThere("abcxyz")→true
xyzThere("abc.xyz")→false
xyzThere("xyz.abc")→true

我试图用正则表达式解决这个问题,但我不确定如何处理where the xyz is not directly preceeded by a period需求.

我没有约束的问题的解决方案是这样的:

public boolean xyzThere(String str) {
    return str.matches(".*xyz.*");
}
Run Code Online (Sandbox Code Playgroud)

知道如何用正则表达式处理上述约束吗?

java regex

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

Durandal Google Analtyics Tracking

我正在使用Durandal 1.2和Durandal路由器插件,并希望通过Google Analytics跟踪SPA中的网页浏览量: window._gaq.push(['_trackPageview', location.pathname + location.search + location.hash]

我知道我可以监听hashchange事件,甚至可以通过Sammy进行连接.我宁愿不这样做,因为Durandal目前正在被重写以消除对Sammy的依赖.

我的问题是,有没有办法使用Durandal路由器插件设置它?

javascript google-analytics durandal

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