小编guo*_*ngj的帖子

如何在WPF程序中使用IDataErrorInfo.Error?

我有一个这样的对象:

public class Person : IDataErrorInfo
{
    public string PersonName{get;set;}
    public int Age{get;set;}

    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            if(propertyName=="PersonName")
            {
                if(PersonName.Length>30 || PersonName.Length<1)
                {
                    return "Name is required and less than 30 characters.";
                }
            }
            return null;
        }
    }

    string IDataErrorInfo.Error
    {
        get
        {
            if(PersonName=="Tom" && Age!=30)
            {
                return "Tom must be 30.";
            }
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

绑定PersonName和Age属性很简单:

<TextBox Text="{Binding PersonName, ValidatesOnDataErrors=True}" />
<TextBox Text="{Binding Age, ValidatesOnDataErrors=True}" />
Run Code Online (Sandbox Code Playgroud)

但是,如何使用Error属性并正确显示它?

validation wpf textbox idataerrorinfo

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

如何确保ASP.net Web API控制器的参数不为空?

我创建了一个ASP.net Web API控制器:

public class UsersController : ApiController
{
    //...
    public void Put([FromBody]User_API user, long UpdateTicks)
    {
        user.UpdateTicks = UpdateTicks;
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

如果客户端未提供正确的参数,则"user"参数将为null.我可以创建一个全局过滤器来检查这样的每个参数,如果发生任何错误,将返回400消息.

asp.net-web-api

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

如何将myBatis选择结果(列表)设置为object的属性?

通常,myBatis的select方法返回单个对象或通用List类型.例如,我想取一个班级的所有学生:

<select id="fetchStudentsOfClass" parameterType="int" resultMap="resultMapStudent">
    SELECT * FROM students WHERE class_id=#{id}
</select>
Run Code Online (Sandbox Code Playgroud)

我很容易得到这样的结果:List<Student>.

现在,如果我想得到这样的结果而不是List<Student>:

class MyClass
{
    List<Student> getStudents{return this.students;}
    void setStudents(List<Student> students){this.students = students}
    private List<Student> students;
}
Run Code Online (Sandbox Code Playgroud)

我能怎么做?

mybatis

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

多列HTML表的设计

最近,我遇到了HTML表格设计问题的麻烦。我有一个CS程序,我想将其重建为BS程序。这是UI屏幕截图。

在此处输入图片说明

如您所见,它有太多列。将出现一个水平滚动条。如何改善用户体验?

我试图将几列合并为一列,但是这带来了一些新问题-混乱,不利于过滤和排序。

如果您有很好的榜样,请告诉我。

html user-interface html-table

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

如何使用 HTML unicode 转义停止 ASP.NET Core 2 MVC 的 razor 引擎?

正常创建一个默认的ASP.NET Core 2 MVC项目,然后稍微修改一下动作:

public IActionResult About()
{
    ViewData["Message"] = "This is Chinese[??]";
    return View();
}
Run Code Online (Sandbox Code Playgroud)

这是视图(About.cshtml):

<h3>@ViewData["Message"]</h3>
<h3>?????</h3>
Run Code Online (Sandbox Code Playgroud)

这是浏览器中的输出结果:

<h3>This is Chinese[&#x4E2D;&#x6587;]</h3>
<h3>?????</h3>
Run Code Online (Sandbox Code Playgroud)

我发现'@'符号渲染的中文文本是html unicode转义的。问题是如何停止“@”符号来转义我的文本?

c# asp.net-core-mvc asp.net-core asp.net-core-2.0

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

收益率和尝试捕获量.(C#)

我遇到了一个非常奇怪的问题.以下代码未预期运行.

static IEnumerable<int> YieldFun()
{
    int[] numbers = new int[3] { 1, 2, 3 };

    if(numbers.Count()==3)
        throw new Exception("Test...");

    //This code continues even an exception was thrown above.
    foreach(int i in numbers)
    {
        if(i%2==1)
            yield return numbers[i];
    }
}

static void Main(string[] args)
{
    IEnumerable<int> result = null;
    try
    {
        result = YieldFun();
    }
    catch (System.Exception ex) //Cannot catch the exception
    {
        Console.WriteLine(ex.Message);
    }


    foreach (int i in result)
    {
        Console.Write(" " + i);
    }
}
Run Code Online (Sandbox Code Playgroud)

两个问题.首先,即使抛出异常,YieldFun似乎仍然可以继续工作.其次,调用者的try-catch块无法捕获抛出的异常.

为什么这个?以及如何解决这个问题?

c# yield return try-catch

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

MongoDB java 客户端的 WriteConcern 不起作用

我是使用 MongoDB 的新手。我刚刚通过 Maven 导入了最新的 MongoDB java 客户端:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.2.2</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

然后我写了一个非常简单的程序来测试插入操作。

        //I use a replicaset
    MongoClient mongoClient = new MongoClient(
            Arrays.asList(new ServerAddress("10.12.16.136", 29017),
                    new ServerAddress("10.12.16.136", 29018),
                    new ServerAddress("10.12.16.136", 29019)));

    //I just want to write and ignore any database errors. (This does not work)
    mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED);

    //Get database and collection
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection<Document> collection = database.getCollection("realtime");

    //This does not work too.
    collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED);

    //Make a simple object to insert. I already have a document with the same _id …
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-java

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