小编The*_*der的帖子

PresentationFramework 中的 NullReference 异常

下面是一个最小的例子,我不可能再减少它了。

我在 ViewModel 中创建了一个实时过滤的 CollectionView,如下所示:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;

namespace AntiBonto.ViewModel
{
    [Serializable]
    public class Person
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public string Name { get; set; }
        public override string ToString()
        {
            return Name;
        }

        private int num;
        public int Num
        {
            get { return num; }
            set { num = value; RaisePropertyChanged(); }
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf

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

获取或设置访问器预期枚举

尝试创建枚举时,我收到以下编译错误:

"预期获取或设置访问者"

这是代码:

private Enum HolidayCalendarType
{
    BusinessDays,
    CalendarDays,        
}
Run Code Online (Sandbox Code Playgroud)

有什么问题?

c# enums

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

空条件运算符和void方法

在C#6之前,我会编写代码来处理像这样的对象:

if (_odbcConnection != null)
{
    _odbcConnection.Close();
    _odbcConnection.Dispose();
    _odbcConnection = null;
}
Run Code Online (Sandbox Code Playgroud)

有了6,我可以编写更少的代码:

_odbcConnection?.Close();
_odbcConnection?.Dispose();
_odbcConnection = null;
Run Code Online (Sandbox Code Playgroud)

但这两个是等价的吗?

c# c#-6.0 null-conditional-operator

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

用lambda语法覆盖

请考虑以下表达式:

public override string ToString() => "ABCDEFG";
Run Code Online (Sandbox Code Playgroud)

汇编到这个:

public override string ToString()
{
    return "ABCDEFG";
}
Run Code Online (Sandbox Code Playgroud)

尽管这种语法是语法糖,但我希望ToString()等同于Func<string>

Func<string> ToString = () => { return "ABCDEFG"; };
Run Code Online (Sandbox Code Playgroud)

请注意,Func<string>需要一个return语句,但ToString()覆盖的lambda语法不需要.

为什么覆盖的Lambda语法不需要使用return

c# lambda roslyn c#-6.0

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

在数组上使用'ref'关键字

我理解为什么ref在编写函数来交换两个值时应该使用,但我不知道如何在整个数组上使用该关键字.这听起来很愚蠢,但我已经尝试将关键字粘贴到我可能想到的任何地方(例如在参数之前,变量之前等等)但是我仍然得到以下错误:

错误1非静态字段,方法或属性'Swap.Program.swapRotations(int [])'需要对象引用

这是我到目前为止所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Swap
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A = {0, 1, 2, 3, 4, 5, 6, 7};

            swapRotations(A);

            for (int i = 0; i < A.Length; i++)
                Console.WriteLine(A[i]);

            Console.WriteLine("\nPress any key ...");
            Console.ReadKey();
        }

        private void swapRotations(int[] intArray)
        {
            int bone1Rot = intArray[3];
            int bone2Rot = intArray[5];

            // Make the swap.
            int temp = bone1Rot;
            bone1Rot = bone2Rot;
            bone2Rot …
Run Code Online (Sandbox Code Playgroud)

c# arrays ref

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

如何从带有子项的父对象列表中提取数据?

如果我在一个列表中的一个类中有一个列表,其中的类定义如下:

class Class1
{
    public int Id { get; set; }

    public List<Class2> Class2s { get; set; }
}

class Class2
{
    public string Name { get; set; }

    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我可以创建一个类型的列表,Result其中Result是:

class Result
{
    public int Class1Id { get; set; }

    public string Name { get; set; }

    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

请注意,Result该类包含来自Class1和 的值Class2

像这样:

var results = new …
Run Code Online (Sandbox Code Playgroud)

c# linq

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

如何更改其他活动的文本?

我正在使用 Android Studio 创建一个 Android 应用程序。

\n\n

该应用程序在 MainActivity.java 中启动,该 java 获取 Activity_main.xml 。在 Activity_main 上,用户可以选择 3 个按钮之一。无论他们选择哪个按钮,都会将他们带到相同的布局 - Primary_layout.xml 以及与之关联的 Java 类 PrimaryClass.java 。

\n\n

我在 Primary_layout 中有一个占位符。我希望这个占位符(id:占位符)根据之前选择的按钮进行更改。

\nEg. If Button 1 (id: button1) is clicked, then the placeholder must say \xe2\x80\x9cButton 1 was clicked.\xe2\x80\x9d\n
Or let\xe2\x80\x99s say Button 2 (id: button2) was clicked, then the placeholder must say \xe2\x80\x9cButton 2 was clicked\xe2\x80\x9d. And the same goes for Button 3.\nI have created an intent to open PrimaryClass but I\'m …

html java android textview android-activity

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

在string.format中使用特殊字符

如果我有以下字符串生成器:

var a = string.Format("{0}-{1}", model.FirstName, model.LastName); 
Run Code Online (Sandbox Code Playgroud)

我想使用特殊字符&#8211而不是-.我尝试了以下方法:

var a = string.Format("{0}\u8211{1}", model.FirstName, model.LastName);`
Run Code Online (Sandbox Code Playgroud)

但它给了我这个?象征.我怎样才能解决这个问题?

.net c# asp.net-mvc

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

对于MM/dd/yyyy hh:mm,DateTime.TryParseExact返回false

以下代码返回false给我,我只是无法弄清楚我做错了什么.

var localDateTimeString = "03/24/2016 21:05"; // subject.Substring(0, 16);

DateTime localDateTime;
if (!DateTime.TryParseExact(
    localDateTimeString,
    "MM/dd/yyyy hh:mm",
    CultureInfo.InvariantCulture,
    DateTimeStyles.None,
    out localDateTime)) return false;
Run Code Online (Sandbox Code Playgroud)

格式固定为16个字符串,日期,月份,分钟和小时始终为2位数.年份4位数.

但是这段代码返回false,我该如何修复呢?

.net c# datetime parsing

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

如果整整一年已经耗尽,请运行代码

我想每年根据用户创建帐户的日期向用户发送电子邮件.

我目前有代码检查自上次登录后是否已经过了几个月,并且想知道它是否可以修改,代码如下所示:

public static int GetMonthsBetween(DateTime from, DateTime to)
{
    if (from > to) return GetMonthsBetween(to, from);

    var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));

    if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
    {
        return monthDiff - 1;
    }
    else
    {
        return monthDiff;
    }
}

if (GetMonthsBetween(lastLoginDate, currentDate) >= 4)
Run Code Online (Sandbox Code Playgroud)

我只想在创建日期过去一整年后发送电子邮件,我该怎么做呢?

c# datetime

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